I’m having issues creating productOptions. (Creating options and variants).
It doesn’t matter if I’m using productCreate with productOptions set, or if I’m using productOptionsCreate on an existing product. I cannot understand why and would love to have your help.
I have this query:
Mutation:
mutation productOptionsCreate($productId: ID!, $options: [OptionCreateInput!]!, $variantStrategy: ProductOptionCreateVariantStrategy) {
productOptionsCreate(
productId: $productId
options: $options
variantStrategy: $variantStrategy
) {
userErrors {
field
message
code
}
product {
id
variants(first: 10) {
nodes {
id
title
selectedOptions {
name
value
}
}
}
options {
id
name
values
position
optionValues {
id
name
hasVariants
}
}
}
}
}
Variables:
{
"productId": "gid://shopify/Product/7081223454853",
"options": [
{
"name": "Weight",
"values": [
{
"name": "Any weight"
}
]
}
],
"variantStrategy": "CREATE"
}
Calling this through the webinterface yields:
{
"data": {
"productOptionsCreate": {
"userErrors": [],
"product": {
"id": "gid://shopify/Product/7081223454853",
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/41001544286341",
"title": "Any weight",
"selectedOptions": [
{
"name": "Weight",
"value": "Any weight"
}
]
}
]
},
"options": [
{
"id": "gid://shopify/ProductOption/9066185457797",
"name": "Weight",
"values": [
"Any weight"
],
"position": 1,
"optionValues": [
{
"id": "gid://shopify/ProductOptionValue/1755303772293",
"name": "Any weight",
"hasVariants": true
}
]
}
]
}
}
},
"extensions": {
"cost": {
"requestedQueryCost": 22,
"actualQueryCost": 16,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1984,
"restoreRate": 100
}
}
}
}
While calling it with this code:
async function createProductOptions(productId, options, variants) {
console.log(`options: ${options}`);
const mutation = `
mutation productOptionsCreate($productId: ID!, $options: [OptionCreateInput!]!, $variantStrategy: ProductOptionCreateVariantStrategy) {
productOptionsCreate(productId: $productId, options: $options, variantStrategy: $variantStrategy) {
userErrors {
field
message
code
}
product {
id
variants(first: 10) {
nodes {
id
title
selectedOptions {
name
value
}
}
}
options {
id
name
values
position
optionValues {
id
name
hasVariants
}
}
}
}
}
`;
const variables = {
productId,
options,
variantStrategy: "CREATE"
};
const formattedMutation = mutation.trim().replace(/\s{2,}/g, ' ');
console.log("Creating options with query:", JSON.stringify({
query: formattedMutation,
variables: variables,
}, null, 2));
try {
const response = await fetch('shopify:admin/api/graphql.json', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: mutation, variables }),
});
const jsonResponse = await response.json();
if (jsonResponse.errors) {
throw new Error('GraphQL errors: ' + jsonResponse.errors.map(error => error.message).join(', '));
}
if (jsonResponse.data.productOptionsCreate.userErrors?.length > 0) {
throw new Error('User errors: ' + jsonResponse.data.productOptionsCreate.userErrors.map(error => error.message).join(', '));
}
console.log("Created options for product ID:", productId);
return jsonResponse.data.productOptionsCreate.product;
} catch (error) {
console.error("Error creating product options:", error);
throw error;
}
}
Yields this result:
Error creating product(s): GraphQL errors:
- `OptionCreateInput` isn't a defined input type (on `$options`)
- `ProductOptionCreateVariantStrategy` isn't a defined input type (on `$variantStrategy`)
- Field `productOptionsCreate` doesn't exist on type `Mutation`
- Variable `$productId` is declared by `productOptionsCreate` but not used
- Variable `$options` is declared by `productOptionsCreate` but not used
- Variable `$variantStrategy` is declared by `productOptionsCreate` but not used
I would really appreciate any input on this