GraphQL productCreate - Expected type 'WeightUnit'

Highlighted
New Member
4 0 0

Hi,

 

I'm attempting to create new Products / Variants via the graphQL endpoint 'productCreate' and posting a JSON body.

Since I'm posting JSON, I cannot explicitly send the enumerated values defined by Shopify's schema,  and am therefore attempting to send a string instead : 

 

mutation {
  productCreate(input: {
    variants: [
      {weight: 0, weightUnit: "KILOGRAMS", position: 1, price: "39.00", barcode: "5060409819394", sku: "16343005___A", inventoryItem: {tracked: False}, options: ["Navy", "8"]},
      {weight: 0, weightUnit: "KILOGRAMS", position: 2, price: "39.00", barcode: "5060409819400", sku: "16343005___B", inventoryItem: {tracked: False}, options: ["Navy", "10"]},
      {weight: 0, weightUnit: "KILOGRAMS", position: 3, price: "39.00", barcode: "5060409819417", sku: "16343005___C", inventoryItem: {tracked: False}, options: ["Navy", "12"]},
      {weight: 0, weightUnit: "KILOGRAMS", position: 4, price: "39.00", barcode: "5060409819424", sku: "16343005___D", inventoryItem: {tracked: False}, options: ["Navy", "14"]},
      {weight: 0, weightUnit: "KILOGRAMS", position: 5, price: "39.00", barcode: "5060409819431", sku: "16343005___E", inventoryItem: {tracked: False}, options: ["Navy", "16"]}
    ], 
    title: "Fara Top", 
    descriptionHtml: "", 
    tags: ["temp"], 
    productType: "Tops", 
    vendor: "i.Level"
  }
  ) {
    product {
      id
      variants(first: 14) {
        edges {
          node {
            id
            barcode
            sku
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

This request fails with the error : "Argument 'weightUnit' on InputObject 'ProductVariantInput' has an invalid value. Expected type 'WeightUnit'." for all variants.

Following this, I did some further reading into GraphQL and found that it is possible to accept string values in-place of enumerations however, this is dependant on the schema that Shopify have implemented. 

 

Am I doing something wrong? Or is this something that is not yet supported?

 

0 Likes
Highlighted
Shopify Partner
101 16 22

Hi @tjbartram 

 

You have multiply errors.

 

weightUnit: "KILOGRAMS"

This shouldn't be a string, call it like so:

weightUnit: KILOGRAMS

---

This is wrong as well:

tracked: False

The False should be lowercase:

tracked: false

---

And finally you need to pass the option object to the product or the options will not be created:

 

options: ["color","size"],

---

 

The final request should be the following:

 

mutation {
  productCreate(input: {
    options: ["color","size"],
    variants: [
      {weight: 0, weightUnit: KILOGRAMS, position: 1, price: "39.00", barcode: "5060409819394", sku: "16343005___A", inventoryItem: {tracked: false}, options: ["Navy", "8"]},
      {weight: 0, weightUnit: KILOGRAMS, position: 2, price: "39.00", barcode: "5060409819400", sku: "16343005___B", inventoryItem: {tracked: false}, options: ["Navy", "10"]},
      {weight: 0, weightUnit: KILOGRAMS, position: 3, price: "39.00", barcode: "5060409819417", sku: "16343005___C", inventoryItem: {tracked: false}, options: ["Navy", "12"]},
      {weight: 0, weightUnit: KILOGRAMS, position: 4, price: "39.00", barcode: "5060409819424", sku: "16343005___D", inventoryItem: {tracked: false}, options: ["Navy", "14"]},
      {weight: 0, weightUnit: KILOGRAMS, position: 5, price: "39.00", barcode: "5060409819431", sku: "16343005___E", inventoryItem: {tracked: false}, options: ["Navy", "16"]}
    ],
    title: "Fara Top", 
    descriptionHtml: "", 
    tags: ["temp"], 
    productType: "Tops", 
    vendor: "i.Level"
  }
  ) {
    product {
      id
      variants(first: 14) {
        edges {
          node {
            id
            barcode
            sku
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}
0 Likes
Highlighted
New Member
4 0 0

Hi @ikolarov 

 

Thank you for your response,

I'm fully aware that this value should be sent as an enumeration, however, the language I'm using (4D) does not allow custom object types.

Therefore I was attempting to emulate the input variable that the interactive example on the productCreate docs page allows you to create.

As you can see, this example autofills the `weightUnit` with a string.

 

chrome_2020-03-02_10-26-18.png

 

I was therefore under the assumption that I would be able to pass these values as a string ¯\_(ツ)_/¯

 

And finally you need to pass the option object to the product or the options will not be created:

Thanks for pointing this one out!

 

0 Likes