From c1fca800d948919cf286fcbfd6626bdb212386b9 Mon Sep 17 00:00:00 2001 From: Denny Tsai Date: Thu, 19 Apr 2018 17:51:33 +0900 Subject: [PATCH 1/2] Add the headers object to the rawRequest's result --- src/index.ts | 12 ++++++------ tests/index.test.ts | 13 ++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index da4f56c1d..43d295079 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { ClientError, GraphQLError, Headers, Options, Variables } from './types' +import { ClientError, GraphQLError, Headers as HttpHeaders, Options, Variables } from './types' export { ClientError } from './types' import 'cross-fetch/polyfill' @@ -14,7 +14,7 @@ export class GraphQLClient { async rawRequest( query: string, variables?: Variables, - ): Promise<{ data?: T, extensions?: any, errors?: GraphQLError[] }> { + ): Promise<{ data?: T, extensions?: any, headers: Headers, errors?: GraphQLError[] }> { const { headers, ...others } = this.options const body = JSON.stringify({ @@ -32,12 +32,12 @@ export class GraphQLClient { const result = await getResult(response) if (response.ok && !result.errors && result.data) { - return result + return { ...result, headers: response.headers } } else { const errorResult = typeof result === 'string' ? { error: result } : result throw new ClientError( - { ...errorResult, status: response.status }, + { ...errorResult, status: response.status, headers: response.headers }, { query, variables }, ) } @@ -75,7 +75,7 @@ export class GraphQLClient { } } - setHeaders(headers: Headers): GraphQLClient { + setHeaders(headers: HttpHeaders): GraphQLClient { this.options.headers = headers return this @@ -97,7 +97,7 @@ export async function rawRequest( url: string, query: string, variables?: Variables, -): Promise<{ data?: T, extensions?: any, errors?: GraphQLError[] }> { +): Promise<{ data?: T, extensions?: any, headers: Headers, errors?: GraphQLError[] }> { const client = new GraphQLClient(url) return client.rawRequest(query, variables) diff --git a/tests/index.test.ts b/tests/index.test.ts index a80629534..fe719429f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,7 +15,7 @@ test('minimal query', async (t) => { }) }) -test('minimal raw query', async (t) => { +test('minimal raw query with response headers', async (t) => { const data = { viewer: { id: 'some-id', @@ -26,8 +26,15 @@ test('minimal raw query', async (t) => { version: '1', } - await mock({body: {data, extensions}}, async () => { - t.deepEqual(await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`), {data, extensions}) + const reqHeaders = { + 'Content-Type': 'application/json', + 'X-Custom-Header': 'test-custom-header', + } + + await mock({headers: reqHeaders, body: {data, extensions}}, async () => { + const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`) + t.deepEqual(result, {data, extensions}) + t.deepEqual(headers.get('X-Custom-Header'), reqHeaders['X-Custom-Header']) }) }) From 1f8d7fb79fe617d33adc5dbb93ed755e15d99ea3 Mon Sep 17 00:00:00 2001 From: Denny Tsai Date: Thu, 19 Apr 2018 18:10:54 +0900 Subject: [PATCH 2/2] Add back minimal raw query test with a slight modification to ignore headers object --- tests/index.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/index.test.ts b/tests/index.test.ts index fe719429f..7c8c3c490 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,6 +15,23 @@ test('minimal query', async (t) => { }) }) +test('minimal raw query', async (t) => { + const data = { + viewer: { + id: 'some-id', + }, + } + + const extensions = { + version: '1', + } + + await mock({body: {data, extensions}}, async () => { + const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`) + t.deepEqual(result, {data, extensions}) + }) +}) + test('minimal raw query with response headers', async (t) => { const data = { viewer: {