A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
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:
- Thanks, James
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
Hi,
Thanks for your reply, I tested it and it still doesn't work. 😞
- Thanks, James
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
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. 🙂
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