Generating Shopify GraphQL Code
Here is the overview of the workflow followed by the specific GraphQL POST example you requested.
General Process Overview
The workflow for converting Shopify documentation into working code involves three main steps. This method allows you to bypass manual coding for HTTP headers, authentication formatting, and JSON parsing.
- Retrieve the cURL Statement: Navigate to the specific endpoint in the Shopify API Reference. In the "Example Request" section (usually on the right side), click the tab labeled cURL. Copy the entire shell command.
- Retrieve the Sample JSON: Immediately next to or below the request example, locate the Response section. Ensure the view is set to JSON and copy the sample response body.
- Generate the Code: Open the Chilkat curlHttp Tool. Paste the cURL command into the top box and the JSON response into the bottom box. Select your programming language and generate the code.
The resulting code will handle the API connection and automatically generate variables for the data fields found in the sample JSON.
Concrete Example: GraphQL POST Request
For a concrete example, we will use the GraphQL Admin API to create a product. This is a common POST request that often trips developers up because of the nested JSON structure required for the GraphQL query.
The Scenario: Creating a Product
We want to create a product titled "Sweet Snowboard" using the productCreate mutation.
1. The CURL Statement (The Request)
In the Shopify documentation (under Products > Product > Create), you would find a cURL command similar to this.
Note how the GraphQL query itself is wrapped inside the --data (or -d) flag.
curl -X POST "https://your-development-store.myshopify.com/admin/api/2024-01/graphql.json" \
-H "Content-Type: application/json" \
-H "X-Shopify-Access-Token: {access_token}" \
-d '{
"query": "mutation { productCreate(input: {title: \"Sweet Snowboard\", productType: \"Snowboard\", vendor: \"JadedPixel\"}) { product { id title } userErrors { field message } } }"
}'
2. The Sample JSON (The Response)
Based on the specific fields requested in the query above (product { id title }), the Shopify API would return a JSON response like this:
{
"data": {
"productCreate": {
"product": {
"id": "gid://shopify/Product/108828309",
"title": "Sweet Snowboard"
},
"userErrors": []
}
}
}
How to use this with the Chilkat Tool
Now you can generate the code to run this specific mutation and parse the ID of the new product immediately.
- Open the Tool: Go to https://tools.chilkat.io/curlHttp.
- Paste the CURL: Copy the bash code block from step 1 into the "cURL Command" box.
- Paste the JSON: Copy the JSON code block from step 2 into the "Sample Response Body" box.
- Generate: Click the button for your language (e.g., C#, Python, PHP).
What the tool will generate for you: The tool is smart enough to handle the complex escaping required for sending GraphQL inside a JSON string. For example, if you chose Python, it would generate code that looks roughly like this (simplified):
import chilkat
# ... setup code ...
# It automatically builds the JSON payload with the correct escaping
json = chilkat.CkJsonObject()
json.UpdateString("query","mutation { productCreate(input: {title: \"Sweet Snowboard\"... }) ... }")
# Sends the POST
resp = http.PostJson3("https://.../graphql.json","application/json",json)
# AND it automatically generates the parsing code for the specific fields you requested:
# This saves you from manually traversing the nested "data" > "productCreate" > "product" structure
productId = jsonResponse.StringOf("data.productCreate.product.id")
productTitle = jsonResponse.StringOf("data.productCreate.product.title")