From 1d5cde96ce5b7647de7d329f9f56e398463a9152 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 18 Aug 2022 15:34:08 +0300 Subject: [PATCH] Support 'application/graphql-response+json' (#1616) * Support 'application/graphql-response+json' * Bring back the old behavior * Fix the order * Go --- .changeset/tall-paws-do.md | 5 ++ .../__tests__/accept-header.spec.ts | 53 +++++++++++++++++++ .../src/plugins/resultProcessor/regular.ts | 8 ++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .changeset/tall-paws-do.md diff --git a/.changeset/tall-paws-do.md b/.changeset/tall-paws-do.md new file mode 100644 index 0000000000..d2a89da54c --- /dev/null +++ b/.changeset/tall-paws-do.md @@ -0,0 +1,5 @@ +--- +'graphql-yoga': patch +--- + +Support `application/graphql-response+json` diff --git a/packages/graphql-yoga/__tests__/accept-header.spec.ts b/packages/graphql-yoga/__tests__/accept-header.spec.ts index 7e0048e521..22a82288d1 100644 --- a/packages/graphql-yoga/__tests__/accept-header.spec.ts +++ b/packages/graphql-yoga/__tests__/accept-header.spec.ts @@ -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' } }) + }) }) diff --git a/packages/graphql-yoga/src/plugins/resultProcessor/regular.ts b/packages/graphql-yoga/src/plugins/resultProcessor/regular.ts index 5242a84539..ce09a782b4 100644 --- a/packages/graphql-yoga/src/plugins/resultProcessor/regular.ts +++ b/packages/graphql-yoga/src/plugins/resultProcessor/regular.ts @@ -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 }