productCreate Options API Mutation Not working

Can anyone shed any light on an issue I am having with a productCreate mutation…

I am sending the mutation inside a query as JSON…

{"query":"mutation {productCreate(input: {descriptionHtml: \"<%[Product Description]%>\", options: [{name: \"<%[Option1Test]%>\", position: 1, values: [\"<%[Option2Test]%>\", \"<%[Option3Test]%>\"]}], giftCard: <%JSONFormat([giftCard])%>, giftCardTemplateSuffix: \"<%[Gift Card Template?]%>\", productType: \"<%[Product Type]%>\", redirectNewHandle: <%JSONFormat([Re-Direct New Handle?])%>, requiresSellingPlan: <%JSONFormat([Requires Selling Plan?])%>, seo: {description: \"<%[SEO Description]%>\", title: \"<%[SEO Title]%>\"}, status: DRAFT, tags: \"<%[TAGS List]%>\", templateSuffix: \"<%[templateSuffix]%>\", title: \"<%[MVSKU Product Title]%>\", vendor: \"<%[MYVEND]%>\"}), {userErrors {field, message} product {availablePublicationCount, createdAt, descriptionHtml, description, handle, hasOnlyDefaultVariant, hasOutOfStockVariants, id, onlineStorePreviewUrl, onlineStoreUrl, productType, publishedAt, requiresSellingPlan, status, tags, templateSuffix, title, totalInventory, totalVariants, tracksInventory, updatedAt, vendor,isGiftCard, mediaCount, options(first: 10) {id, name, position, values}, priceRange {maxVariantPrice {amount, currencyCode} minVariantPrice {amount,currencyCode}}, productType, seo{description, title}, }}} "}

This is the request processed and the Response…

Request

{≡}

POST https://d4fce6.myshopify.com/admin/api/2023-07/graphql.json HTTP/1.1
{ "query": "mutation {productCreate(input: {descriptionHtml: \"TestDescription\", options: [{name: \"Option1\", position: 1, values: [\"Option2\", \"Option3\"]}], giftCard: false, giftCardTemplateSuffix: \"\", productType: \"Tool Setter\", redirectNewHandle: true, requiresSellingPlan: false, seo: {description: \"Our world-class flagship Contact CNC Optical Tool Setter. Industrial Performance. For professional machine shops. Repeatability of 0.5µm. IP68 Rated.\", title: \"Nano500 CNC Contact Optical Tool Setter | EMG Precision\"}, status: DRAFT, tags: \"\", templateSuffix: \"\", title: \"EMG Precision Nano 2 | Ø20mm Contact Optical Tool Setter CNC Z Axis\", vendor: \"\"}), {userErrors {field, message} product {availablePublicationCount, createdAt, descriptionHtml, description, handle, hasOnlyDefaultVariant, hasOutOfStockVariants, id, onlineStorePreviewUrl, onlineStoreUrl, productType, publishedAt, requiresSellingPlan, status, tags, templateSuffix, title, totalInventory, totalVariants, tracksInventory, updatedAt, vendor,isGiftCard, mediaCount, options(first: 10) {id, name, position, values}, priceRange {maxVariantPrice {amount, currencyCode} minVariantPrice {amount,currencyCode}}, productType, seo{description, title}, }}} " }

Response

{≡}

HTTP/1.1 200 OK
{ "errors": [ { "message": "Argument 'options' on InputObject 'ProductInput' has an invalid value ([{name: \"Option1\", position: 1, values: [\"Option2\", \"Option3\"]}]). Expected type '[String!]'.", "locations": [ { "line": 1, "column": 32 } ], "path": [ "mutation", "productCreate", "input", "options" ], "extensions": { "code": "argumentLiteralsIncompatible", "typeName": "InputObject", "argumentName": "options" } } ] }

No matter what I seem to do I cannot get the mutation to work, and it is specific to the ‘Options’ part. If i remove the product options element, it works perfectly…

I hope someone has some bright idea because shopify support is neither use nor ornament.

Hi @JonnyGee

Thank you for reaching out to the Shopify community.

It has come to my attention that there may be an issue with the input you are providing in the options field. The options field is designed to accept an array of string values exclusively. To assist you further, I have included a sample payload illustrating the correct input format:

