Skip to content

Commit

Permalink
Use GraphQL for rendering news articles
Browse files Browse the repository at this point in the history
This retrieves the content from Publishing API, instead of Content
Store, using a GraphQL query.

Note this has been done in the way that reduces the number of changes
needed in this application. As a result, we have introduced a small
"hack" to return the data in `GdsApi::Response` format but with this app
rewriting the hash keys to make them all underscore based.
  • Loading branch information
brucebolt committed Dec 2, 2024
1 parent 3f8607c commit 5983a10
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
45 changes: 36 additions & 9 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions app/queries/graphql/news_article_query.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5983a10

Please sign in to comment.