Skip to content

Commit

Permalink
Merge pull request #2995 from alphagov/add-details-and-links-hashes-t…
Browse files Browse the repository at this point in the history
…o-graphql-role

Add "details" and "links" to GraphQL Role
  • Loading branch information
mike3985 authored Nov 25, 2024
2 parents 92fa78a + da4ec33 commit 5ad4297
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 140 deletions.
150 changes: 75 additions & 75 deletions app/graphql/types/role_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,103 +11,103 @@ class Organisation < Types::BaseObject

class RoleAppointment < Types::BaseObject
class Person < Types::BaseObject
field :base_path, String
field :biography, String
field :title, String, null: false
class PersonDetails < Types::BaseObject
field :body, String

def biography
Presenters::EditionPresenter
.new(object)
.present
.dig(:details, :body)
.find { |body| body[:content_type] == "text/html" }[:content]
end
end
def body
govspeak = object.fetch(:body, [])
.filter { _1[:content_type] == "text/govspeak" }
.map { _1[:content] }
.first

field :ended_on, GraphQL::Types::ISO8601DateTime
field :person, Person
field :started_on, GraphQL::Types::ISO8601DateTime
Govspeak::Document.new(govspeak).to_html if govspeak.present?
end
end

def ended_on
object.details[:ended_on]
field :base_path, String
field :details, PersonDetails
field :title, String, null: false
end

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" },
)
.first
class RoleAppointmentDetails < Types::BaseObject
field :current, Boolean
field :ended_on, GraphQL::Types::ISO8601DateTime
field :started_on, GraphQL::Types::ISO8601DateTime
end

def started_on
object.details[:started_on]
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 Translation < Types::BaseObject
field :locale, String
field :base_path, String
end

field :available_translations, [Translation]
field :current_role_appointment, RoleAppointment
field :ordered_parent_organisations, [Organisation]
field :past_role_appointments, [RoleAppointment]
field :responsibilities, String
field :supports_historical_accounts, Boolean
class RoleDetails < Types::BaseObject
field :body, String
field :supports_historical_accounts, Boolean

def available_translations
Presenters::Queries::AvailableTranslations.by_edition(object)
.translations.fetch(:available_translations, [])
end
def body
govspeak = object.fetch(:body, [])
.filter { _1[:content_type] == "text/govspeak" }
.map { _1[:content] }
.first

def current_role_appointment
Edition
.live
.joins(document: :link_set_links)
.where(
document: { locale: "en" },
link_set_links: { target_content_id: object.content_id, link_type: "role" },
)
.where("details ->> 'current' = 'true'")
.first
Govspeak::Document.new(govspeak).to_html if govspeak.present?
end
end

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
class RoleLinks < Types::BaseObject
field :available_translations, [Translation]
field :ordered_parent_organisations, [Organisation]
field :role_appointments, [RoleAppointment]

def past_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" },
)
.where("details ->> 'current' = 'false'")
end
def available_translations
Presenters::Queries::AvailableTranslations.by_edition(object)
.translations.fetch(:available_translations, [])
end

def responsibilities
presented_edition
.dig(:details, :body)
.find { |body| body[:content_type] == "text/html" }[:content]
end
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 supports_historical_accounts
object.details[:supports_historical_accounts]
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
154 changes: 89 additions & 65 deletions spec/integration/graphql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,91 +354,115 @@
edition(basePath: \"/government/ministers/prime-minister\") {
... on Role {
basePath
title
responsibilities
supportsHistoricalAccounts
locale
title
availableTranslations {
basePath
locale
details {
body
supportsHistoricalAccounts
}
currentRoleAppointment {
person {
...basePersonInfo
biography
links {
availableTranslations {
basePath
locale
}
}
pastRoleAppointments {
endedOn
startedOn
person {
...basePersonInfo
roleAppointments {
details {
current
endedOn
startedOn
}
links {
person {
basePath
title
details {
body
}
}
}
}
}
orderedParentOrganisations {
basePath
title
orderedParentOrganisations {
basePath
title
}
}
}
}
}
fragment basePersonInfo on Person {
basePath
title
}",
}

expected = {
data: {
edition: {
availableTranslations: [
{
basePath: "/government/ministers/prime-minister",
locale: "en",
},
{
basePath: "/government/ministers/prime-minister.es",
locale: "es",
},
],
basePath: "/government/ministers/prime-minister",
currentRoleAppointment: {
person: {
basePath: "/government/people/keir-starmer",
biography: "<p>Sir Keir Starmer became Prime Minister on 5 July 2024.</p>\n",
title: "The Rt Hon Sir Keir Starmer KCB KC MP",
},
},
locale: "en",
orderedParentOrganisations: [
{
basePath: "/government/organisations/cabinet-office",
title: "Cabinet Office",
},
{
basePath: "/government/organisations/prime-ministers-office-10-downing-street",
title: "Prime Minister's Office, 10 Downing Street",
},
],
pastRoleAppointments: [
{
endedOn: "2024-07-05T00:00:00Z",
person: {
basePath: "/government/people/rishi-sunak",
title: "The Rt Hon Rishi Sunak MP",
},
startedOn: "2022-10-25T00:00:00Z",
},
],
responsibilities: "<h1 id=\"prime-minister\">Prime Minister</h1>\n<p>The Prime Minister is the leader of His Majesty’s Government</p>\n",
supportsHistoricalAccounts: true,
title: "Prime Minister",
details: {
body: "<h1 id=\"prime-minister\">Prime Minister</h1>\n<p>The Prime Minister is the leader of His Majesty’s Government</p>\n",
supportsHistoricalAccounts: true,
},
links: {
availableTranslations: [
{
basePath: "/government/ministers/prime-minister",
locale: "en",
},
{
basePath: "/government/ministers/prime-minister.es",
locale: "es",
},
],
roleAppointments: [
{
details: {
current: true,
endedOn: nil,
startedOn: "2024-07-05T00:00:00Z",
},
links: {
person: [
basePath: "/government/people/keir-starmer",
title: "The Rt Hon Sir Keir Starmer KCB KC MP",
details: {
body: "<p>Sir Keir Starmer became Prime Minister on 5 July 2024.</p>\n",
},
],
},
},
{
details: {
current: false,
endedOn: "2024-07-05T00:00:00Z",
startedOn: "2022-10-25T00:00:00Z",
},
links: {
person: [
basePath: "/government/people/rishi-sunak",
details: {
body: "<p>Rishi Sunak was Prime Minister between 25 October 2022 and 5 July 2024.</p>\n",
},
title: "The Rt Hon Rishi Sunak MP",
],
},
},
],
orderedParentOrganisations: [
{
basePath: "/government/organisations/cabinet-office",
title: "Cabinet Office",
},
{
basePath: "/government/organisations/prime-ministers-office-10-downing-street",
title: "Prime Minister's Office, 10 Downing Street",
},
],
},
},
},
}
Expand Down

0 comments on commit 5ad4297

Please sign in to comment.