A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hello,
I am updating our functionality for importing products from Shopify. Previously we were importing the list by calling the RESP API endpoint:
admin/api/{SHOPIFY_API_VERSION}/products.json?fields=variants&limit={PRODUCTS_COUNT_LIMIT}
(with subsequent calls with paging), we then mapped the results to our data structures and merged them into our database. However at the time we only had one partner using Shopify, and so we just used one default category for all their products.
We are expecting a lot of new partners using Shopify, and we want to import the products with their respective categories. According to your API docs, categories are only supported in the GraphQL API, not the REST API. Here I've run into the following issues:
1. No matter what I do, I'm not able to retrieve the category. The following query:
{ products(first: 10) { edges { node { id title handle productCategory { productTaxonomyNode { fullName } } variants(first:10) { edges { node { id title } } } } } }
will return products with their variants, but the productCategory field is always null. Why is that? How can I get the category?
2. What about paging? In the old implementation using REST API we had implemented paging, and were grabbing the products 250 at a time. In the GraphQL API there's the first(x) filter, and there's an "after" filter, which "Returns the elements that come after the specified cursor." But what is this cursor? How can I use this to implement paging?
3. How do I get the category hierarchy from a category? The productTaxonomyNode object has a fullName field. If the value of fullName is "Light Switches", how do I get the rest? Or will the fullName value be
2428 - Hardware > Power & Electrical Supplies > Electrical Switches > Light Switches
and I should split on the ">" symbol?
4. What's with the query cost limitations? With the REST API we would retrieve 250 products with all their variants in one request. In GraphQL if I try to get as little as 70 products, with 10 variants, I get a message saying I exceeded the single query max cost limit. Not tom mention that while there are unlikely to be more than 10 variants, it's possible there will be more, and we need to make sure we're retrieving all of them - we are actually importing the variants, not the products themselves. I don't want to sound rude, but what good is this API if it has such severe performance limitations?
OK so I have it almost all solved now:
1. the categories were null because this was an old shop with old products, from before categories were available and they were not set. We set them and they are showing up now.
3. Now that I'm getting categories I can see that they are indeed the whole thing:
"Electronics > Electronics Accessories > Computer Components > Input Devices > Keyboards"
4. I'll just keep retrieving the products and variants via REST API and use GraphQL to just get the categories.
Open issue remains point 3: paging.
My guess is since the products are by default ordered by id, the "after" keyword expects a product id. But I have no idea how to input it in the query (this is in C#):
string query = "{ \"query\": \"query { products(first: 250, after: 7490375647481) ...
{"errors":[{"message":"Argument 'after' on Field 'products' has an invalid value (7490375647481).
string query = "{ \"query\": \"query { products(first: 250, after: '7490375647481') ...
{"errors":[{"message":"Parse error on \"'\" (error) at [1, 37]","locations":[{"line":1,"column":37}]}]}
string query = "{ \"query\": \"query { products(first: 250, after: \'7490375647481\')...
{"errors":[{"message":"Parse error on \"'\" (error) at [1, 37]","locations":[{"line":1,"column":37}]}]}
string query = "{ \"query\": \"query { products(first: 250, after: \"7490375647481\")
400 Bad Request
How can I make this work?
(I guess this is worth making a separate topic, I'll do that if there is no response here)
Hi there! I'm following into the same issue.
* Being able to retrieve product category
* Being able to set a product category
Are you using graphQL for both transactions? What's the mutation used to update a product cateogry?
Hi @savia-dev,
The ProductUpdate mutation's ProductCategory takes a Product Taxonomy ID value in the ProductCategoryInput.
Here's an example of what that might look like:
mutation productUpdate($input: ProductInput!) {
productUpdate(input: $input) {
product {
id
title
productCategory{
...on ProductCategory{
productTaxonomyNode{
id
fullName
isRoot
isLeaf
}
}
}
}
userErrors {
field
message
}
}
},
variables:
{
"input": {
"id": "gid://shopify/Product/4356443570198",
"productCategory": {
"productTaxonomyNodeId": "gid://shopify/ProductTaxonomyNode/1048"
}
}
}
Hope that helps!
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog