Skip to content

Commit

Permalink
Properly support multiple content types in details fields
Browse files Browse the repository at this point in the history
According to ADR-004, it is possible to represent content in an array
format containing multiple types. This can include already parsed
govspeak in HTML format or only govspeak. In the latter case, Publishing
API parses this and converts to HTML before sending the content to
Content Store.

This replicates that behaviour for GraphQL responses, expanding this to
all fields within `details`, not just the body.

It also has the side effect of supporting embeddded content and full
change history in GraphQL responses.
  • Loading branch information
brucebolt committed Jan 2, 2025
1 parent cd6a41f commit 4cde8c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
21 changes: 10 additions & 11 deletions app/graphql/types/edition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,6 @@ class WhipOrganisation < Types::BaseObject
field :supports_historical_accounts, Boolean
field :whip_organisation, WhipOrganisation
field :world_locations, [EditionType], null: false

def body
return object[:body] if object[:body].is_a?(String)

govspeak = object.fetch(:body, [])
.filter { _1[:content_type] == "text/govspeak" }
.map { _1[:content] }
.first

Govspeak::Document.new(govspeak).to_html if govspeak.present?
end
end

field :active, Boolean, null: false
Expand Down Expand Up @@ -197,6 +186,16 @@ def body
field :web_url, String
field :withdrawn_notice, WithdrawnNotice

def details
Presenters::ContentTypeResolver.new("text/html").resolve(
Presenters::DetailsPresenter.new(
object.to_h[:details],
Presenters::ChangeHistoryPresenter.new(object),
Presenters::ContentEmbedPresenter.new(object),
).details,
)
end

def withdrawn_notice
return nil unless object.unpublishing&.withdrawal?

Expand Down
50 changes: 50 additions & 0 deletions spec/graphql/types/edition_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,54 @@
end
end
end

context "content types in the details" do
context "when the body is a string" do
it "returns the string" do
edition = create(:edition, details: { body: "some text" })

expect(
run_graphql_field(
"Edition.details",
edition,
)[:body],
).to eq("some text")
end
end

context "when there are multiple content types and one is html" do
it "returns the html" do
edition = create(:edition, details: {
body: [
{ content_type: "text/govspeak", content: "some text" },
{ content_type: "text/html", content: "<p>some other text</p>" },
],
})

expect(
run_graphql_field(
"Edition.details",
edition,
)[:body],
).to eq("<p>some other text</p>")
end
end

context "when there are multiple content types and none are html" do
it "converts the govspeak to html" do
edition = create(:edition, details: {
body: [
{ content_type: "text/govspeak", content: "some text" },
],
})

expect(
run_graphql_field(
"Edition.details",
edition,
)[:body],
).to eq("<p>some text</p>\n")
end
end
end
end

0 comments on commit 4cde8c1

Please sign in to comment.