A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I'm having trouble with a particular product. I'm fetching variants for a particular product in the GraphQL API, but getting far fewer results than with the REST API.
REST API gives me 12 variants. GraphQL gives me 2 (earlier today, only 1).
Here's the query I'm using:
query ($query: String, $cursor: String, $per_page: Int, $sortKey: ProductVariantSortKeys) { productVariants(first: $per_page, after: $cursor, query: $query, sortKey: $sortKey) { edges { cursor node { legacyResourceId title price image { originalSrc } } } pageInfo { hasNextPage } } }
Seems to be just with one product, but I can't see what would prevent the rest of the results from coming back.
Any ideas? Thanks!
Solved! Go to the solution
This is an accepted solution.
I think you may be approaching it the wrong way, you are currently returning all variants and then searching them for the product_id. The correct way would be to query the product_id and then return all of it's variants like so:
{ product(id: "gid://shopify/Product/255985516570") { variants (first:25) { edges { node { legacyResourceId title price image { originalSrc } } } } } }
This will return the fields you wanted for the first 25 variants on that product. By querying ProductVariants you would be searching every variant on the shop for the string of your product_id, which as far as I know is not a supported query parameter.
Ryan | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hi @danielmorrison ,
To begin answering this we would need a bit more info. Any of the following can help, what is the ID of the variant you are trying to get? The X-Request-ID header response from one of the GraphQL requests? Or the query variable values that you are passing.
Without more, best I can do is guess. And if I had to guess, you may only be asking for the first 2 in GraphQL.
Ryan | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Ryan, I'm definitely not requesting only 2. Other products show all the variants.
Product ID: 255985516570
Variables:
"query": "product_id:255985516570, "cursor": null, "per_page": 25, "sortKey": POSITION
Today it seems to be returning 3 via GraphQL. 12 via REST.
Thanks!
This is an accepted solution.
I think you may be approaching it the wrong way, you are currently returning all variants and then searching them for the product_id. The correct way would be to query the product_id and then return all of it's variants like so:
{ product(id: "gid://shopify/Product/255985516570") { variants (first:25) { edges { node { legacyResourceId title price image { originalSrc } } } } } }
This will return the fields you wanted for the first 25 variants on that product. By querying ProductVariants you would be searching every variant on the shop for the string of your product_id, which as far as I know is not a supported query parameter.
Ryan | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Ryan
Ok, that does work as expected.
product_id is a listed query parameter for ProductVariants in the docs: https://help.shopify.com/en/api/graphql-admin-api/reference/queryroot
Good to know about product_id. Played with it a bit but wasn't able to replicate the behaviour you are seeing unfortunately. If you can provide a specific X-Request-ID header of a call that has this issue I could dig into it a bit more.
Cheers.
Ryan | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
@Ryan x-request-id: 821e404e-ac6f-49fb-9f1c-b70b69240a86
Seems to be returning 4 today (expecting 12).