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)