{
  "input": {
    "bodyHtml": "",
    "collectionsToJoin": [
      ""
    ],
    "collectionsToLeave": [
      ""
    ],
    "customProductType": "",
    "descriptionHtml": "",
    "giftCard": true,
    "giftCardTemplateSuffix": "",
    "handle": "",
    "id": "",
    "images": [{
      "altText": "",
      "id": "",
      "src": ""
    }],
    "metafields": [{
      "description": "",
      "id": "",
      "key": "",
      "namespace": "",
      "type": "",
      "value": ""
    }],
    "options": [
      ""
    ],
    "privateMetafields": [{
      "key": "",
      "namespace": "",
      "owner": "",
      "valueInput": {
        "value": "",
        "valueType": ""
      }
    }],
    "productCategory": {
      "productTaxonomyNodeId": ""
    },
    "productPublications": [{
      "channelHandle": "",
      "channelId": "",
      "publicationId": "",
      "publishDate": ""
    }],
    "productType": "",
    "publications": [{
      "channelHandle": "",
      "channelId": "",
      "publicationId": "",
      "publishDate": ""
    }],
    "publishDate": "",
    "publishOn": "",
    "published": true,
    "publishedAt": "",
    "redirectNewHandle": true,
    "requiresSellingPlan": true,
    "seo": {
      "description": "",
      "title": ""
    },
    "standardizedProductType": {
      "productTaxonomyNodeId": ""
    },
    "status": "",
    "tags": [
      ""
    ],
    "templateSuffix": "",
    "title": "",
    "variants": [{
      "barcode": "",
      "compareAtPrice": "",
      "fulfillmentServiceId": "",
      "harmonizedSystemCode": "",
      "id": "",
      "imageId": "",
      "imageSrc": "",
      "inventoryItem": {
        "cost": "",
        "tracked": true
      },
      "inventoryManagement": "",
      "inventoryPolicy": "",
      "inventoryQuantities": [{
        "availableQuantity": 1,
        "locationId": ""
      }],
      "mediaSrc": [
        ""
      ],
      "metafields": [{
        "description": "",
        "id": "",
        "key": "",
        "namespace": "",
        "type": "",
        "value": ""
      }],
      "options": [
        ""
      ],
      "position": 1,
      "price": "",
      "privateMetafields": [{
        "key": "",
        "namespace": "",
        "owner": "",
        "valueInput": {
          "value": "",
          "valueType": ""
        }
      }],
      "productId": "",
      "requiresShipping": true,
      "sku": "",
      "taxCode": "",
      "taxable": true,
      "title": "",
      "weight": 1.1,
      "weightUnit": ""
    }],
    "vendor": ""
  },
  "media": [{
    "alt": "",
    "mediaContentType": "",
    "originalSource": ""
  }]
}

Should you encounter any difficulties during the implementation of this code snippet, please do not hesitate to reach out to me for specific assistance. Just help me with the current implemented code snippet so that I can modify and share it here. I am here to provide support in resolving any issues you may face.

Thank you.

Thanks for your reply, although I do not believe this approach will work… You have not stated how multiple options should be syntaxed..

And there looks to be other issues with the code above… e.g There is no publications section now… This is handled with productPublish.

It seems that the Dev Guides are really inadequate and inaccurate.

Can you please confirm the syntax required to add one product with 3 options, each with multiple variants. It simply doesnt work as suggested in the documentation.

Kind Regards

Jonathan

Also,

What is the options element for in the productCreate? You cannot create options and save them without variants! So what is the actual working code to create Product Options and Variants please? The Dev guide is obviously out of date and inaccurate.

Regards

Jonathan

Hi @JonnyGee ,

I hope this message finds you well. I would like to address your concerns with the utmost seriousness. However, it appears that, at this juncture, the only viable course of action is to proceed with experimentation.

To provide you with a clearer understanding of the matter, I have prepared a written example that demonstrates how various options can be effectively utilized. I kindly request you to review the enclosed payload and examine its contents thoroughly. Should any questions or uncertainties arise during your examination, please do not hesitate to reach out for clarification.

Here is the payload for your reference:

{
  "input": {
    "bodyHtml": "",
    "descriptionHtml": "",
    "options": [
      "Size", "Color"
    ],
    "published": true,
    "tags": [
      "example1", "example2"
    ],
    "title": "Test Product",
    "variants": [{
      "compareAtPrice": "100",
      "inventoryQuantities": [{
        "availableQuantity": 100,
        "locationId": "gid://shopify/Location/{location_id}"
      }],
      "options": [
        "L", "Red"
      ],
      "position": 1,
      "price": "50",
      "title": "Test Variant 1"
    },
    {
      "compareAtPrice": "100",
      "inventoryQuantities": [{
        "availableQuantity": 100,
        "locationId": "gid://shopify/Location/{location_id}"
      }],
      "options": [
        "XL", "Red"
      ],
      "position": 2,
      "price": "50",
      "title": "Test Variant 2"
    },
    {
      "compareAtPrice": "100",
      "inventoryQuantities": [{
        "availableQuantity": 100,
        "locationId": "gid://shopify/Location/{location_id}"
      }],
      "options": [
        "L", "Green"
      ],
      "position": 3,
      "price": "50",
      "title": "Test Variant 3"
    }]
  }
}

