I’m totally new to both Shopify Storefront API AND GraphQL. I’ve been pulling my hair out the last 3-4 hours reading through docs and trying to find and write a query that, in my mind anyways, should be simple.
In a nutsell, I have a storefront that has certain products included on it with custom buy buttons. Each buy button’s ID corresponds with the product ID on my actual shopify store, meaning ../products/5854987878567
When the page loads, I need to query my store to pull the updated prices, and then reflect those prices in the button text so the person has an accurate idea of how much they are spending.
To do this, I’ve pulled all unique button ID’s and pushed them to a fresh Array, the idea being I can query all ID’s at once using GraphQL and get one nice object back that I can then parse and update the buttons accordingly.
After hours of going in circles, I haven’t gotten any closer to succeeding.
I’ve been reading the Query Root documentation and it seems like they do have the products
connection, which I assume serves as the entry point to query all products on the store.
However under the query
field itself it seems like they have every field BUT id.
available_for_sale
created_at
product_type
tag
title
updated_at
variants.price
vendor
Are all the fields they have. Since I started writing this I found sortKey
, but it failed with the error "Field 'sortKey' doesn't exist on type 'QueryRoot'"
sigh.
Here is my query that I’m currently rolling with. I’m not sure this idea will work because if I can only query one id at a time that’ll take dozens of API calls per visitor.
$.ajax({
type : 'POST',
url : 'https://xxxxx.myshopify.com/api/2020-10/graphql.json',
headers: {
'X-Shopify-Storefront-Access-Token': 'xxxxx',
'Content-Type': 'application/graphql'
},
data: 'query products { sortKey(id: "5854987878567") }',
success: function(res) {
console.log(res)
},
error: function(status) {
console.log(status)
}
});
Any help would be appreciated!
3
Answers
I don’t understand what you mean but the GraphQL below gets "id", "title", "description", "the first image" and "price" for each product(10 products):