Skip to content

Commit

Permalink
Merge pull request #3872 from alphagov/graphql-ministers-index
Browse files Browse the repository at this point in the history
Use GraphQL for rendering Ministers Index page
  • Loading branch information
mike3985 authored Nov 29, 2024
2 parents 3c62ae7 + a25eba3 commit 6a47e1c
Show file tree
Hide file tree
Showing 9 changed files with 1,434 additions and 118 deletions.
11 changes: 9 additions & 2 deletions app/controllers/ministers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ class MinistersController < ApplicationController
around_action :switch_locale

def index
ministers_index = MinistersIndex.find!(request.path)
@presented_ministers = MinistersIndexPresenter.new(ministers_index)
if Features.graphql_feature_enabled? || params.include?(:graphql)
ministers_index = Graphql::MinistersIndex.find!(request.path)
content_item_data = ministers_index.content_item
else
ministers_index = MinistersIndex.find!(request.path)
content_item_data = ministers_index.content_item.content_item_data
end

@presented_ministers = MinistersIndexPresenter.new(content_item_data)
setup_content_item_and_navigation_helpers(ministers_index)
end
end
17 changes: 17 additions & 0 deletions app/models/graphql/ministers_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Graphql::MinistersIndex
attr_reader :content_item

def initialize(content_item)
@content_item = content_item
end

def self.find!(base_path)
query = Graphql::MinistersIndexQuery.new(base_path).query

edition = Services.publishing_api.graphql_query(query)
.dig("data", "edition")
.deep_transform_keys(&:underscore)

new(edition)
end
end
18 changes: 9 additions & 9 deletions app/presenters/ministers_index_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
class MinistersIndexPresenter
def initialize(ministers_index)
@ministers_index = ministers_index.content_item
def initialize(content_item_data)
@content_item_data = content_item_data
end

def lead_paragraph
@ministers_index.details.fetch("body", nil)
@content_item_data.fetch("details", {})["body"]
end

def is_during_reshuffle?
@ministers_index.details.fetch("reshuffle", nil)
@content_item_data.fetch("details", {})["reshuffle"]
end

def reshuffle_messaging
@ministers_index.details.dig("reshuffle", "message")
@content_item_data.dig("details", "reshuffle", "message")
end

def cabinet_ministers
ordered_cabinet_ministers = @ministers_index.content_item_data.dig("links", "ordered_cabinet_ministers") || []
ordered_cabinet_ministers = @content_item_data.dig("links", "ordered_cabinet_ministers") || []
ordered_cabinet_ministers.map do |minister_data|
Minister.new(minister_data)
end
end

def also_attends_cabinet
ordered_also_attends_cabinet = @ministers_index.content_item_data.dig("links", "ordered_also_attends_cabinet") || []
ordered_also_attends_cabinet = @content_item_data.dig("links", "ordered_also_attends_cabinet") || []
ordered_also_attends_cabinet.map do |minister_data|
Minister.new(minister_data)
end
end

def by_organisation
ordered_ministerial_departments = @ministers_index.content_item_data.dig("links", "ordered_ministerial_departments") || []
ordered_ministerial_departments = @content_item_data.dig("links", "ordered_ministerial_departments") || []
ordered_ministerial_departments.map do |department_data|
Department.new(
url: department_data.fetch("web_url"),
Expand All @@ -49,7 +49,7 @@ def by_organisation

def whips
ordered_whip_organisations.each do |whip_org|
ordered_whip_organisation = @ministers_index.content_item_data.dig("links", whip_org.item_key) || []
ordered_whip_organisation = @content_item_data.dig("links", whip_org.item_key) || []
whip_org.ministers = ordered_whip_organisation.map do |minister_data|
Minister.new(minister_data, whip_only: true)
end
Expand Down
111 changes: 111 additions & 0 deletions app/queries/graphql/ministers_index_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class Graphql::MinistersIndexQuery
def initialize(base_path)
@base_path = base_path
end

def query
<<-QUERY
{
edition(basePath: "#{@base_path}") {
... on MinistersIndex {
basePath
links {
orderedCabinetMinisters {
...basePersonInfo
}
orderedAlsoAttendsCabinet {
...basePersonInfo
}
orderedAssistantWhips {
...basePersonInfo
}
orderedBaronessesAndLordsInWaitingWhips {
...basePersonInfo
}
orderedHouseLordsWhips {
...basePersonInfo
}
orderedHouseOfCommonsWhips {
...basePersonInfo
}
orderedJuniorLordsOfTheTreasuryWhips {
...basePersonInfo
}
orderedMinisterialDepartments {
title
webUrl
details {
brand
logo {
crest
formattedTitle
}
}
links {
orderedMinisters {
...basePersonInfo
}
orderedRoles {
contentId
}
}
}
}
}
}
}
fragment basePersonInfo on MinistersIndexPerson {
title
basePath
webUrl
details {
privyCounsellor
image {
url
altText
}
}
links {
roleAppointments {
details {
current
}
links {
role {
contentId
title
webUrl
details {
rolePaymentType
seniority
whipOrganisation {
label
sortOrder
}
}
}
}
}
}
}
QUERY
end
end
Loading

0 comments on commit 6a47e1c

Please sign in to comment.