Skip to content

Commit 1d5cde9

Browse files
authored
Support 'application/graphql-response+json' (#1616)
* Support 'application/graphql-response+json' * Bring back the old behavior * Fix the order * Go
1 parent 0591f1b commit 1d5cde9

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

.changeset/tall-paws-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-yoga': patch
3+
---
4+
5+
Support `application/graphql-response+json`

packages/graphql-yoga/__tests__/accept-header.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,57 @@ describe('accept header', () => {
204204
})
205205
expect(response.status).toEqual(406)
206206
})
207+
208+
it('server returns "application/graphql-response+json" content-type if accept header is "application/graphql-response+json"', async () => {
209+
const yoga = createYoga({
210+
schema: createSchema({
211+
typeDefs: /* GraphQL */ `
212+
type Query {
213+
ping: String
214+
}
215+
`,
216+
resolvers: {
217+
Query: { ping: () => 'pong' },
218+
},
219+
}),
220+
})
221+
222+
const response = await yoga.fetch(`http://yoga/graphql?query=query{ping}`, {
223+
headers: {
224+
accept: 'application/graphql-response+json',
225+
},
226+
})
227+
expect(response.headers.get('content-type')).toEqual(
228+
'application/graphql-response+json',
229+
)
230+
const result = await response.json()
231+
expect(result).toEqual({ data: { ping: 'pong' } })
232+
})
233+
234+
it('server returns "application/graphql-response+json" content-type if accept header includes both "application/graphql-response+json" and "application/json"', async () => {
235+
const yoga = createYoga({
236+
schema: createSchema({
237+
typeDefs: /* GraphQL */ `
238+
type Query {
239+
ping: String
240+
}
241+
`,
242+
resolvers: {
243+
Query: { ping: () => 'pong' },
244+
},
245+
}),
246+
})
247+
248+
const response = await yoga.fetch(`http://yoga/graphql?query=query{ping}`, {
249+
headers: {
250+
accept:
251+
'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
252+
},
253+
})
254+
expect(response.headers.get('content-type')).toEqual(
255+
'application/graphql-response+json',
256+
)
257+
const result = await response.json()
258+
expect(result).toEqual({ data: { ping: 'pong' } })
259+
})
207260
})

packages/graphql-yoga/src/plugins/resultProcessor/regular.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ export function isRegularResult(
1212
if (!isAsyncIterable(result)) {
1313
const acceptHeader = request.headers.get('accept')
1414
if (acceptHeader && !acceptHeader.includes('*/*')) {
15-
if (acceptHeader.includes('application/json')) {
16-
acceptHeaderByResult.set(result, 'application/json')
15+
if (acceptHeader.includes('application/graphql-response+json')) {
16+
acceptHeaderByResult.set(result, 'application/graphql-response+json')
1717
return true
1818
}
1919
if (acceptHeader.includes('application/graphql+json')) {
2020
acceptHeaderByResult.set(result, 'application/graphql+json')
2121
return true
2222
}
23+
if (acceptHeader.includes('application/json')) {
24+
acceptHeaderByResult.set(result, 'application/json')
25+
return true
26+
}
2327
// If there is an accept header but this processer doesn't support, reject
2428
return false
2529
}

0 commit comments

Comments
 (0)