Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #97 from Shopify/default-data-type-for-graphql
Browse files Browse the repository at this point in the history
Handle data objects with variables in GraphQL Client
  • Loading branch information
thecodepixi authored Feb 9, 2021
2 parents 2d25cf9 + db71552 commit 6ad910c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/clients/graphql/graphql_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export class GraphqlClient {
};
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});
}
}
52 changes: 51 additions & 1 deletion src/clients/graphql/test/graphql_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ 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({
Expand Down Expand Up @@ -79,6 +81,54 @@ describe('GraphQL client', () => {
it('fails to instantiate without access token', () => {
expect(() => new GraphqlClient(DOMAIN)).toThrow(ShopifyErrors.MissingRequiredArgument);
});

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));

assertHttpRequest(
'POST',
DOMAIN,
'/admin/api/unstable/graphql.json',
{'Content-Length': 219, 'Content-Type': 'application/json', 'X-Shopify-Access-Token': 'bork'},
JSON.stringify(queryWithVariables),
);
});
});

function buildExpectedResponse(obj: unknown) {
Expand Down
18 changes: 8 additions & 10 deletions src/clients/http_client/test/test_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,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,
}),
]);
}
}

0 comments on commit 6ad910c

Please sign in to comment.