allows users to provide a hook when graphql protected customer data e…#672
Closed
infiton wants to merge 1 commit intoMONEI:masterfrom
Closed
allows users to provide a hook when graphql protected customer data e…#672infiton wants to merge 1 commit intoMONEI:masterfrom
infiton wants to merge 1 commit intoMONEI:masterfrom
Conversation
Collaborator
|
I'm not a fan of adding a new option for this edge case. What do you think about something like this diff --git a/index.js b/index.js
index 14616cf..00bd31f 100644
--- a/index.js
+++ b/index.js
@@ -286,16 +286,18 @@ 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 updateLimits = (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)) {
+ // Make Got consider this response errored and retry if needed.
+ throw new Error(res.body.errors[0].message);
}
return res;
@@ -303,19 +305,21 @@ Shopify.prototype.graphql = function graphql(data, variables) {
if (this.options.hooks) {
options.hooks = { ...this.options.hooks };
- options.hooks.afterResponse = [afterResponse];
+ options.hooks.afterResponse = [updateLimits];
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: [updateLimits, maybeError],
beforeError: [decorateError]
};
}
The idea is to allow the user to add a hook that will change the behavior of the last (error) hook. This is untested, I don't know if it works. |
Contributor
Author
|
I'm down to try that; one sec I'll write some tests for it |
Contributor
Author
|
closing in favour of #673 |
Collaborator
Contributor
Author
more ergonomics than anything; I don't want to wrap every graphql request in our sync engine with an error handler and pull the data I need out of the error in the cases where it does throw |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gadget has just completed migrating all of our Shopify syncing engine to using the GraphQL api exclusively. One pain point in doing so was dealing with models that have protected customer data fields. Because there is no way to know if an app has filled out the form to request specific customer data fields the only way to know if Gadget can query these fields on apps behalf is to try.
Unfortunately with the way that shopify-api-node is currently set up, if an app does not have access the query will throw since shopify populates the
errorsfield. However they also will return the data for the query, but withnullin the place of the protected fields.I think the default behaviour of throwing if the
errorsfield is populated makes sense, however in this case we would like the option to not throw.To resolve this I have added a configuration to the client
onProtectedCustomerDataErrorthat allows for custom handling of this situation.Let me know what you think
cc @lpinca @airhorns @jcao49