From f4d6952ffd0c6ee6ca63dc82362531a62dd9cf42 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Thu, 5 Dec 2024 10:06:30 +0000 Subject: [PATCH] Spike: can we simplify the GraphQL types? --- app/graphql/types/edition_type.rb | 184 +++++++++++------- app/graphql/types/ministers_index_type.rb | 135 ------------- app/graphql/types/news_article_type.rb | 12 -- .../query_type/edition_type_or_subtype.rb | 8 +- app/graphql/types/role_type.rb | 102 ---------- app/graphql/types/world_index_type.rb | 25 --- spec/graphql/types/news_article_type_spec.rb | 12 -- 7 files changed, 112 insertions(+), 366 deletions(-) delete mode 100644 app/graphql/types/ministers_index_type.rb delete mode 100644 app/graphql/types/news_article_type.rb delete mode 100644 app/graphql/types/role_type.rb delete mode 100644 app/graphql/types/world_index_type.rb delete mode 100644 spec/graphql/types/news_article_type_spec.rb diff --git a/app/graphql/types/edition_type.rb b/app/graphql/types/edition_type.rb index 6687ddef9..f33d7e7c9 100644 --- a/app/graphql/types/edition_type.rb +++ b/app/graphql/types/edition_type.rb @@ -7,14 +7,121 @@ class WithdrawnNotice < Types::BaseObject field :withdrawn_at, GraphQL::Types::ISO8601DateTime end + class EditionLinks < Types::BaseObject + links_field :active_top_level_browse_page, [EditionType] + links_field :associated_taxons, [EditionType] + links_field :available_translations, [EditionType] + links_field :child_taxons, [EditionType] + links_field :children, [EditionType] + links_field :contact, [EditionType] + links_field :contacts, [EditionType] + links_field :content_owners, [EditionType] + links_field :corporate_information_pages, [EditionType] + links_field :current_prime_minister, [EditionType] + links_field :document_collections, [EditionType] + links_field :documents, [EditionType] + links_field :email_alert_signup, [EditionType] + links_field :embed, [EditionType] + links_field :fatality_notices, [EditionType] + links_field :featured_policies, [EditionType] + links_field :field_of_operation, [EditionType] + links_field :fields_of_operation, [EditionType] + links_field :finder, [EditionType] + links_field :government, [EditionType] + links_field :historical_accounts, [EditionType] + links_field :home_page_offices, [EditionType] + links_field :lead_organisations, [EditionType] + links_field :level_one_taxons, [EditionType] + links_field :linked_items, [EditionType] + links_field :main_office, [EditionType] + links_field :mainstream_browse_pages, [EditionType] + links_field :manual, [EditionType] + links_field :meets_user_needs, [EditionType] + links_field :ministerial, [EditionType] + links_field :ministers, [EditionType] + links_field :office_staff, [EditionType] + links_field :ordered_also_attends_cabinet, [EditionType] + links_field :ordered_assistant_whips, [EditionType] + links_field :ordered_baronesses_and_lords_in_waiting_whips, [EditionType] + links_field :ordered_board_members, [EditionType] + links_field :ordered_cabinet_ministers, [EditionType] + links_field :ordered_chief_professional_officers, [EditionType] + links_field :ordered_child_organisations, [EditionType] + links_field :ordered_contacts, [EditionType] + links_field :ordered_featured_policies, [EditionType] + links_field :ordered_foi_contacts, [EditionType] + links_field :ordered_high_profile_groups, [EditionType] + links_field :ordered_house_lords_whips, [EditionType] + links_field :ordered_house_of_commons_whips, [EditionType] + links_field :ordered_junior_lords_of_the_treasury_whips, [EditionType] + links_field :ordered_military_personnel, [EditionType] + links_field :ordered_ministerial_departments, [EditionType] + links_field :ordered_ministers, [EditionType] + links_field :ordered_parent_organisations, [EditionType] + links_field :ordered_related_items, [EditionType] + links_field :ordered_related_items_overrides, [EditionType] + links_field :ordered_roles, [EditionType] + links_field :ordered_special_representatives, [EditionType] + links_field :ordered_successor_organisations, [EditionType] + links_field :ordered_traffic_commissioners, [EditionType] + links_field :organisations, [EditionType] + links_field :original_primary_publishing_organisation, [EditionType] + links_field :pages_part_of_step_nav, [EditionType] + links_field :pages_related_to_step_nav, [EditionType] + links_field :pages_secondary_to_step_nav, [EditionType] + links_field :parent, [EditionType] + links_field :parent_taxons, [EditionType] + links_field :part_of_step_navs, [EditionType] + links_field :people, [EditionType] + links_field :person, [EditionType] + links_field :policies, [EditionType] + links_field :policy_areas, [EditionType] + links_field :popular_links, [EditionType] + links_field :primary_publishing_organisation, [EditionType] + links_field :primary_role_person, [EditionType] + links_field :related, [EditionType] + links_field :related_guides, [EditionType] + links_field :related_mainstream_content, [EditionType] + links_field :related_policies, [EditionType] + links_field :related_statistical_data_sets, [EditionType] + links_field :related_to_step_navs, [EditionType] + links_field :related_topics, [EditionType] + links_field :role, [EditionType] + links_field :role_appointments, [EditionType] + links_field :roles, [EditionType] + links_field :root_taxon, [EditionType] + links_field :second_level_browse_pages, [EditionType] + links_field :secondary_role_person, [EditionType] + links_field :secondary_to_step_navs, [EditionType] + links_field :sections, [EditionType] + links_field :service_manual_topics, [EditionType] + links_field :speaker, [EditionType] + links_field :sponsoring_organisations, [EditionType] + links_field :suggested_ordered_related_items, [EditionType] + links_field :take_part_pages, [EditionType] + links_field :taxonomy_topic_email_override, [EditionType] + links_field :taxons, [EditionType] + links_field :top_level_browse_pages, [EditionType] + links_field :topical_events, [EditionType] + links_field :world_locations, [EditionType] + links_field :worldwide_organisation, [EditionType] + links_field :worldwide_organisations, [EditionType] + end + + field :active, Boolean, null: false field :analytics_identifier, String field :base_path, String field :content_id, ID + field :current, Boolean field :description, String field :details, GraphQL::Types::JSON, null: false field :document_type, String + field :ended_on, GraphQL::Types::ISO8601DateTime field :first_published_at, GraphQL::Types::ISO8601DateTime, null: false + field :iso2, String + field :links, EditionLinks, method: :itself field :locale, String, null: false + field :name, String, null: false field :phase, String, null: false field :public_updated_at, GraphQL::Types::ISO8601DateTime, null: false field :publishing_app, String @@ -23,9 +130,13 @@ class WithdrawnNotice < Types::BaseObject field :rendering_app, String field :scheduled_publishing_delay_seconds, Int field :schema_name, String + field :slug, String, null: false + field :started_on, GraphQL::Types::ISO8601DateTime field :state, String + field :supports_historical_accounts, Boolean field :title, String, null: false field :updated_at, GraphQL::Types::ISO8601DateTime + field :web_url, String field :withdrawn_notice, WithdrawnNotice def withdrawn_notice @@ -43,79 +154,6 @@ 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 diff --git a/app/graphql/types/ministers_index_type.rb b/app/graphql/types/ministers_index_type.rb deleted file mode 100644 index a2fec9be3..000000000 --- a/app/graphql/types/ministers_index_type.rb +++ /dev/null @@ -1,135 +0,0 @@ -# frozen_string_literal: true - -module Types - class MinistersIndexType < Types::EditionType - def self.document_types = %w[ministers_index] - - class MinistersIndexPerson < Types::BaseObject - class MinistersIndexRoleAppointment < Types::BaseObject - class MinistersIndexRoleAppointmentDetails < Types::BaseObject - field :current, Boolean - - def current - true - end - end - - class MinistersIndexRoleAppointmentLinks < Types::BaseObject - class MinistersIndexRole < Types::BaseObject - class MinistersIndexRoleDetails < Types::BaseObject - class MinistersIndexRoleDetailsWhipOrganisation < Types::BaseObject - field :label, String - field :sort_order, Integer - end - - field :role_payment_type, String - field :seniority, Integer - field :whip_organisation, MinistersIndexRoleDetailsWhipOrganisation - end - - field :content_id, String - field :details, MinistersIndexRoleDetails - field :title, String - field :web_url, String - end - - field :role, [MinistersIndexRole] - - def role - [object] - end - end - - field :details, MinistersIndexRoleAppointmentDetails, method: :itself - - field :links, MinistersIndexRoleAppointmentLinks, method: :itself - end - - class MinistersIndexPersonDetails < Types::BaseObject - class Image < Types::BaseObject - field :url, String - field :alt_text, String - end - - field :image, Image - field :privy_counsellor, Boolean - end - - class MinistersIndexPersonLinks < Types::BaseObject - field :role_appointments, [MinistersIndexRoleAppointment] - - def role_appointments - Edition - .live - .includes( - document: { - reverse_links: { # role -> role_appointment - link_set: { - documents: [ - :editions, # role_appointment - :link_set_links, # role_appointment -> person - ], - }, - }, - }, - ) - .where( - document_type: "ministerial_role", - document: { locale: "en" }, - reverse_links: { link_type: "role" }, - editions_documents: { document_type: "role_appointment" }, - link_set_links: { target_content_id: object.content_id, link_type: "person" }, - ) - .where("editions_documents.details ->> 'current' = 'true'") # editions_documents is the alias that Active Record gives to the role_appointment Editions in the SQL query - .order(reverse_links: { position: :asc }) - end - end - - field :base_path, String - field :details, MinistersIndexPersonDetails - field :links, MinistersIndexPersonLinks, method: :itself - field :title, String - field :web_url, String - end - - class Department < Types::BaseObject - class DepartmentRole < Types::BaseObject - field :content_id, String - end - - class DepartmentDetails < Types::BaseObject - class Logo < Types::BaseObject - field :crest, String - field :formatted_title, String - end - - field :brand, String - field :logo, Logo - end - - class DepartmentLinks < Types::BaseObject - links_field :ordered_ministers, [MinistersIndexPerson] - links_field :ordered_roles, [DepartmentRole] - end - - field :details, DepartmentDetails - field :links, DepartmentLinks, method: :itself - field :title, String - field :web_url, String - end - - class MinistersIndexLinks < Types::BaseObject - links_field :ordered_also_attends_cabinet, [MinistersIndexPerson] - links_field :ordered_assistant_whips, [MinistersIndexPerson] - links_field :ordered_baronesses_and_lords_in_waiting_whips, [MinistersIndexPerson] - links_field :ordered_cabinet_ministers, [MinistersIndexPerson] - links_field :ordered_house_lords_whips, [MinistersIndexPerson] - links_field :ordered_house_of_commons_whips, [MinistersIndexPerson] - links_field :ordered_junior_lords_of_the_treasury_whips, [MinistersIndexPerson] - - links_field :ordered_ministerial_departments, [Department] - end - - field :links, MinistersIndexLinks, method: :itself - end -end diff --git a/app/graphql/types/news_article_type.rb b/app/graphql/types/news_article_type.rb deleted file mode 100644 index dbc2e6bf0..000000000 --- a/app/graphql/types/news_article_type.rb +++ /dev/null @@ -1,12 +0,0 @@ -# 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 -end diff --git a/app/graphql/types/query_type/edition_type_or_subtype.rb b/app/graphql/types/query_type/edition_type_or_subtype.rb index 787f689e1..42b52a717 100644 --- a/app/graphql/types/query_type/edition_type_or_subtype.rb +++ b/app/graphql/types/query_type/edition_type_or_subtype.rb @@ -3,13 +3,7 @@ module Types class QueryType class EditionTypeOrSubtype < Types::BaseUnion - EDITION_TYPES = [ - Types::EditionType, - Types::MinistersIndexType, - Types::NewsArticleType, - Types::RoleType, - Types::WorldIndexType, - ].freeze + EDITION_TYPES = [Types::EditionType].freeze possible_types(*EDITION_TYPES) diff --git a/app/graphql/types/role_type.rb b/app/graphql/types/role_type.rb deleted file mode 100644 index b364953e1..000000000 --- a/app/graphql/types/role_type.rb +++ /dev/null @@ -1,102 +0,0 @@ -# frozen_string_literal: true - -module Types - class RoleType < Types::EditionType - def self.document_types = %w[ministerial_role] - - class Organisation < Types::BaseObject - field :base_path, String - field :title, String, null: false - end - - class RoleAppointment < Types::BaseObject - class Person < Types::BaseObject - class PersonDetails < Types::BaseObject - field :body, String - - def body - govspeak = object.fetch(:body, []) - .filter { _1[:content_type] == "text/govspeak" } - .map { _1[:content] } - .first - - Govspeak::Document.new(govspeak).to_html if govspeak.present? - end - end - - field :base_path, String - field :details, PersonDetails - field :title, String, null: false - end - - class RoleAppointmentDetails < Types::BaseObject - field :current, Boolean - field :ended_on, GraphQL::Types::ISO8601DateTime - field :started_on, GraphQL::Types::ISO8601DateTime - end - - field :details, RoleAppointmentDetails - - class RoleAppointmentLinks < Types::BaseObject - field :person, [Person] - - def person - Edition - .live - .joins(document: { reverse_links: :link_set }) - .where( - document: { locale: "en" }, - link_set: { content_id: object.content_id }, - reverse_links: { link_type: "person" }, - ) - .limit(1) - end - end - - field :links, RoleAppointmentLinks, method: :itself - end - - class RoleDetails < Types::BaseObject - field :body, String - field :supports_historical_accounts, Boolean - - def body - govspeak = object.fetch(:body, []) - .filter { _1[:content_type] == "text/govspeak" } - .map { _1[:content] } - .first - - Govspeak::Document.new(govspeak).to_html if govspeak.present? - end - end - - class RoleLinks < EditionType::EditionLinks - field :ordered_parent_organisations, [Organisation] - field :role_appointments, [RoleAppointment] - - def ordered_parent_organisations - Edition - .live - .joins(document: { reverse_links: :link_set }) - .where( - document: { locale: "en" }, - link_set: { content_id: object.content_id }, - reverse_links: { link_type: "ordered_parent_organisations" }, - ) - end - - def role_appointments - Edition - .live - .joins(document: :link_set_links) - .where( - document: { locale: "en" }, - link_set_links: { target_content_id: object.content_id, link_type: "role" }, - ) - end - end - - field :details, RoleDetails - field :links, RoleLinks, method: :itself - end -end diff --git a/app/graphql/types/world_index_type.rb b/app/graphql/types/world_index_type.rb deleted file mode 100644 index 5e48ab573..000000000 --- a/app/graphql/types/world_index_type.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module Types - class WorldIndexType < Types::EditionType - def self.document_types = %w[world_index] - - class WorldLocation < Types::BaseObject - field :active, Boolean, null: false - field :analytics_identifier, String - field :content_id, ID, null: false - field :iso2, String - field :name, String, null: false - field :slug, String, null: false - field :updated_at, GraphQL::Types::ISO8601DateTime, null: false - end - - class WorldIndexDetails < BaseObject - field :body, String - field :international_delegations, [WorldLocation], null: false - field :world_locations, [WorldLocation], null: false - end - - field :details, WorldIndexDetails - end -end diff --git a/spec/graphql/types/news_article_type_spec.rb b/spec/graphql/types/news_article_type_spec.rb deleted file mode 100644 index a4d586e04..000000000 --- a/spec/graphql/types/news_article_type_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -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