Help | Creating a product via grapql productCreate and adding variant price

Solved
Excursionist
33 4 1

I'm creating an app that has a form for adding products to inventory, on this form I'd like to include the price the product is to be set to. New to graphQL I'm having trouble wrapping my head around the productCreate mutation and setting the price without receiving an error.

 

I've tried including the price within the initial productCreate input: 

 

{
  "input": {
    "title": "test product2",
    "productType": "test type",
    "descriptionHtml": "test product description 2",
    "tags": "test",
    "variants":{
      "price": "10"
    }
  } 
}

Which returns the error:

"message": "you must provide one of first or last"

At this point the product with the price has been created but the error persists on the client side confusing me as it makes it seem like the product is not created. 

 

Could someone explain to me why I'm getting this error so I can figure out how to eliminate it from happening when I create products or update them with variants? 

 

Cheers.

 

You are phoenix
0 Likes
Highlighted

Success.

Excursionist
33 4 1

Solved, but without clear understanding. I updated my mutation from:

mutation productCreate($input:ProductInput!) {
  productCreate(input:$input) {
    userErrors {
      field
      message
    }
    product {
      title
      description
      variants {
        edges {
          node {
            price
          }
        }
      }
    }
  }
}

To the following:

mutation productCreate($input:ProductInput!) {
  productCreate(input:$input) {
    userErrors {
      field
      message
    }
    product {
      title
      description
      variants(first:1) {
        edges {
          node {
            price
          }
        }
      }
    }
  }
}

Yet I do not know what this means nor where last, before, or after will be of use in the future. 

 

Still seeking clarification for my edification.

You are phoenix
0 Likes
Highlighted
Shopify Staff
Shopify Staff
330 62 45

 

Hey @jenn11  - You've got it right now. Typically fields that could return long lists accept arguments "first" and "after" to allow for specifying a specific region of a list, where "after" is a unique identifier of each of the values in the list. This is a GraphQL standard practice for fields such as this. This is used to control how much information you are loading, to avoid an overly costly query. There is some great information available here: https://www.shopify.ca/partners/blog/shopify-graphql-learning-kit#pagination

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


1 Like