Skip to content

Commit

Permalink
WIP: get this app using GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
brucebolt committed Nov 29, 2024
1 parent dde0c86 commit 9f0abcb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem "rails", "7.2.2"
gem "bootsnap", require: false
gem "dalli"
gem "dartsass-rails"
gem "gds-api-adapters"
gem "gds-api-adapters", path: "../gds-api-adapters"
gem "govspeak"
gem "govuk_ab_testing"
gem "govuk_app_config"
Expand Down
20 changes: 12 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
PATH
remote: ../gds-api-adapters
specs:
gds-api-adapters (97.4.1)
addressable
link_header
null_logger
plek (>= 1.9.0)
rack (>= 2.2.0)
rest-client (~> 2.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -123,13 +134,6 @@ GEM
execjs (2.9.1)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
gds-api-adapters (97.4.0)
addressable
link_header
null_logger
plek (>= 1.9.0)
rack (>= 2.2.0)
rest-client (~> 2.0)
globalid (1.2.1)
activesupport (>= 6.1)
google-protobuf (4.28.3)
Expand Down Expand Up @@ -658,7 +662,7 @@ DEPENDENCIES
dalli
dartsass-rails
faker
gds-api-adapters
gds-api-adapters!
govspeak
govuk_ab_testing
govuk_app_config
Expand Down
104 changes: 96 additions & 8 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,104 @@ def set_guide_draft_access_token
@content_item.draft_access_token = params[:token]
end

def load_content_item
content_item = Services.content_store.content_item(content_item_path)
def graphql_query(base_path)
<<-QUERY
{
edition(
basePath: "#{base_path}",
contentStore: "live",
) {
... on NewsArticle {
basePath
description
details
documentType
firstPublishedAt
links {
availableTranslations {
basePath
locale
}
government {
details {
current
}
title
}
organisations {
basePath
contentId
title
}
people {
basePath
contentId
title
}
taxons {
basePath
contentId
documentType
phase
title
}
topicalEvents {
basePath
contentId
title
}
worldLocations {
basePath
contentId
title
}
}
locale
schemaName
title
}
}
}
QUERY
end

content_item["links"]["ordered_related_items"] = ordered_related_items(content_item["links"]) if content_item["links"]
## This is a hack to stop us needing to rewrite most of this application to support the format for GraphQL responses
class GdsApi::Response
def update_response_body
updated_response = JSON.parse(@http_response.body).dig("data", "edition").deep_transform_keys(&:underscore)
@http_response = RestClient::Response.create(
updated_response.to_json,
@http_response.net_http_res,
@http_response.request,
)
end
end

@content_item = PresenterBuilder.new(
content_item,
content_item_path,
view_context,
).presenter
def load_content_item
# # TODO: add the feature flag here
@content_item = if params.include?(:graphql)
content_item = Services
.publishing_api
.graphql_query(graphql_query(content_item_path))

content_item.update_response_body

PresenterBuilder.new(
content_item,
content_item_path,
view_context,
).presenter
else
content_item = Services.content_store.content_item(content_item_path)

content_item["links"]["ordered_related_items"] = ordered_related_items(content_item["links"]) if content_item["links"]

PresenterBuilder.new(
content_item,
content_item_path,
view_context,
).presenter
end
end

def ordered_related_items(links)
Expand Down

0 comments on commit 9f0abcb

Please sign in to comment.