Hi folks,
I’m really struggling to understand shopify graphql API. I have a DB with all my product information. I’m able to post my products to shopify with description, image, and title. I can’t for the life of me figure out how to get SKU, Price, Cost and Compare-At Price working. How can I include these on the initial upload/post of products? How can I update the price options?
Are there limitations to the workflow? I can’t really understand the docs. There are far too many poorly described options with a million dropdown obscuring info on each page.
It seems like there should be an easy example of a product post that includes all the basic information you can fill out when you create a new product in the desktop web app.
What is the best strategy/workflow for getting all the product info up and then regularly updating prices?
Hi Lovers_Audio,
Appreciate your feedback on the docs and general experience adopting GraphQL - it sounds like the main issue here is that you’ll need to use multiple mutations to add all the product and variant data you need.
First you’d use productCreate
to create a product, the options, the option values, and a single variant, and then you’d use the productVariantsBulkCreate
mutation to create the other product variants and map them to the existing options, along with SKU info. There’s a doc that describes this process here: Add product data using the new product model
Then when you’re making updates to the product after this you’d use the productVariantsBulkUpdate mutation. I hope this helps - I’ve also connected with our dev docs team to pass on the feedback you have on providing clearer examples and options.
Thanks @Liam-Shopify ,
I seemed to get price and compareAtprice working with the [productVariantsBulkUpdate, but I’m unable to get cost and sku working. I’m trying to place it in InventoryItem like in the docs, but I’m getting this error:
GraphQL Errors: Field ‘cost’ doesn’t exist on type ‘InventoryItem’
query = <<~GRAPHQL
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
price
compareAtPrice
inventoryItem {
cost
sku
}
}
userErrors {
field
message
}
}
}
GRAPHQL
variables = {
productId: product_id,
variants: [
{
id: variant_id,
price: price.to_s,
compareAtPrice: compare_at_price.to_s,
inventoryItem: {
cost: cost.to_s,
sku: sku
}
}
]
}
Can you tell me whats wrong with this?
it should be unitCost in your result.
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
price
compareAtPrice
inventoryItem {
unitCost
sku
}
}
1 Like
@Eric_Han,
Are these docs inaccurate? I tried unitCost the way you suggested and got this error:
field ‘unitCost’ returns MoneyV2 but has no selections. Did you mean 'unitCost { … }
I attempted to update to this:
inventoryItem {
unitCost {
amount
currencyCode
}
sku
}
And then I got this error:
Variable $variants of type [ProductVariantsBulkInput!]! was provided invalid value for 0.inventoryItem.unitCost (Field is not defined on InventoryItemInput)
@Liam-Shopify Can you tell whats wrong here?
i mean your result not the input param.
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
price
compareAtPrice
inventoryItem {
unitCost{
amount
}
sku
}
}
@Eric_Han,
I have exactly what you are suggesting. Are you sure this is correct? Are you able to successfully update cost this?
I keep getting this error:
GraphQL Errors: Variable $variants of type [ProductVariantsBulkInput!]! was provided invalid value for 0.inventoryItem.unitCost (Field is not defined on InventoryItemInput)
Seems to be a mismatch that your input needs "cost"
but your return fields must specify "unitCost"
.
>>> print (q2)
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
price
compareAtPrice
inventoryItem {
unitCost { amount }
sku
}
}
userErrors {
field
message
}
}
}
>>> pprint (variables)
{'productId': 'gid://shopify/Product/MY_PRODUCT_ID',
'variants': [{'id': 'gid://shopify/ProductVariant/MY_VARIANT_ID',
'inventoryItem': {'cost': 5.99},
'price': 15.99}]}
>>> result = task_runner.client.execute(q2, variables)
>>> pprint (result)
{'data': {'productVariantsBulkUpdate': {'product': {'id': 'gid://shopify/Product/MY_PRODUCT_ID'},
'productVariants': [{'compareAtPrice': None,
'id': 'gid://shopify/ProductVariant/MY_VARIANT_ID',
'inventoryItem': {'sku': '',
'unitCost': {'amount': '5.99'}},
'price': '15.99'}],
'userErrors': []}},
'extensions': {'cost': {'actualQueryCost': 11,
'requestedQueryCost': 11,
'throttleStatus': {'currentlyAvailable': 1989,
'maximumAvailable': 2000.0,
'restoreRate': 100.0}}}}
>>>
1 Like
Wow this worked! Thanks @dogowner
Unfortunately in the shopify product UI viewing my products by most recently edited this action does not bring the item to the top of the list. The cost is updated, but unfortunately it doesn’t count as an update as far as sorting is concered.
@Liam-Shopify You may want to note this inconsistency. I don’t think I ever would have figured this out on my own.