Your cooperation and feedback on this matter are highly appreciated. Should you require any further assistance or have additional inquiries, please do not hesitate to contact me.

Thank you.

Hi @JonnyGee ,

I wanted to explain the “options” element on the product level in simpler terms. Think of it as a way to group different variations of a product. Let’s take the example of an ice cream product. We want to offer different flavours and sizes of ice cream, so in the “options” section, we list “Flavor” and “Size.”

For flavours, we have three options: Vanilla, Pista, and Chocolate. For sizes, we offer two options: 500ml and 1000ml.

Now, it’s up to you to create specific combinations on the variant level. For instance, you can create a variant with Vanilla and 500ml, or another one with Vanilla and 1000ml. In total, you can create six different combinations based on the options I mentioned.

Here’s a breakdown:

  1. Variant 1: Vanilla, 500ml
  2. Variant 2: Vanilla, 1000ml
  3. Variant 3: Pista, 500ml
  4. Variant 4: Pista, 1000ml
  5. Variant 5: Chocolate, 500ml
  6. Variant 6: Chocolate, 1000ml

I hope this helps clarify how the “options” element works for product variants. If you have any more questions or need further assistance, please feel free to ask.

Thank You,

I fully understand how options and variants work.

What does not seem obvious or even possible based on the information presented in the Shopify Dev is how to create those with a mutation. What mutation(s) should be used to create a product with max. three options and multiple sub options to create the list of possible variants…?

As you can see by the request and response below, it does not work. Any ideas?### Request

{≡}

POST https://d4fce6.myshopify.com/admin/api/2023-07/graphql.json HTTP/1.1
{ “query”: "mutation {productCreate(input: {descriptionHtml: "Test Description", options: [{name: "Size", position: 1, values: ["Large", "Small"]}, {name: "Material", position: 2, values: ["Stainless Steel", "Aluminium", "Plastic"]}], giftCard: false, giftCardTemplateSuffix: "", productType: "CNC Tool Setting", redirectNewHandle: true, requiresSellingPlan: false, seo: {description: "#########", title: "#############"}, status: DRAFT, tags: "##########, ##########, ########, #########, ##########, #########, ##########, ##########, ##########, #########, Purchaseable, Visible, ", templateSuffix: "######", title: "#########", vendor: "EMG Precision Ltd."}), {userErrors {field, message} product {availablePublicationCount, createdAt, descriptionHtml, description, handle, hasOnlyDefaultVariant, hasOutOfStockVariants, id, onlineStorePreviewUrl, onlineStoreUrl, productType, publishedAt, requiresSellingPlan, status, tags, templateSuffix, title, totalInventory, totalVariants, tracksInventory, updatedAt, vendor,isGiftCard, mediaCount, options(first: 10) {id, name, position, values}, priceRange {maxVariantPrice {amount, currencyCode} minVariantPrice {amount,currencyCode}}, productType, seo{description, title}, }}} " }

Response{≡}

HTTP/1.1 200 OK
{ “errors”: [ { “message”: “Argument ‘options’ on InputObject ‘ProductInput’ has an invalid value ([{name: "Size", position: 1, values: ["Large", "Small"]}, {name: "Material", position: 2, values: ["Stainless Steel", "Aluminium", "Plastic"]}]). Expected type ‘[String!]’.”, “locations”: [ { “line”: 1, “column”: 32 } ], “path”: [ “mutation”, “productCreate”, “input”, “options” ], “extensions”: { “code”: “argumentLiteralsIncompatible”, “typeName”: “InputObject”, “argumentName”: “options” } } ] }

Hi @JonnyGee ,

Thank you for reaching out to the Shopify community.

I hope you are doing fantastic, Please check the Postman collection in the attachment. I have structured your payload and tested it, this works as expected. please give it a try. Do let me know if you face any issues.

https://drive.google.com/file/d/1PgoT1IOdTvr_nqSrbT9eXg7hUGoVJTRt/view?usp=drive_link

Thanks,

Hi again,

I feel like I am going round and round in circles here…

In Shopify Products Edit, if you manually create three options, each with 3 possible values, and click done, shopify automatically creates 9 variables. Which is perfect and as it should be.

How do I create 3 options, each with 3 sub values in GraphQL? so shopify automatically creates the 9 possible values which will then be fed back to me as a product update Webhook.

I don’t want to manually set Variants… I want to only set the Options and the Option Sub values, so shopify creates all the possible variants.