Skip to content

Commit

Permalink
Update GraphQL approach for world index
Browse files Browse the repository at this point in the history
Given we're still in the learning stage with the GraphQL project, we've
decided to minimise the changes needed on frontend apps by retaining the
current heavily nested data structure. This updates Collections to work
with that format (the changes to Publishing API are also in progress).
This means that the code for the world index page in collections will be
less divergent between paths for GraphQL and REST data sources. This
commit undoes some of the divergence we initially introduced

We plan to revisit simplifying the data structure later (into a less
nested form)
  • Loading branch information
yndajas committed Dec 13, 2024
1 parent 071d887 commit 5ba7de7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 129 deletions.
13 changes: 7 additions & 6 deletions app/controllers/world_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class WorldController < ApplicationController
def index
if Features.graphql_feature_enabled? || params.include?(:graphql)
index = WorldIndexGraphql.find!("/world")
@presented_index = WorldIndexGraphqlPresenter.new(index)
if params.include?(:graphql)
world_index = Graphql::WorldIndex.find!(request.path)
content_item_data = world_index.content_item
else
index = WorldIndex.find!("/world")
@presented_index = WorldIndexPresenter.new(index)
world_index = WorldIndex.find!(request.path)
content_item_data = world_index.content_item.content_item_data
end

setup_content_item_and_navigation_helpers(index)
@presented_index = WorldIndexPresenter.new(content_item_data)
setup_content_item_and_navigation_helpers(world_index)
end
end
19 changes: 19 additions & 0 deletions app/models/graphql/world_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Graphql::WorldIndex
attr_reader :content_item

def initialize(content_item)
@content_item = content_item
end

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

# binding.pry

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

new(edition)
end
end
15 changes: 0 additions & 15 deletions app/presenters/world_index_graphql_presenter.rb

This file was deleted.

14 changes: 7 additions & 7 deletions app/presenters/world_index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class WorldIndexPresenter
delegate :count, to: :world_locations, prefix: true
delegate :count, to: :international_delegations, prefix: true

def initialize(world_index)
@world_index = world_index
def initialize(content_item_data)
@content_item_data = content_item_data
end

def title
@world_index.content_item.title
@content_item_data.fetch("title")
end

def grouped_world_locations
world_locations
@content_item_data.dig("details", "world_locations")
.group_by { |location| location_group(location) }
.sort
end

def international_delegations
details["international_delegations"]
@content_item_data.dig("details", "international_delegations")
end

def world_location_link(world_location)
Expand All @@ -45,10 +45,10 @@ def location_group(location)
end

def details
@world_index.content_item.details
@content_item_data.fetch("details")
end

def world_locations
details["world_locations"]
@content_item_data.dig("details", "world_locations")
end
end
12 changes: 7 additions & 5 deletions app/queries/graphql/world_index_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def query
... on WorldIndex {
title
worldLocations {
...worldLocationInfo
}
details {
worldLocations {
...worldLocationInfo
}
internationalDelegations {
...worldLocationInfo
internationalDelegations {
...worldLocationInfo
}
}
}
}
Expand Down
95 changes: 0 additions & 95 deletions spec/presenters/world_index_graphql_presenter_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/presenters/world_index_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
fixture = GovukSchemas::Example.find("world_index", example_name: "world_index")
content_item = ContentItem.new(fixture)
world_index = WorldIndex.new(content_item)
@world_index_presenter ||= described_class.new(world_index)
@world_index_presenter ||= described_class.new(world_index.content_item.content_item_data)
end

describe "#title" do
Expand Down

0 comments on commit 5ba7de7

Please sign in to comment.