Skip to content

Commit

Permalink
Use GraphQL for rendering World Index page
Browse files Browse the repository at this point in the history
This will allow us to build a proof of concept for using GraphQL on
GOV.UK.

It will run alongside the existing retrieval from Content Store to allow
us to switch the rendering used in different environment, through a
feature flag or A-B test.

The feature is switched on by adding a `GRAPHQL_FEATURE_FLAG`
environment variable.
  • Loading branch information
brucebolt committed Oct 31, 2024
1 parent 6192fcf commit 91069a7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 7 deletions.
10 changes: 8 additions & 2 deletions app/controllers/world_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class WorldController < ApplicationController
def index
index = WorldIndex.find!("/world")
@presented_index = WorldIndexPresenter.new(index)
if ENV["GRAPHQL_FEATURE_FLAG"]
index = WorldIndexGraphql.find!("/world")
@presented_index = WorldIndexPresenterGraphql.new(index)
else
index = WorldIndex.find!("/world")
@presented_index = WorldIndexPresenter.new(index)
end

setup_content_item_and_navigation_helpers(index)
end
end
14 changes: 14 additions & 0 deletions app/models/world_index_graphql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class WorldIndexGraphql
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

edition = Services.publishing_api.graphql_query(query).dig("data", "edition")
new(edition)
end
end
15 changes: 15 additions & 0 deletions app/presenters/world_index_presenter_graphql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class WorldIndexPresenterGraphql < WorldIndexPresenter
def title
@world_index.content_item["title"]
end

def international_delegations
@world_index.content_item["internationalDelegations"]
end

private

def world_locations
@world_index.content_item["worldLocations"]
end
end
31 changes: 31 additions & 0 deletions app/queries/graphql/world_index_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Graphql::WorldIndexQuery
def initialize(base_path)
@base_path = base_path
end

def query
<<-QUERY
fragment worldLocationInfo on WorldLocation {
active
name
slug
}
{
edition(basePath: "#{@base_path}") {
... on WorldIndex {
title
worldLocations {
...worldLocationInfo
}
internationalDelegations {
...worldLocationInfo
}
}
}
}
QUERY
end
end
49 changes: 44 additions & 5 deletions spec/controllers/world_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
RSpec.describe WorldController do
describe "GET index" do
before do
stub_content_store_has_item("/world", GovukSchemas::Example.find("world_index", example_name: "world_index"))
context "without the GraphQL feature flag" do
before do
stub_content_store_has_item("/world", GovukSchemas::Example.find("world_index", example_name: "world_index"))
end

it "has a success response" do
get :index
expect(response).to have_http_status(:success)
end
end

it "has a success response" do
get :index
expect(response).to have_http_status(:success)
context "with the GraphQL feature flag" do
before do
stub_const("ENV", { "GRAPHQL_FEATURE_FLAG" => true })

graphql_query = Graphql::WorldIndexQuery.new("/world").query

graphql_response = {
"data": {
"edition": {
"title": "Help and services around the world",
"worldLocations": [
{
"active": true,
"name": "Test World Location",
"slug": "test-world-location",
},
],
"internationalDelegations": [
{
"active": false,
"name": "Test International Delegation",
"slug": "test-international-delegation",
},
],
},
},
}

stub_publishing_api_graphql_query(graphql_query, graphql_response)
end

it "has a success response" do
get :index
expect(response).to have_http_status(:success)
end
end
end
end

0 comments on commit 91069a7

Please sign in to comment.