Heads and Tails out of GraphQL

Solved
Highlighted
Excursionist
26 0 4

I have gotten a basic "working code" using Shopify's python library.

If I run this query

query = '''
		mutation {
			productCreate(input: {title: "Title Input HERE", productType: "product type11111", vendor: "3333 Vendor"}) {
				product {
					id
				}
			}
		}
	'''
client.execute(query)

The product is created succesfully in my store.

Yet I can't figure out how and where to add other aspects of this product.

For example, where can I add the Cost?

How does it fit in to this GraphQL query?

I am reading the Shopify docs... I am watching tutorial videos... I am reading up on HowToGraphQL.com... and I am just lost.

 

I can't make sense of this.

 

For example, once this product is created, I believe that GraphQL will send back the product ID...

How can I capture it in my python script?

 

Thank you.

0 Likes
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
624 102 129

This is an accepted solution.

Hi @AJ_MotorSport ,

 

To better learn about GraphQL and the Shopify GraphQL Admin API https://shopify-graphiql-app.shopifycloud.com/login, I'd recommend you install the GraphiQL App on your store. You can play around and experiment with this app, and it'll help you get more familiar with how GraphQL works and how Shopify's API is structured. 

 

In order to add the `price` field in your productCreate mutation, you can refer to the the documentation (https://shopify.dev/docs/admin-api/graphql/reference/mutation/productcreate and https://shopify.dev/docs/admin-api/graphql/reference/mutation/input-object/productinput). One thing you need to know is that the `price` field is actually a property of the Product Variant resource and not the Product resource, so you will need to put a ProductVariantInput inside of your ProductInput to add the price.

 

Once the product is created, Shopify's GraphQL Admin API will indeed return the new product's ID (because you specified that in your mutation with "id"). In your Python Script, you should assign a variable `result` to be equal to the `client.execute(query)`. Once the mutation API request completes and returns, `result` will now be a string of the JSON object returned by Shopify's GraphQL Admin API - and within this JSON object you will get the ID.

 

 Putting this all together, this is the code you need to implement:

client = shopify.GraphQL()
query = '''
mutation {
			productCreate(input: {title: "Python 3", productType: "product 3", vendor: "4444 Vendor", variants: [{price: "14.99"}]}) {
				product {
					id
				}
			}
		}
  '''

result = client.execute(query)
json_result = json.loads(result)
print("ID of the newly created product is: " + json_result["data"]["productCreate"]["product"]["id"])

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

Highlighted
Excursionist
26 0 4

@hassainThank you very much.

I just installed the GraphiQL app into my store.

Will start playing with it a little bit more.

 

 

It helped a lot when you mentioned that the price is a "variant".

That was twisting my mind.

 

Will keep this thread updated with additional information.

Thank you!

0 Likes