diff --git a/app/controllers/content_items_controller.rb b/app/controllers/content_items_controller.rb index fce914e36..7f99e7d52 100644 --- a/app/controllers/content_items_controller.rb +++ b/app/controllers/content_items_controller.rb @@ -136,16 +136,64 @@ def set_guide_draft_access_token @content_item.draft_access_token = params[:token] end + def graphql_query(base_path) + <<-QUERY + { + edition(basePath: "#{base_path}", contentStore: "live") { + ... on NewsArticle { + basePath + title + schemaName + details + documentType + } + } + } + QUERY + end + + ## 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 + puts @http_response.inspect + 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 + def load_content_item - content_item = Services.content_store.content_item(content_item_path) + # # TODO: add the feature flag here + if params.include?(:graphql) + content_item = Services + .publishing_api + .graphql_query(graphql_query(content_item_path)) + # .dig("data", "edition") + # .deep_transform_keys(&:underscore) + + content_item.update_response_body + + puts content_item.inspect + + @content_item = 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"] + content_item["links"]["ordered_related_items"] = ordered_related_items(content_item["links"]) if content_item["links"] - @content_item = PresenterBuilder.new( - content_item, - content_item_path, - view_context, - ).presenter + @content_item = PresenterBuilder.new( + content_item, + content_item_path, + view_context, + ).presenter + end end def ordered_related_items(links)