Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for news articles to GraphQL endpoint #3008

Merged
merged 6 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions app/graphql/types/edition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WithdrawnNotice < Types::BaseObject
field :rendering_app, String
field :scheduled_publishing_delay_seconds, Int
field :schema_name, String
field :state, String
field :title, String, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime
field :withdrawn_notice, WithdrawnNotice
Expand All @@ -42,6 +43,79 @@ def not_stored_in_publishing_api
alias_method :publishing_scheduled_at, :not_stored_in_publishing_api
alias_method :scheduled_publishing_delay_seconds, :not_stored_in_publishing_api

class Translation < Types::BaseObject
field :locale, String
field :base_path, String
end

class GovernmentDetails < Types::BaseObject
field :current, Boolean
end

class GovernmentLink < Types::BaseObject
field :base_path, String
field :content_id, String
field :details, GovernmentDetails
field :title, String
end

class OrganisationLink < Types::BaseObject
field :base_path, String
field :content_id, String
field :title, String
end

class PersonLink < Types::BaseObject
field :base_path, String
field :content_id, String
field :title, String
end

class Taxon < Types::BaseObject
field :base_path, String
field :content_id, String
field :document_type, String
field :phase, String
field :title, String
end

class TaxonLink < Taxon
class TaxonLinks < Types::BaseObject
links_field :parent_taxons, [Taxon]
end

field :links, TaxonLinks, method: :itself
end

class TopicalEventLink < Types::BaseObject
field :base_path, String
field :content_id, String
field :title, String
end

class WorldLocationLink < Types::BaseObject
field :base_path, String
field :content_id, String
field :title, String
end

class EditionLinks < Types::BaseObject
field :available_translations, [Translation]
links_field :government, [GovernmentLink]
links_field :organisations, [OrganisationLink]
links_field :people, [PersonLink]
links_field :taxons, [TaxonLink]
links_field :topical_events, [TopicalEventLink]
links_field :world_locations, [WorldLocationLink]

def available_translations
Presenters::Queries::AvailableTranslations.by_edition(object)
.translations.fetch(:available_translations, [])
end
end

field :links, EditionLinks, method: :itself

private

def presented_edition
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/ministers_index_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Types
class MinistersIndexType < Types::EditionType
def self.document_type = "ministers_index"
def self.document_types = %w[ministers_index]

class MinistersIndexPerson < Types::BaseObject
class MinistersIndexRoleAppointment < Types::BaseObject
Expand Down
12 changes: 12 additions & 0 deletions app/graphql/types/news_article_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Types
class NewsArticleType < Types::EditionType
def self.document_types = %w[
government_response
news_story
press_release
world_news_story
]
end
Comment on lines +4 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: approach

Given the lack of type-specific fields, I'm curious if there's a benefit to this versus just falling back to the generic EditionType

end
5 changes: 3 additions & 2 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module Types
class QueryType < Types::BaseObject
field :edition, EditionTypeOrSubtype, description: "An edition or one of its subtypes" do
argument :base_path, String
argument :content_store, String, required: false, default_value: "live"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: interface

  • Will content_store be the most obvious name for this argument for frontend devs/in client code (versus e.g. state or publishing_state)?
  • Do you know what the difference is between content_store, state, and phase (and why is content_store the correct one to use here?)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we're gonna have to face this decision at some point! I wonder if there's already a Content Store-free name for this concept in use?

end

def edition(base_path:)
Edition.live.find_by(base_path:)
def edition(base_path:, content_store:)
Edition.where(content_store:).find_by(base_path:)
end
end
end
10 changes: 8 additions & 2 deletions app/graphql/types/query_type/edition_type_or_subtype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
module Types
class QueryType
class EditionTypeOrSubtype < Types::BaseUnion
EDITION_TYPES = [Types::EditionType, Types::RoleType, Types::WorldIndexType, Types::MinistersIndexType].freeze
EDITION_TYPES = [
Types::EditionType,
Types::MinistersIndexType,
Types::NewsArticleType,
Types::RoleType,
Types::WorldIndexType,
].freeze

possible_types(*EDITION_TYPES)

Expand All @@ -12,7 +18,7 @@ def resolve_type(object, _context)
document_type = object.document_type

matching_edition_subtype = Types::EditionType.descendants.find do |edition_subtype|
document_type == edition_subtype.document_type
edition_subtype.document_types.include?(document_type)
end

matching_edition_subtype || Types::EditionType
Expand Down
15 changes: 2 additions & 13 deletions app/graphql/types/role_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Types
class RoleType < Types::EditionType
def self.document_type = "ministerial_role"
def self.document_types = %w[ministerial_role]

