diff --git a/app/controllers/ministers_controller.rb b/app/controllers/ministers_controller.rb index 40cf4ab27..aa6a84911 100644 --- a/app/controllers/ministers_controller.rb +++ b/app/controllers/ministers_controller.rb @@ -2,15 +2,25 @@ class MinistersController < ApplicationController around_action :switch_locale def index - if 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 + content_item_data = if params[:graphql] == "false" + load_from_content_store + elsif params[:graphql] == "true" || Features.graphql_feature_enabled? + load_from_graphql + else + load_from_content_store + end @presented_ministers = MinistersIndexPresenter.new(content_item_data) - setup_content_item_and_navigation_helpers(ministers_index) + setup_content_item_and_navigation_helpers(@ministers_index) + end + + def load_from_graphql + @ministers_index = Graphql::MinistersIndex.find!(request.path) + @ministers_index.content_item + end + + def load_from_content_store + @ministers_index = MinistersIndex.find!(request.path) + @ministers_index.content_item.content_item_data end end diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 8ca97a6ed..656c5d744 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -2,15 +2,25 @@ class RolesController < ApplicationController around_action :switch_locale def show - if Features.graphql_feature_enabled? || params.include?(:graphql) - @role = Graphql::Role.find!(request.path) - content_item_data = @role.content_item - else - @role = Role.find!(request.path) - content_item_data = @role.content_item.content_item_data - end + content_item_data = if params[:graphql] == "false" + load_from_content_store + elsif params[:graphql] == "true" || Features.graphql_feature_enabled? + load_from_graphql + else + load_from_content_store + end setup_content_item_and_navigation_helpers(@role) render :show, locals: { role: RolePresenter.new(content_item_data) } end + + def load_from_graphql + @role = Graphql::Role.find!(request.path) + @role.content_item + end + + def load_from_content_store + @role = Role.find!(request.path) + @role.content_item.content_item_data + end end diff --git a/app/controllers/world_controller.rb b/app/controllers/world_controller.rb index be073ee59..16227c52e 100644 --- a/app/controllers/world_controller.rb +++ b/app/controllers/world_controller.rb @@ -1,14 +1,24 @@ class WorldController < ApplicationController def index - if Features.graphql_feature_enabled? || params.include?(:graphql) - world_index = Graphql::WorldIndex.find!(request.path) - content_item_data = world_index.content_item - else - world_index = WorldIndex.find!(request.path) - content_item_data = world_index.content_item.content_item_data - end + content_item_data = if params[:graphql] == "false" + load_from_content_store + elsif params[:graphql] == "true" || Features.graphql_feature_enabled? + load_from_graphql + else + load_from_content_store + end @presented_index = WorldIndexPresenter.new(content_item_data) - setup_content_item_and_navigation_helpers(world_index) + setup_content_item_and_navigation_helpers(@world_index) + end + + def load_from_graphql + @world_index = Graphql::WorldIndex.find!(request.path) + @world_index.content_item + end + + def load_from_content_store + @world_index = WorldIndex.find!(request.path) + @world_index.content_item.content_item_data end end diff --git a/spec/features/ministers_spec.rb b/spec/features/ministers_spec.rb index 121fb9dbb..650e0a364 100644 --- a/spec/features/ministers_spec.rb +++ b/spec/features/ministers_spec.rb @@ -123,52 +123,124 @@ end end - context "without the graphql feature flag" do + context "without the GraphQL feature flag" do let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-off") } before do stub_content_store_has_item("/government/ministers", document) - visit "/government/ministers" end - it_behaves_like "ministers index page" + context "when the GraphQL parameter is not set" do + before do + visit "/government/ministers" + end + + it_behaves_like "ministers index page" + + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end + + context "during a reshuffle" do + let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-on") } + + it_behaves_like "ministers index page during a reshuffle" + end + + context "during a reshuffle preview" do + let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-on-preview") } + + it_behaves_like "ministers index page during a reshuffle preview" + end + end + + context "when the GraphQL parameter is true" do + before do + stub_publishing_api_graphql_query( + Graphql::MinistersIndexQuery.new("/government/ministers").query, + document, + ) + + visit "/government/ministers?graphql=true" + end - context "during a reshuffle" do - let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-on") } + let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-off") } - it_behaves_like "ministers index page during a reshuffle" + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end end - context "during a reshuffle preview" do - let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-on-preview") } + context "when the GraphQL parameter is false" do + before do + visit "/government/ministers?graphql=false" + end - it_behaves_like "ministers index page during a reshuffle preview" + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end end end - context "with the GraphQL query param set" do + context "with the GraphQL feature flag" do let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-off") } before do + enable_graphql_feature_flag + stub_publishing_api_graphql_query( Graphql::MinistersIndexQuery.new("/government/ministers").query, document, ) - visit "/government/ministers?graphql=true" end - it_behaves_like "ministers index page" + context "when the GraphQL parameter is not set" do + before do + visit "/government/ministers" + end + + it_behaves_like "ministers index page" - context "during a reshuffle" do - let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-on") } + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end - it_behaves_like "ministers index page during a reshuffle" + context "during a reshuffle" do + let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-on") } + + it_behaves_like "ministers index page during a reshuffle" + end + + context "during a reshuffle preview" do + let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-on-preview") } + + it_behaves_like "ministers index page during a reshuffle preview" + end end - context "during a reshuffle preview" do - let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-on-preview") } + context "when the GraphQL parameter is true" do + before do + visit "/government/ministers?graphql=true" + end - it_behaves_like "ministers index page during a reshuffle preview" + let(:document) { fetch_graphql_fixture("ministers_index-reshuffle-mode-off") } + + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end + end + + context "when the GraphQL parameter is false" do + let(:document) { GovukSchemas::Example.find("ministers_index", example_name: "ministers_index-reshuffle-mode-off") } + + before do + stub_content_store_has_item("/government/ministers", document) + visit "/government/ministers?graphql=false" + end + + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end end end end diff --git a/spec/features/role_spec.rb b/spec/features/role_spec.rb index 57efdb8ab..c4008ed2b 100644 --- a/spec/features/role_spec.rb +++ b/spec/features/role_spec.rb @@ -11,79 +11,118 @@ role_edition_data, ) stub_search(body: { results: [] }) - - visit "/government/ministers/prime-minister" end let(:role_edition_data) do fetch_graphql_fixture("prime_minister") end - it "renders the page successfully" do - expect(page).to have_selector("h1", text: "Prime Minister") - end + context "when there is no GraphQL parameter" do + before do + visit "/government/ministers/prime-minister" + end - context "when there's a current role holder" do - let(:role_edition_data) do - fetch_graphql_fixture("prime_minister") - .tap do |fixture| - any_current = fixture["data"]["edition"]["links"]["role_appointments"] - .any? { |ra| ra["details"]["current"] == true } + it "renders the page successfully" do + expect(page).to have_selector("h1", text: "Prime Minister") + end - unless any_current - links = fixture["data"]["edition"]["links"] - links["role_appointments"][0]["details"]["current"] = true - end - end + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + context "when there's a current role holder" do + let(:role_edition_data) do + fetch_graphql_fixture("prime_minister") + .tap do |fixture| + any_current = fixture["data"]["edition"]["links"]["role_appointments"] + .any? { |ra| ra["details"]["current"] == true } + + unless any_current + links = fixture["data"]["edition"]["links"] + links["role_appointments"][0]["details"]["current"] = true + end + end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end - end - context "when there isn't a current role holder" do - let(:role_edition_data) do - fetch_graphql_fixture("prime_minister") - .tap do |fixture| - current = fixture["data"]["edition"]["links"]["role_appointments"] - .find { |ra| ra["details"]["current"] == true } + context "when there isn't a current role holder" do + let(:role_edition_data) do + fetch_graphql_fixture("prime_minister") + .tap do |fixture| + current = fixture["data"]["edition"]["links"]["role_appointments"] + .find { |ra| ra["details"]["current"] == true } - if current - current["details"]["current"] = false - current["details"]["ended_on"] = Time.zone.now + if current + current["details"]["current"] = false + current["details"]["ended_on"] = Time.zone.now + end end - end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + context "when the role 'supports historical accounts'" do + let(:role_edition_data) do + fetch_graphql_fixture("prime_minister") + .tap do |fixture| + fixture["data"]["edition"]["details"]["supports_historical_accounts"] = true + end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end + end + + context "when the role doesn't 'support historical accounts'" do + let(:role_edition_data) do + fetch_graphql_fixture("prime_minister") + .tap do |fixture| + fixture["data"]["edition"]["details"]["supports_historical_accounts"] = false + end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end end - context "when the role 'supports historical accounts'" do - let(:role_edition_data) do - fetch_graphql_fixture("prime_minister") - .tap do |fixture| - fixture["data"]["edition"]["details"]["supports_historical_accounts"] = true - end + context "when the GraphQL parameter is true" do + before do + visit "/government/ministers/prime-minister?graphql=true" end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made end end - context "when the role doesn't 'support historical accounts'" do - let(:role_edition_data) do - fetch_graphql_fixture("prime_minister") - .tap do |fixture| - fixture["data"]["edition"]["details"]["supports_historical_accounts"] = false - end + context "when the GraphQL parameter is false" do + before do + stub_content_store_has_item( + "/government/ministers/prime-minister", + role_content_item_data, + ) + stub_search(body: { results: [] }) + + visit "/government/ministers/prime-minister?graphql=false" end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + let(:role_content_item_data) do + GovukSchemas::Example.find("role", example_name: "prime_minister") + end + + it "does not get data from GraphQL" do + puts Plek.find("content-store") + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made end end end @@ -103,70 +142,109 @@ GovukSchemas::Example.find("role", example_name: "prime_minister") end - it "renders the page successfully" do - expect(page).to have_selector("h1", text: "Prime Minister") - end + context "when there is no GraphQL parameter" do + it "renders the page successfully" do + expect(page).to have_selector("h1", text: "Prime Minister") + end - context "when there's a current role holder" do - let(:role_content_item_data) do - GovukSchemas::Example.find("role", example_name: "prime_minister") - .tap do |example| - any_current = example["links"]["role_appointments"] - .any? { |ra| ra["details"]["current"] == true } + it "does not get data from GraphQL" do + puts Plek.find("content-store") + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end + + context "when there's a current role holder" do + let(:role_content_item_data) do + GovukSchemas::Example.find("role", example_name: "prime_minister") + .tap do |example| + any_current = example["links"]["role_appointments"] + .any? { |ra| ra["details"]["current"] == true } - unless any_current - example["links"]["role_appointments"][0]["details"]["current"] = true + unless any_current + example["links"]["role_appointments"][0]["details"]["current"] = true + end end - end - end + end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end - end - context "when there isn't a current role holder" do - let(:role_content_item_data) do - GovukSchemas::Example.find("role", example_name: "prime_minister") - .tap do |example| - current = example["links"]["role_appointments"] - .find { |ra| ra["details"]["current"] == true } + context "when there isn't a current role holder" do + let(:role_content_item_data) do + GovukSchemas::Example.find("role", example_name: "prime_minister") + .tap do |example| + current = example["links"]["role_appointments"] + .find { |ra| ra["details"]["current"] == true } - if current - current["details"]["current"] = false - current["details"]["ended_on"] = Time.zone.now + if current + current["details"]["current"] = false + current["details"]["ended_on"] = Time.zone.now + end end - end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + context "when the role 'supports historical accounts'" do + let(:role_content_item_data) do + GovukSchemas::Example.find("role", example_name: "prime_minister") + .tap do |example| + example["details"]["supports_historical_accounts"] = true + end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end + end + + context "when the role doesn't 'support historical accounts'" do + let(:role_content_item_data) do + GovukSchemas::Example.find("role", example_name: "prime_minister") + .tap do |example| + example["details"]["supports_historical_accounts"] = false + end + end + + it "renders the page successfully" do + expect(page.status_code).to eq(200) + end end end - context "when the role 'supports historical accounts'" do - let(:role_content_item_data) do - GovukSchemas::Example.find("role", example_name: "prime_minister") - .tap do |example| - example["details"]["supports_historical_accounts"] = true - end + context "when the GraphQL parameter is true" do + before do + enable_graphql_feature_flag + stub_publishing_api_graphql_query( + Graphql::RoleQuery.new("/government/ministers/prime-minister").query, + role_edition_data, + ) + stub_search(body: { results: [] }) + + visit "/government/ministers/prime-minister?graphql=true" end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + let(:role_edition_data) do + fetch_graphql_fixture("prime_minister") + end + + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made end end - context "when the role doesn't 'support historical accounts'" do - let(:role_content_item_data) do - GovukSchemas::Example.find("role", example_name: "prime_minister") - .tap do |example| - example["details"]["supports_historical_accounts"] = false - end + context "when the GraphQL parameter is false" do + before do + visit "/government/ministers/prime-minister?graphql=false" end - it "renders the page successfully" do - expect(page.status_code).to eq(200) + it "does not get data from GraphQL" do + puts Plek.find("content-store") + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made end end end diff --git a/spec/features/world_index_spec.rb b/spec/features/world_index_spec.rb index b1713a5d6..59c5f69b9 100644 --- a/spec/features/world_index_spec.rb +++ b/spec/features/world_index_spec.rb @@ -74,34 +74,87 @@ context "without the GraphQL feature flag" do before do stub_content_store_has_item("/world", GovukSchemas::Example.find("world_index", example_name: "world_index")) - visit "/world" end - it_behaves_like "world index page" + context "when the GraphQL parameter is not set" do + before do + visit "/world" + end + + it_behaves_like "world index page" + + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end + end + + context "when the GraphQL parameter is true" do + before do + stub_publishing_api_graphql_query( + Graphql::WorldIndexQuery.new("/world").query, + fetch_graphql_fixture("world_index"), + ) + + visit "/world?graphql=true" + end + + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end + end + + context "when the GraphQL parameter is false" do + before do + visit "/world?graphql=false" + end + + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end + end end context "with the GraphQL feature flag" do before do enable_graphql_feature_flag + stub_publishing_api_graphql_query( Graphql::WorldIndexQuery.new("/world").query, fetch_graphql_fixture("world_index"), ) - visit "/world" end - it_behaves_like "world index page" - end + context "when the GraphQL parameter is not set" do + before do + visit "/world" + end - context "with the GraphQL query string" do - before do - stub_publishing_api_graphql_query( - Graphql::WorldIndexQuery.new("/world").query, - fetch_graphql_fixture("world_index"), - ) - visit "/world?graphql=true" + it_behaves_like "world index page" + + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end end - it_behaves_like "world index page" + context "when the GraphQL parameter is true" do + before do + visit "/world?graphql=true" + end + + it "gets the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).to have_been_made + end + end + + context "when the GraphQL parameter is false" do + before do + stub_content_store_has_item("/world", GovukSchemas::Example.find("world_index", example_name: "world_index")) + visit "/world?graphql=false" + end + + it "does not get the data from GraphQL" do + expect(a_request(:post, "#{Plek.find('publishing-api')}/graphql")).not_to have_been_made + end + end end end