Skip to content

allows users to provide a hook when graphql protected customer data e… #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const stopcock = require('stopcock');
const got = require('got');
const url = require('url');

const isPlainObject = require('lodash/isPlainObject');

const pkg = require('./package');
const resources = require('./resources');

Expand Down Expand Up @@ -292,9 +294,27 @@ Shopify.prototype.graphql = function graphql(data, variables) {
this.updateGraphqlLimits(res.body.extensions.cost);
}

// see https://shopify.dev/docs/apps/launch/protected-customer-data#graphql-admin-api-request-with-unapproved-fields
if (Array.isArray(res.body.errors)) {
// Make Got consider this response errored and retry if needed.
throw new Error(res.body.errors[0].message);
const isProtectedCustomerDataError = res.body.errors.every((error) => {
return (
error &&
isPlainObject(error.extensions) &&
error.extensions.code === 'ACCESS_DENIED' &&
error.extensions.documentation &&
error.extensions.requiredAccess
);
});

if (
isProtectedCustomerDataError &&
this.options.onProtectedCustomerDataError
) {
this.options.onProtectedCustomerDataError(res.body.errors);
} else {
// Make Got consider this response errored and retry if needed.
throw new Error(res.body.errors[0].message);
}
}
}

Expand Down
128 changes: 128 additions & 0 deletions test/shopify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,134 @@ describe('Shopify', () => {
);
});

it('throws an error on protected customer data errors by default', () => {
const shopify = new Shopify({ shopName, accessToken });

scope.post('/admin/api/graphql.json').reply(200, {
errors: [
{
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.'
}
}
]
});

return shopify.graphql('query').then(
() => {
throw new Error('Test invalidation');
},
(err) => {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal(
'This app is not approved to use the email field. See https://partners.shopify.com/1/apps/1/customer_data for more details.'
);
}
);
});

it('calls a provided onProtectedCustomerDataError hook when a protected customer data error occurs', () => {
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,
onProtectedCustomerDataError: (errors) => {
calledWithErrors = errors;
}
});

scope.post('/admin/api/graphql.json').reply(200, {
errors: customerDataErrors
});

return shopify.graphql('query').then(() => {
expect(calledWithErrors).to.deep.equal(customerDataErrors);
});
});

it('throws an error if the onProtectedCustomerDataError hook is provided but not all errors are customer data errors', () => {
const shopify = new Shopify({ shopName, accessToken });

scope.post('/admin/api/graphql.json').reply(200, {
errors: [
{
message: "Field 'foo' doesn't exist on type 'QueryRoot'",
locations: [
{
line: 1,
column: 3
}
],
path: ['query', 'foo'],
extensions: {
code: 'undefinedField',
typeName: 'QueryRoot',
fieldName: 'foo'
}
},
{
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.'
}
}
]
});

return shopify.graphql('query').then(
() => {
throw new Error('Test invalidation');
},
(err) => {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal(
"Field 'foo' doesn't exist on type 'QueryRoot'"
);
}
);
});

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

Expand Down
13 changes: 13 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,16 @@ declare namespace Shopify {
interval: number;
}

export interface IProtectedCustomerDataError {
message: string;
path: string[];
extension: {
code: 'ACCESS_DENIED';
documentation: string;
requiredAccess: string;
};
}

export interface IPublicShopifyConfig {
accessToken: string;
apiVersion?: string;
Expand All @@ -796,6 +806,9 @@ declare namespace Shopify {
timeout?: number;
hooks?: Hooks;
agent?: Agents;
onProtectedCustomerDataError?: (
errors: IProtectedCustomerDataError[]
) => void;
}

export interface IPrivateShopifyConfig {
Expand Down