GraphQL mutation always returns 400 errors

GraphQL mutation always returns 400 errors

James071
Shopify Partner
6 0 1

I executed the following curl request according to the GraphQL manual, but I always get a 400 Bad Request error. I am very confused, what is wrong with it?

 

 

curl -X POST \
https://42shops.myshopify.com/admin/api/2022-10/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: MY-TOKEN' \
-d '{
"query": "mutation { 
  productUpdate(input: {
    id: "gid://shopify/Product/MY-PRODUCT-ID", 
    title: "New title."
  }) { 
    product { 
      id 
    } 
  } 
}"
}'

 

 

The curl request was copied from Shopify dev:

 

https://shopify.dev/api/admin-graphql/2022-10/mutations/productupdate#examples-Update_a_product_s_ti...

 

- Thanks, James

Replies 5 (5)

ShopifyDevSup
Shopify Staff
1451 238 497

Hi @James071 👋

 

Thanks for flagging this request that appears to be missing the escaped characters! It should have the double quote character " escaped with a black slash  \". Using your example, it would look like this:

 

curl -X POST \
https://42shops.myshopify.com/admin/api/2022-10/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: MY-TOKEN' \
-d '{
"query": "mutation { 
  productUpdate(input: {
    id: \"gid://shopify/Product/MY-PRODUCT-ID\", 
    title: \"New title.\"
  }) { 
    product { 
      id 
    } 
  } 
}"
}'

 

I'll be sure get the docs updated as soon as possible!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

James071
Shopify Partner
6 0 1

Hi,

 

Thanks for your reply, I tested it and it still doesn't work. 😞

 

James071_0-1669769270196.png

- Thanks, James

 

ShopifyDevSup
Shopify Staff
1451 238 497

This is a little less readable, but would you mind trying this:
 

curl -L -X POST 'https://SHOP-NAME.myshopify.com/admin/api/2022-10/graphql.json' \ -H 'X-Shopify-Access-Token: MY-TOKEN' \ -H 'Content-Type: application/json' \ --data-raw '{"query":"mutation {productUpdate(input:{id: \"gid://shopify/Product/MY-PRODUCT-ID\", title: \"title 123\"}){product{id title}}}"}'


The above works on my end:

 

Generally, it's a little easier to read when we break out the variables separately as well:

--data-raw '{"query":"mutation productUpdate($input:ProductInput!){productUpdate(input:$input){product{id}}}", "variables":{"input":{"id":"gid://shopify/Product/6767605416054","title":"New title."}}}'

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

James071
Shopify Partner
6 0 1

Hi,

Thank you for your reply, your advice is correct.

I have a few questions:

1, I don't know how to write such code. I would like to be able to use the code generated by GraphiQL APP directly and copy it into my Python code.
2, Such code is very difficult to maintain. If there is a problem, there is no way to locate what went wrong.
3, Is there any other way to interact with the Shopify server via the GraphQL API?

Thank you very much for your help. 🙂

ShopifyDevSup
Shopify Staff
1451 238 497

You can absolutely post the query/mutations from GraphiQL using Python!


For the Admin API, you can use our Python API library here, and pass the query/variables into `shopify.GraphQL().execute()`.  Alternatively, you can use the `requests` package directly like this:

 

 

 

import requests
import json

url = f"https://{shop_name}.myshopify.com/admin/api/2022-10/graphql.json"

headers = {
  "Content-Type": "application/json",
  "X-Shopify-Access-Token": token
}
query  = """
mutation productUpdate($input: ProductInput!) {
    productUpdate(input: $input) {
        product {
            id
            title
        }
    }
}
"""
variables = {
    "input": {
        "id": "gid://shopify/Product/6767605416054",
        "title": "New title."
    }
} 
data = {"query": query, "variables": variables}
requests.post(url, headers=headers, data=json.dumps(data))

 

 

 

 

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog