Skip to content

Commit

Permalink
Only parse details fields that are requested
Browse files Browse the repository at this point in the history
At the moment, we are putting the entire contents of the `details` field
through the `DetailsPresenter`, which is sub-optimal as we are parsing
values that clients are not requesting.

By using lookaheads, we can determine which fields from `details` have
been requested and only parse those.

In a previous prototype, we had only transformed the govspeak in the
`body` field. However the schemas can permit mixed govspeak/HTML content
in any details field. We therefore need to parse all items within
`details`, as is already done using the `DetailsPresenter`.

Note: the code here could've been simpler (i.e. not duplicate the
object, just slice the details hash) but the `ContentEmbedPresenter`
requires a full `Edition` object.
  • Loading branch information
brucebolt committed Feb 11, 2025
1 parent bf30f11 commit e939446
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/graphql/types/edition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class WhipOrganisation < Types::BaseObject

field :body, String
field :brand, String
field :change_history, GraphQL::Types::JSON, extras: [:parent]
field :change_history, GraphQL::Types::JSON
field :current, Boolean
field :default_news_image, Image
field :display_date, GraphQL::Types::ISO8601DateTime
Expand All @@ -162,7 +162,7 @@ class WhipOrganisation < Types::BaseObject
field :content_id, ID
field :current, Boolean
field :description, String
field :details, Details
field :details, Details, extras: [:lookahead]
field :document_type, String
field :ended_on, GraphQL::Types::ISO8601DateTime
field :first_published_at, GraphQL::Types::ISO8601DateTime, null: false
Expand All @@ -187,16 +187,19 @@ class WhipOrganisation < Types::BaseObject
field :web_url, String
field :withdrawn_notice, WithdrawnNotice

def change_history
Presenters::ChangeHistoryPresenter.new(object).change_history
end
def details(lookahead:)
requested_details_fields = lookahead.selections.map(&:name)
object_with_only_requested_details = object.dup
object_with_only_requested_details.details = object_with_only_requested_details.details.slice(*requested_details_fields)

change_history_presenter = Presenters::ChangeHistoryPresenter.new(object_with_only_requested_details) if requested_details_fields.include?(:change_history)
content_embed_presenter = Presenters::ContentEmbedPresenter.new(object_with_only_requested_details)

def details
Presenters::ContentTypeResolver.new("text/html").resolve(
Presenters::DetailsPresenter.new(
object.details,
nil,
Presenters::ContentEmbedPresenter.new(object),
object_with_only_requested_details.details,
change_history_presenter,
content_embed_presenter,
).details,
)
end
Expand Down

0 comments on commit e939446

Please sign in to comment.