Skip to content

Commit

Permalink
fix #388,#373 add application/graphql+json and application/graphql-re…
Browse files Browse the repository at this point in the history
…sponse+json content type (#406)

Co-authored-by: Matt Labrum <[email protected]>
  • Loading branch information
mlabrum and mlabrum authored Nov 3, 2022
1 parent 33b8211 commit 1565d49
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ async function getResult(response: Dom.Response, jsonSerializer = defaultJsonSer
}
})

if (contentType && contentType.toLowerCase().startsWith('application/json')) {
if (contentType && (contentType.toLowerCase().startsWith('application/json') || contentType.toLowerCase().startsWith("application/graphql+json") || contentType.toLowerCase().startsWith("application/graphql-response+json"))) {
return jsonSerializer.parse(await response.text())
} else {
return response.text()
Expand Down
44 changes: 44 additions & 0 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,50 @@ test('minimal raw query with response headers', async () => {
expect(headers.get('X-Custom-Header')).toEqual(reqHeaders!['X-Custom-Header'])
})

test('minimal raw query with response headers and new graphql content type', async () => {
const { headers: reqHeaders, body } = ctx.res({
headers: {
'Content-Type': 'application/graphql+json',
},
body: {
data: {
me: {
id: 'some-id',
},
},
extensions: {
version: '1',
},
},
}).spec

const { headers, ...result } = await rawRequest(ctx.url, `{ me { id } }`)

expect(result).toEqual({ ...body, status: 200 })
})

test('minimal raw query with response headers and application/graphql-response+json response type', async () => {
const { headers: reqHeaders, body } = ctx.res({
headers: {
'Content-Type': 'application/graphql-response+json',
},
body: {
data: {
me: {
id: 'some-id',
},
},
extensions: {
version: '1',
},
},
}).spec

const { headers, ...result } = await rawRequest(ctx.url, `{ me { id } }`)

expect(result).toEqual({ ...body, status: 200 })
})

test('content-type with charset', async () => {
const { data } = ctx.res({
// headers: { 'Content-Type': 'application/json; charset=utf-8' },
Expand Down

0 comments on commit 1565d49

Please sign in to comment.