Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,36 +286,39 @@ Shopify.prototype.graphql = function graphql(data, variables) {
timeout: this.options.timeout
};

const afterResponse = (res) => {
if (res.body) {
if (res.body.extensions && res.body.extensions.cost) {
this.updateGraphqlLimits(res.body.extensions.cost);
}
const updateGqlLimits = (res) => {
if (res.body && res.body.extensions && res.body.extensions.cost) {
this.updateGraphqlLimits(res.body.extensions.cost);
}

if (Array.isArray(res.body.errors)) {
// Make Got consider this response errored and retry if needed.
throw new Error(res.body.errors[0].message);
}
return res;
};

const maybeError = (res) => {
if (res.body && Array.isArray(res.body.errors)) {
throw new Error(res.body.errors[0].message);
}

return res;
};

if (this.options.hooks) {
options.hooks = { ...this.options.hooks };
options.hooks.afterResponse = [afterResponse];
options.hooks.afterResponse = [updateGqlLimits];
options.hooks.beforeError = [decorateError];

if (this.options.hooks.afterResponse) {
options.hooks.afterResponse.push(...this.options.hooks.afterResponse);
}

options.hooks.afterResponse.push(maybeError);

if (this.options.hooks.beforeError) {
options.hooks.beforeError.push(...this.options.hooks.beforeError);
}
} else {
options.hooks = {
afterResponse: [afterResponse],
afterResponse: [updateGqlLimits, maybeError],
beforeError: [decorateError]
};
}
Expand Down
75 changes: 75 additions & 0 deletions test/shopify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,81 @@ describe('Shopify', () => {
);
});

it('can add a hook to not throw an error when the response has errors', () => {
const customerDataErrors = [
{
message:
'This app is not approved to use the email field. See https://partners.shopify.com/1/apps/1/customer_data for more details.',
path: ['customers', 'edges', '0', 'node', 'email'],
extensions: {
code: 'ACCESS_DENIED',
documentation:
'https://partners.shopify.com/1/apps/1/customer_data',
requiredAccess:
'Shopify approval is required before using the email field.'
}
},
{
message:
'This app is not approved to use the firstName field. See https://partners.shopify.com/1/apps/1/customer_data for more details.',
path: ['customers', 'edges', '0', 'node', 'firstName'],
extensions: {
code: 'ACCESS_DENIED',
documentation:
'https://partners.shopify.com/1/apps/1/customer_data',
requiredAccess:
'Shopify approval is required before using the firstName field.'
}
}
];

let calledWithErrors = undefined;

const shopify = new Shopify({
shopName,
accessToken,
hooks: {
afterResponse: [
(res) => {
if (res.body && res.body.errors) {
calledWithErrors = res.body.errors;

res.body.errors = undefined;
}

return res;
}
]
}
});

scope.post('/admin/api/graphql.json').reply(200, {
data: {
customers: {
edges: [
{
node: {
id: 'gid://shopify/Customer/1234567890',
email: null,
firstName: null
}
}
]
}
},
errors: customerDataErrors
});

return shopify.graphql('query').then((result) => {
expect(calledWithErrors).to.deep.equal(customerDataErrors);
expect(result.customers.edges[0].node.id).to.equal(
'gid://shopify/Customer/1234567890'
);
expect(result.customers.edges[0].node.email).to.equal(null);
expect(result.customers.edges[0].node.firstName).to.equal(null);
});
});

it('uses basic auth as intended', () => {
const shopify = new Shopify({ shopName, apiKey, password });

Expand Down