From 245d208269bb3ba0761dfc5ea4ca1139cb22c030 Mon Sep 17 00:00:00 2001 From: Emily Harber Date: Mon, 8 Feb 2021 16:25:57 -0500 Subject: [PATCH 1/2] Handle data objects with variables in GraphQL Client --- src/clients/graphql/graphql_client.ts | 10 ++++- .../graphql/test/graphql_client.test.ts | 44 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/clients/graphql/graphql_client.ts b/src/clients/graphql/graphql_client.ts index b6c6d9e92..e079247bb 100644 --- a/src/clients/graphql/graphql_client.ts +++ b/src/clients/graphql/graphql_client.ts @@ -16,6 +16,14 @@ export class GraphqlClient { params.extraHeaders = {[ShopifyHeader.AccessToken]: this.token, ...params.extraHeaders}; const path = `/admin/api/${Context.API_VERSION}/graphql.json`; - return this.client.post({path, type: DataType.GraphQL, ...params}); + let dataType: DataType.GraphQL | DataType.JSON; + + if (typeof params.data === 'object') { + dataType = DataType.JSON; + } else { + dataType = DataType.GraphQL; + } + + return this.client.post({path, type: dataType, ...params}); } } diff --git a/src/clients/graphql/test/graphql_client.test.ts b/src/clients/graphql/test/graphql_client.test.ts index bf41de46b..b7b0842a2 100644 --- a/src/clients/graphql/test/graphql_client.test.ts +++ b/src/clients/graphql/test/graphql_client.test.ts @@ -37,11 +37,53 @@ describe('GraphQL client', () => { fetchMock.mockResponseOnce(JSON.stringify(successResponse)); - await expect(client.query({extraHeaders: customHeader, data: QUERY})).resolves.toEqual(buildExpectedResponse(successResponse)); + await expect(client.query({extraHeaders: customHeader, data: QUERY})).resolves.toEqual( + buildExpectedResponse(successResponse), + ); customHeader[ShopifyHeader.AccessToken] = 'bork'; assertHttpRequest('POST', DOMAIN, '/admin/api/unstable/graphql.json', customHeader, QUERY); }); + + it('can handle queries with variables', async () => { + const client: GraphqlClient = new GraphqlClient(DOMAIN, 'bork'); + const queryWithVariables = { + query: `query FirstTwo($first: Int) { + products(first: $first) { + edges { + node { + id + } + } + } + }`, + variables: `{ + 'first': 2, + }`, + }; + const expectedResponse = { + data: { + products: { + edges: [ + { + node: { + id: 'foo', + }, + }, + { + node: { + id: 'bar', + }, + }, + ], + }, + }, + }; + + fetchMock.mockResponseOnce(JSON.stringify(expectedResponse)); + + await expect(client.query({data: queryWithVariables})).resolves.toEqual(buildExpectedResponse(expectedResponse)); + }); }); function buildExpectedResponse(obj: unknown) { From db71552c70c19b8b57d0e90f4e17265c64ede98d Mon Sep 17 00:00:00 2001 From: Emily Harber Date: Tue, 9 Feb 2021 11:13:10 -0500 Subject: [PATCH 2/2] Add assertHttpRequest to test --- .../graphql/test/graphql_client.test.ts | 8 ++++++++ src/clients/http_client/test/test_helper.ts | 18 ++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/clients/graphql/test/graphql_client.test.ts b/src/clients/graphql/test/graphql_client.test.ts index b7b0842a2..1ed148d15 100644 --- a/src/clients/graphql/test/graphql_client.test.ts +++ b/src/clients/graphql/test/graphql_client.test.ts @@ -83,6 +83,14 @@ describe('GraphQL client', () => { fetchMock.mockResponseOnce(JSON.stringify(expectedResponse)); await expect(client.query({data: queryWithVariables})).resolves.toEqual(buildExpectedResponse(expectedResponse)); + + assertHttpRequest( + 'POST', + DOMAIN, + '/admin/api/unstable/graphql.json', + {'Content-Length': 219, 'Content-Type': 'application/json', 'X-Shopify-Access-Token': 'bork'}, + JSON.stringify(queryWithVariables), + ); }); }); diff --git a/src/clients/http_client/test/test_helper.ts b/src/clients/http_client/test/test_helper.ts index 3d0677267..76d03a6dd 100644 --- a/src/clients/http_client/test/test_helper.ts +++ b/src/clients/http_client/test/test_helper.ts @@ -16,15 +16,13 @@ export function assertHttpRequest( const maxCall = currentCall + tries; for (let i = currentCall; i < maxCall; i++) { currentCall++; - expect(fetchMock.mock.calls[i]).toEqual( - [ - `https://${domain}${path}`, - expect.objectContaining({ - method, - headers: expect.objectContaining(headers), - body: data, - }), - ], - ); + expect(fetchMock.mock.calls[i]).toEqual([ + `https://${domain}${path}`, + expect.objectContaining({ + method, + headers: expect.objectContaining(headers), + body: data, + }), + ]); } }