Yassine Badache
Yassine Badache

Reputation: 1851

GraphiQL shows an object but request sends back "cannot coerce value to String"

I am using the GraphiQL editor for Shopify Admin in order to test a mutation to push a product to a store. This is the mutation:

mutation createProduct($input: ProductInput!) {
      productCreate(input: $input) {
        product {
          title
          descriptionHtml
          vendor
          handle
          tags
          variants(first: 50)  {
              nodes  {
                  inventoryItem {
                      tracked
                      sku
                  }
                  price
                  sku
              }
          }
          options { << problem seem to occur here
              name
              position
              values
          }
        }
      }
    }

input is declared as a ProductInput! and input.options should be an array of ProductOption, which have fields such as name, position and values. The GraphiQL editor makes it clear.

When I try to pass any option as an array of object, such as:

...
"options": [
   {
     "name": "Size",
     "position": 1,
     "values": [ "Small", "Medium", "Large" ]
   }
]
...

The Shopify API throws me this error message:

"message": "Variable $input of type ProductInput! was provided invalid value for options.0 (Could not coerce value {name:\"Taille\",position:1,values:[\"S\",\"M\",\"L\"]} to String)"

Did I miss something in the API ? This was how we used to input options for a product using the REST API, how do you achieve the same result in GraphQL (for instance, what if I want two options, size and color with different choice values) ?

Is there a discrepancy between documentation and actual implementation ?

Upvotes: 0

Views: 355

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20227

A productInput type is designed to create a single product. If you want to create >1 product in a single mutation you can gang your mutations in a single request:

mutation createProducts($p1: ProductInput!, $p2: ProductInput!) {
  m1:productCreate(input: $p1) {
    product {
      …product fields  
    }
  }
  m2:productCreate(input: $p2) {
    product {
      …product fields
    }
  }
  …and so on
}

More on this pattern

Upvotes: 0

Related Questions