A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I was able to do updateCustomer in the GraphQL playground, but when I try to do it in node.js (see below) I get the following error:
Error updating customer: RequestError: Variable $input of type CustomerInput! was provided invalid value
at Request.<anonymous> (c:\nodejs\node_modules\got\dist\source\as-promise\index.js:113:42)
at afterResponse (c:\nodejs\node_modules\shopify-api-node\index.js:296:15)
at Request.<anonymous> (c:\nodejs\node_modules\got\dist\source\as-promise\index.js:87:42)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
{name: 'RequestError', code: 'ERR_GOT_REQUEST_ERROR', timings: {…}, locations: Array(1), path: undefined, …}
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: 'myStoreAt.myshopify.com',
accessToken:'xxx'
});
const updateCustomer = async () => {
const query = `
mutation customerUpdate($input: CustomerInput!) {
customerUpdate(input: $input) {
userErrors { field, message }
customer { id, firstName, lastName }
}
} `
const variables= {
"input": {
"id": 'gid://shopify/Customer/1018520244',
"firstName": 'Frank',
"lastName": 'Jones'
}
};
try {
const response = await shopify.graphql(query);
console.log(JSON.stringify(response, null, 2));
} catch (error) {
console.log('Error updating customer:', error);
}
}
updateCustomer();
I would structure my GraphQl request as shown in the documentation and not assign "variables" as a constant but as a key within the query. Like this:
const data = await client.query({
data: {
"query": `mutation customerUpdate($input: CustomerInput!) {
customerUpdate(input: $input) {
userErrors {
field
message
}
customer {
id
firstName
lastName
}
}
}`,
"variables": {
"input": {
"id": "gid://shopify/Customer/1018520244",
"firstName": "Tobi",
"lastName": "Lutke"
}
},
},
});
You can see the documentation here. https://shopify.dev/docs/api/admin-graphql/2023-10/mutations/customerupdate
Duplicate https://community.shopify.com/c/graphql-basics-and/syntax-for-customerupdate-mutation-using-node-js-... 🙂
Scott | Developer Advocate @ Shopify