Solved

graphql mutation via curl with variables. Content-Type application/json or /graphql ?

palacios
Visitor
2 0 0

Hello,

I'm having troubles to declare variables in graphql and send them via curl. On this forum I found this post, but it wasn't closed and still doesn't solve my problem

https://community.shopify.com/c/shopify-apis-and-sdks/declare-variables-in-a-mutation-using-php-and-...

 

Whenever I use the Content-Type defined as application/json instead of application/graphql, Shopify sends me back the error Bad Request

For example with this curl

 

 

 

curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
 "query": "mutation productUpdate($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
}",
"variables":{
  "input": 
    {
      "id": "gid://shopify/Product/{product_id}",
      "title": "New title",
      "descriptionHtml": "<p>Update from GraphQL Admin API</p>"
    }
  }
}'

 

 

 

I get back the same message Bad Request also it I try the curl on the Shopify Docs here https://shopify.dev/api/admin-graphql/2022-04/mutations/productUpdate#examples-Update_a_product_s_ti...

 

Instead, if I use the the Content-Type as application/graphql, I get the same problem of the post I linked here above, something like

 

 

 

{"errors":[{"message":"Parse error on \"query\" (STRING) at [3, 5]","locations":[{"line":2,"column":2}]}]}

 

 

 

 

Thank you for any input!

Accepted Solution (1)

m8th
Shopify Partner
13 5 11

This is an accepted solution.

Looks like it only works if the query part does not contain newlines.

In the API documentation are also all examples without newlines in the query part.

 

curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
"query": "mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { id } userErrors { field message } } }",
"variables":{
  "input": 
    {
      "id": "gid://shopify/Product/{product_id}",
      "title": "New title",
      "descriptionHtml": "<p>Update from GraphQL Admin API</p>"
    }
  }
}'

 

RTFM!

View solution in original post

Replies 2 (2)

m8th
Shopify Partner
13 5 11

This is an accepted solution.

Looks like it only works if the query part does not contain newlines.

In the API documentation are also all examples without newlines in the query part.

 

curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
"query": "mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { id } userErrors { field message } } }",
"variables":{
  "input": 
    {
      "id": "gid://shopify/Product/{product_id}",
      "title": "New title",
      "descriptionHtml": "<p>Update from GraphQL Admin API</p>"
    }
  }
}'

 

RTFM!
palacios
Visitor
2 0 0

Thanks a lot!