diff --git a/app/controllers/content_items_controller.rb b/app/controllers/content_items_controller.rb index fce914e36..ad65883c5 100644 --- a/app/controllers/content_items_controller.rb +++ b/app/controllers/content_items_controller.rb @@ -136,16 +136,43 @@ 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) - - content_item["links"]["ordered_related_items"] = ordered_related_items(content_item["links"]) if content_item["links"] + 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: how do we know this is a news article before we request the item from content store? + ## i.e., how do we know which GraphQL query to use? + @content_item = if Features.graphql_feature_enabled? || params.include?(:graphql) + graphql_response = Services + .publishing_api + .graphql_query(Graphql::NewsArticleQuery.new(content_item_path).query) + + graphql_response.update_response_body + + PresenterBuilder.new( + graphql_response, + 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) diff --git a/app/queries/graphql/news_article_query.rb b/app/queries/graphql/news_article_query.rb new file mode 100644 index 000000000..ba1beb51e --- /dev/null +++ b/app/queries/graphql/news_article_query.rb @@ -0,0 +1,75 @@ +class Graphql::NewsArticleQuery + def initialize(base_path) + @base_path = base_path + end + + def query + <<-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 + links { + parentTaxons { + basePath + contentId + documentType + phase + title + } + } + } + topicalEvents { + basePath + contentId + title + } + worldLocations { + basePath + contentId + title + } + } + locale + schemaName + title + } + } + } + QUERY + end +end