Shopify-api-node
Shopify-api-node copied to clipboard
Question: Are GraphQL mutations supported?
I was trying a simple mutation the other day to update a collections descriptionHtml. I could only get it to work with fetch. I noticed that body assumes query. Could that be the issue or is it normal to wrap query around mutation?
https://github.com/MONEI/Shopify-api-node/blob/fe7eee04ee6bf2cf5db57323dc389df9d2796b58/index.js#L225
Hi @chrisandrewca
this is how I implemented mutations:
const query = gql.mutation({
operation: operation,
variables: variables,
fields: [
{product: ['id']}, {userErrors: ['field', 'message']},
],
})
// shopifyAdapter?.query is shopify.graphql(query, variables)
const promise = this.shopifyAdapter?.query(query.query, query.variables)
const response = await promise
@mjsilva Thanks for the hack
Could we get a look into this from a maintainer please to support it directly? I found myself in need of it to use the Shopify-bulk-opertions
I've had luck with this:
const shopify = new Shopify({
shopName: shop,
accessToken: accessToken
});
const productMutation = `mutation productCreate($input: ProductInput!) {
productCreate(input: $input) {
product {
id
}
userErrors {
field
message
}
}
}`;
const variables = {
"input": {
"title": params.title,
"descriptionHtml": params.description,
"variants": [{
"price": params.price,
"barcode": params.barcode,
"requiresShipping": false,
"taxable": true
}],
"published": true,
}
};
try {
let response = await shopify.graphql(productMutation, variables);
return response;
}
catch(error) {
console.log(error.message);
}
Thanks @tomredman
Just noting that this doesn't work with the Shopify Bulk API. This is what I tried:
const bulkImportMutation = `mutation {
bulkOperationRunQuery(
query: """
{
products {
edges {
node {
id
title
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}`
const shopify = new Shopify({
shopName: storeName,
accessToken
})
try {
let response = await shopify.graphql(bulkImportMutation, {})
console.log(response)
} catch (e) {
console.error(e)
}
What I get is an error saying:
Error: Field 'bulkOperationRunQuery' doesn't exist on type 'Mutation'
at /home/jon/Gitlab/Jaiqo/Loyaly/merge/node_modules/shopify-api-node/index.js:240:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ShopifyStoreProvider.bulkImportStore (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/dist/index.js:261:28)
at async AsyncFunction.bulkImportStore (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/dist/index.js:391:33)
at async typeResolvers.<computed> (/home/jon/Gitlab/Jaiqo/Loyaly/merge/node_modules/@graphql-modules/core/dist/index.cjs.js:1148:46)
at async middleware (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/node_modules/graphql-shield/dist/generator.js:29:24) {
locations: [ { line: 2, column: 7 } ],
path: [ 'mutation', 'bulkOperationRunQuery' ],
extensions: {
code: 'undefinedField',
typeName: 'Mutation',
fieldName: 'bulkOperationRunQuery'
},
response: PassThrough {
.......
......
I tried the same mutation using the Shopify Graphiql app and it launches a job sucessfully.
I think I'll fall back to a simple POST request directly to the endpoint.
I came across this, and there was a few things I found. The library uses the oldest supported version of the API which caught me out, so I'd specify the version you want to use. The variables need to have a quoted key, for example.
{
"id": "an ID"
}
My mutations worked after that :)
I can confirm that setting the apiVersion to "2023-04" fixed the problem in my case too (Error: Field 'companyCreate' doesn't exist on type 'Mutation'). 🎉
Im having the same issue with productCreate mutation, using Next.js eCommerce template and trying to create product having error like this
` const variables = { input: { title: "title", descriptionHtml: "title", published: true, } };
const query = mutation productCreate($input: ProductInput!) { productCreate(input: $input) { product { id } userErrors { field message } } };
`