class Organisation < Types::BaseObject
field :base_path, String
Expand Down Expand Up @@ -56,11 +56,6 @@ def person
field :links, RoleAppointmentLinks, method: :itself
end

class Translation < Types::BaseObject
field :locale, String
field :base_path, String
end

class RoleDetails < Types::BaseObject
field :body, String
field :supports_historical_accounts, Boolean
Expand All @@ -75,16 +70,10 @@ def body
end
end

class RoleLinks < Types::BaseObject
field :available_translations, [Translation]
class RoleLinks < EditionType::EditionLinks
field :ordered_parent_organisations, [Organisation]
field :role_appointments, [RoleAppointment]

def available_translations
Presenters::Queries::AvailableTranslations.by_edition(object)
.translations.fetch(:available_translations, [])
end

def ordered_parent_organisations
Edition
.live
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/world_index_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Types
class WorldIndexType < Types::EditionType
def self.document_type = "world_index"
def self.document_types = %w[world_index]

class WorldLocation < Types::BaseObject
field :active, Boolean, null: false
Expand Down
12 changes: 12 additions & 0 deletions spec/graphql/types/news_article_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
RSpec.describe Types::NewsArticleType do
describe ".document_types" do
it "defines the document types for .resolve_type to distinguish the type from the generic Edition type" do
expect(described_class.document_types).to eq(%w[
government_response
news_story
press_release
world_news_story
])
end
end
end
101 changes: 101 additions & 0 deletions spec/graphql/types/query_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
RSpec.describe Types::QueryType do
include GraphQL::Testing::Helpers.for(PublishingApiSchema)

describe "#edition" do
let(:query) do
<<~QUERY
query($basePath: String!, $contentStore: String!) {
edition(basePath: $basePath, contentStore: $contentStore) {
... on Edition {
basePath
state
}
}
}
QUERY
end

context "when there is only a draft edition" do
let(:draft_edition) { create(:draft_edition) }

context "requesting the draft edition" do
it "returns the draft edition" do
expected_data = {
"basePath" => draft_edition.base_path,
"state" => "draft",
}

result = PublishingApiSchema.execute(query, variables: { basePath: draft_edition.base_path, contentStore: "draft" })
edition_data = result.dig("data", "edition")
expect(edition_data).to eq(expected_data)
end
end

context "requesting the live edition" do
it "returns no edition" do
result = PublishingApiSchema.execute(query, variables: { basePath: draft_edition.base_path, contentStore: "live" })
edition_data = result.dig("data", "edition")
expect(edition_data).to be_nil
end
end
end

context "when there is only a live edition" do
let(:live_edition) { create(:live_edition) }

context "requesting the draft edition" do
it "returns no edition" do
result = PublishingApiSchema.execute(query, variables: { basePath: live_edition.base_path, contentStore: "draft" })
edition_data = result.dig("data", "edition")
expect(edition_data).to be_nil
end
end

context "requesting the live edition" do
it "returns the live edition" do
expected_data = {
"basePath" => live_edition.base_path,
"state" => "published",
}

result = PublishingApiSchema.execute(query, variables: { basePath: live_edition.base_path, contentStore: "live" })
edition_data = result.dig("data", "edition")
expect(edition_data).to eq(expected_data)
end
end
end

context "when there is a published edition and a newer draft edition" do
let(:document) { create(:document) }
let(:base_path) { "/foo" }
let!(:live_edition) { create(:live_edition, document:, base_path:, user_facing_version: 1) }
let!(:draft_edition) { create(:draft_edition, document:, base_path:, user_facing_version: 2) }

context "requesting the draft edition" do
it "returns the draft edition" do
expected_data = {
"basePath" => draft_edition.base_path,
"state" => "draft",
}

result = PublishingApiSchema.execute(query, variables: { basePath: draft_edition.base_path, contentStore: "draft" })
edition_data = result.dig("data", "edition")
expect(edition_data).to eq(expected_data)
end
end

context "requesting the live edition" do
it "returns the live edition" do
expected_data = {
"basePath" => live_edition.base_path,
"state" => "published",
}

result = PublishingApiSchema.execute(query, variables: { basePath: live_edition.base_path, contentStore: "live" })
edition_data = result.dig("data", "edition")
expect(edition_data).to eq(expected_data)
end
end
end
end
end
6 changes: 3 additions & 3 deletions spec/graphql/types/world_index_type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe "Types::WorldIndexType" do
describe ".document_type" do
it "defines a document type for .resolve_type to distinguish the type from the generic Edition type" do
expect(Types::WorldIndexType.document_type).to eq("world_index")
describe ".document_types" do
it "defines the document types for .resolve_type to distinguish the type from the generic Edition type" do
expect(Types::WorldIndexType.document_types).to eq(%w[world_index])
end
end
end
Loading
Loading