Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

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

Solved

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

jenn11
Excursionist
33 4 7

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
Accepted Solution (1)

jenn11
Excursionist
33 4 7

This is an accepted solution.

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

View solution in original post

Replies 3 (3)

jenn11
Excursionist
33 4 7

This is an accepted solution.

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
vix
Shopify Staff
541 103 123

 

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

To learn more visit the Shopify Help Center or the Community Blog.

enkil2003
Tourist
3 0 3

This should do it, I also filtered by active.

query activeCollectionDiscount {
  automaticDiscountNodes (first: 10, query:"status:active") {
    edges {
      node {
        id
        automaticDiscount{
          ... on DiscountAutomaticBasic {
            status
            title
            customerGets {

              items {
                __typename
                ... on DiscountCollections {
                  __typename
                  collections(first:1) {
                    edges{
                      node {
                        id
                        title
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}