-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use GraphQL for rendering World Index page
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
Showing
5 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |