Skip to content

Commit

Permalink
Support 'application/graphql-response+json' (#1616)
Browse files Browse the repository at this point in the history
* Support 'application/graphql-response+json'

* Bring back the old behavior

* Fix the order

* Go
  • Loading branch information
ardatan committed Aug 18, 2022
1 parent 0591f1b commit 1d5cde9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-paws-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Support `application/graphql-response+json`
53 changes: 53 additions & 0 deletions packages/graphql-yoga/__tests__/accept-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,57 @@ describe('accept header', () => {
})
expect(response.status).toEqual(406)
})

it('server returns "application/graphql-response+json" content-type if accept header is "application/graphql-response+json"', async () => {
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
ping: String
}
`,
resolvers: {
Query: { ping: () => 'pong' },
},
}),
})

const response = await yoga.fetch(`http://yoga/graphql?query=query{ping}`, {
headers: {
accept: 'application/graphql-response+json',
},
})
expect(response.headers.get('content-type')).toEqual(
'application/graphql-response+json',
)
const result = await response.json()
expect(result).toEqual({ data: { ping: 'pong' } })
})

it('server returns "application/graphql-response+json" content-type if accept header includes both "application/graphql-response+json" and "application/json"', async () => {
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
ping: String
}
`,
resolvers: {
Query: { ping: () => 'pong' },
},
}),
})

const response = await yoga.fetch(`http://yoga/graphql?query=query{ping}`, {
headers: {
accept:
'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
},
})
expect(response.headers.get('content-type')).toEqual(
'application/graphql-response+json',
)
const result = await response.json()
expect(result).toEqual({ data: { ping: 'pong' } })
})
})
8 changes: 6 additions & 2 deletions packages/graphql-yoga/src/plugins/resultProcessor/regular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export function isRegularResult(
if (!isAsyncIterable(result)) {
const acceptHeader = request.headers.get('accept')
if (acceptHeader && !acceptHeader.includes('*/*')) {
if (acceptHeader.includes('application/json')) {
acceptHeaderByResult.set(result, 'application/json')
if (acceptHeader.includes('application/graphql-response+json')) {
acceptHeaderByResult.set(result, 'application/graphql-response+json')
return true
}
if (acceptHeader.includes('application/graphql+json')) {
acceptHeaderByResult.set(result, 'application/graphql+json')
return true
}
if (acceptHeader.includes('application/json')) {
acceptHeaderByResult.set(result, 'application/json')
return true
}
// If there is an accept header but this processer doesn't support, reject
return false
}
Expand Down

0 comments on commit 1d5cde9

Please sign in to comment.