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 1d713b1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/graphql/types/edition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -191,12 +191,16 @@ def change_history
Presenters::ChangeHistoryPresenter.new(object).change_history
end

def details
def details(lookahead:)
requested_details_fields = lookahead.selections.map(&:name) - [:change_history]
object_with_only_requested_details = object.dup
object_with_only_requested_details.details = object_with_only_requested_details.details.slice(*requested_details_fields)

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

0 comments on commit 1d713b1

Please sign in to comment.