A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I am trying to create multiple product using bulk operation but all the docs I see/visited talks about uploading json file or stagedUploadsCreate, for example, I want to fetch products from API and loop over them and pass them once to bulkOperationRunQuery mutation
import requests import json # Create a list of product inputs product_inputs = [ { 'title': 'Product 1' # Add other fields as needed }, { 'title': 'Product 2' # Add other fields as needed }, # Add more product inputs as necessary ] # Construct the GraphQL request as a string query = ''' mutation{ productCreate(input: { title:input } ) { product{ id } } } ''' # Define the request headers # Create the request payload payload = { 'query': query, 'variables': { 'products': product_inputs, }, } # Send the GraphQL request response = requests.post(endpoint, headers=headers, json=payload) if response.status_code == 200: result = response.json() print(result) product_create_multiple = result.get('data', {}).get('productCreateMultiple', {}) if 'userErrors' in product_create_multiple and product_create_multiple['userErrors']: print('Errors occurred while creating products:') for error in product_create_multiple['userErrors']: print(f"Field: {error['field']}, Message: {error['message']}") elif 'products' in product_create_multiple and product_create_multiple['products']: print('Products created successfully:') for product in product_create_multiple['products']: print(f"ID: {product['id']}, Title: {product['title']}") else: print('Failed to send the GraphQL request. Status Code:', response.status_code)
Solved! Go to the solution
This is an accepted solution.
Hey @mohanedmashaly
An alternative approach would to send a mutation for each product, or multiple mutations in a single request like this:
mutation {
p1: productCreate(input: {
title: "Product 1",
descriptionHtml: "Description for product 1",
productType: "Custom product",
vendor: "Vendor"
}) {
product {
id
}
userErrors {
field
message
}
}
p2: productCreate(input: {
title: "Product 2",
descriptionHtml: "Description for product 2",
productType: "Custom product",
vendor: "Vendor"
}) {
product {
id
}
userErrors {
field
message
}
}
# continue for each product...
}
For large amount of products, the bulkOperationRunMutation/upload approach would be best.
Scott | Developer Advocate @ Shopify
Hey @mohanedmashaly
I can't help you with the Python part but here's an example of running multiple productCreate mutations with a bulk mutation.
Scott | Developer Advocate @ Shopify
Hello,
Thank you for your reply,
So it is possible to create multiple of products without using stagedUploadsCreate ?
This is an accepted solution.
Hey @mohanedmashaly
An alternative approach would to send a mutation for each product, or multiple mutations in a single request like this:
mutation {
p1: productCreate(input: {
title: "Product 1",
descriptionHtml: "Description for product 1",
productType: "Custom product",
vendor: "Vendor"
}) {
product {
id
}
userErrors {
field
message
}
}
p2: productCreate(input: {
title: "Product 2",
descriptionHtml: "Description for product 2",
productType: "Custom product",
vendor: "Vendor"
}) {
product {
id
}
userErrors {
field
message
}
}
# continue for each product...
}
For large amount of products, the bulkOperationRunMutation/upload approach would be best.
Scott | Developer Advocate @ Shopify
Thank you 😊