MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
946 stars 278 forks source link

Getting HTTPError: Response code 400 (Bad Request) on graphql Mutation(query works) #499

Closed xmdcode closed 3 years ago

xmdcode commented 3 years ago

Hello ,

I have the following code :

connectShopify.graphql(query).then((res) => {                    
                    const mutationId = res.products.edges[0].node.id;
                    console.log(mutationId);

                    const productMutation = `mutation tagsAdd($id: ID!, $tags: [String!]!) {
                        tagsAdd(id: $id, tags: $tags) {
                          node {
                            id
                          }
                          userErrors {
                            field
                            message
                          }
                        }
                      }`;

                      const variables = `{
                        "id": "${mutationId}",
                        "tags": [
                          "placeholder"
                        ]
                      }`;

                          connectShopify.graphql(productMutation, variables).then((response) => {
                        console.log(response);
                        console.log('ok');
                         }).catch(error=>console.log(error));
                     })

When i do the query i do the first graphql query i get the gid which is in the form of : gid://shopify/Product/1234567891023

(so the shopify auth works)

but when i try the mutation (for updating with tag) i get as response HTTPError: Response code 400 (Bad Request)

.

From inside the Shopify GraphiQL App same Graphql mutation works succesfully with same permissions (only on products).

I have permissions given only on products for writing.

What could be the issue?

xmdcode commented 3 years ago

Found the solution :

const variables = { "id": "${mutationId}", "tags": [ "placeholder" ] };

Because is an object can't wrap the whole object with and need to pass the argument directly as a variable inside "id" like the following :

const variables = { "id": mutationId, "tags": [ "placeholder" ] };

Sorry for opening the issue but i was banging my head for the past 2 hours why this is not working.