Skip to content

Commit 1a06e01

Browse files
yndajasGweaton
andcommitted
Implement GraphQL Edition type withdrawn_notice
Currently content items for non-withdrawn editions will include an empty object for the `withdrawn_notice`. We opted to return `nil` instead, which feels more conventional (for APIs in general) and is also the [recommended way of doing things in GraphQL][1]. Implementing an empty object in GraphQL requires some fairly [hacky code][2]. We'll need to ensure that consumers can handle this field being `nil` before any move to use the GraphQL endpoint in production [1]: https://spec.graphql.org/draft/#sel-HAHZhCFBABPBT5pS [2]: graphql/graphql-spec#568 (comment) Co-authored-by: George <[email protected]>
1 parent 858148e commit 1a06e01

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

app/graphql/types/edition_type.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
module Types
44
class EditionType < Types::BaseObject
5+
class WithdrawnNotice < Types::BaseObject
6+
field :explanation, String
7+
field :withdrawn_at, GraphQL::Types::ISO8601DateTime
8+
end
9+
510
field :analytics_identifier, String
611
field :base_path, String
712
field :content_id, ID
@@ -23,7 +28,16 @@ class EditionType < Types::BaseObject
2328
field :schema_name, String
2429
field :title, String, null: false
2530
field :updated_at, GraphQL::Types::ISO8601DateTime
26-
field :withdrawn_notice, String
31+
field :withdrawn_notice, WithdrawnNotice
32+
33+
def withdrawn_notice
34+
return nil unless object.unpublishing&.withdrawal?
35+
36+
Presenters::EditionPresenter
37+
.new(object)
38+
.present
39+
.fetch(:withdrawn_notice)
40+
end
2741

2842
# Aliased by field methods for fields that are currently presented in the
2943
# content item, but are not available via the Publishing API.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
RSpec.describe "Types::EditionType" do
2+
include GraphQL::Testing::Helpers.for(PublishingApiSchema)
3+
4+
describe "#withdrawn_notice" do
5+
context "when the edition is withdrawn" do
6+
it "returns a withdrawal notice" do
7+
edition = create(:withdrawn_unpublished_edition, explanation: "for testing", unpublished_at: "2024-10-28 17:00:00.000000000 +0000")
8+
expected = {
9+
explanation: "for testing",
10+
withdrawn_at: "2024-10-28T17:00:00Z",
11+
}
12+
13+
expect(
14+
run_graphql_field(
15+
"Edition.withdrawnNotice",
16+
edition,
17+
),
18+
).to eq(expected)
19+
end
20+
end
21+
22+
context "when the edition is not withdrawn" do
23+
it "returns nil" do
24+
expect(
25+
run_graphql_field(
26+
"Edition.withdrawnNotice",
27+
create(:edition),
28+
),
29+
).to be_nil
30+
end
31+
end
32+
end
33+
end

spec/integration/graphql_spec.rb

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@
6161
"schemaName": @edition.schema_name,
6262
"title": @edition.title,
6363
"updatedAt": @edition.updated_at.iso8601,
64-
"withdrawnNotice": {
65-
"explanation": nil,
66-
"withdrawnAt": nil,
67-
},
64+
"withdrawnNotice": nil,
6865
},
6966
},
7067
}
@@ -74,6 +71,48 @@
7471
expect(parsed_response).to eq(expected)
7572
end
7673

74+
context "when there is a withdrawn notice" do
75+
before do
76+
create(
77+
:withdrawn_unpublished_edition,
78+
base_path: "/my/withdrawn/edition",
79+
explanation: "for integration testing",
80+
unpublished_at: "2024-10-27 17:00:00.000000000 +0000",
81+
)
82+
end
83+
84+
it "populates the withdrawn notice" do
85+
post "/graphql", params: {
86+
query:
87+
"{
88+
edition(basePath: \"/my/withdrawn/edition\") {
89+
... on Edition {
90+
withdrawnNotice {
91+
explanation
92+
withdrawnAt
93+
}
94+
}
95+
}
96+
}",
97+
}
98+
99+
expected = {
100+
"data": {
101+
"edition": {
102+
"withdrawnNotice": {
103+
"explanation": "for integration testing",
104+
"withdrawnAt": "2024-10-27T17:00:00Z",
105+
},
106+
},
107+
},
108+
}
109+
110+
parsed_response = JSON.parse(response.body).deep_symbolize_keys
111+
112+
expect(parsed_response).to eq(expected)
113+
end
114+
end
115+
77116
it "does not expose non-generic edition fields" do
78117
post "/graphql", params: {
79118
query:

0 commit comments

Comments
 (0)