Focusing on managing products, variants, and collections through the API.
The following GraphQL Node.js code generates error.
Code
import "@shopify/shopify-api/adapters/node"; import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api"; import { restResources } from "@shopify/shopify-api/rest/admin/2023-04"; const shopify = shopifyApi({ apiSecretKey: Shopify_App_API_secret_key, // Note: this is the API Secret Key, NOT the API access token apiVersion: ApiVersion.April23, isCustomStoreApp: true, // this MUST be set to true (default is false) adminApiAccessToken: Shopify_Admin_API_Access_Token, // Note: this is the API access token, NOT the API Secret Key isEmbeddedApp: false, hostName: Shopify_Host_Name, // Mount REST resources. restResources, }); const shopifySession = shopify.session.customAppSession(Shopify_Host_Name); const shopifyClient = new shopify.clients.Graphql({session:shopifySession}); const data = await shopifyClient.query({ data: `mutation { productCreate(input: { handle: "test-product", title: "Sweet new product", descriptionHtml: "test", vendor: "JadedPixel", productCategory: { productTaxonomyNodeId: "gid://shopify/TaxonomyCategory/fr" }, productType: "Snowboard", tags: "new", status: DRAFT, templateSuffix: "ltl-shipping" }) { product { id } } }`, });
Error
Uncaught GraphqlQueryError Error: invalid id
at throwFailedRequest (c:\Users\BusinessOnline\Documents\software\node_modules\@shopify\shopify-api\dist\esm\lib\clients\common.mjs:55:15)
at request (c:\Users\BusinessOnline\Documents\software\node_modules\@shopify\shopify-api\dist\esm\lib\clients\admin\graphql\client.mjs:71:13)
at processTicksAndRejections (internal/process/task_queues:95:5)
When I remove productCategory: {productTaxonomyNodeId: "gid://shopify/TaxonomyCategory/fr"} in query, no error, and the product is created, which I can check in my Shopify Admin site.
I got the product category ID at https://shopify.github.io/product-taxonomy/releases/2024-07/.
I used the following as the main references.
https://shopify.dev/docs/api/admin-graphql/2024-07/mutations/productCreate
https://shopify.dev/docs/api/admin-graphql/2024-07/input-objects/ProductCategoryInput
https://shopify.dev/docs/api/admin-graphql/2024-07/objects/ProductTaxonomyNode
How can I fix this problem?
Solved! Go to the solution
This is an accepted solution.
I found a solution.
After thinking about the problem a bit, I thought maybe using the latest Shopify API might solve the problem, since the product category ID from https://shopify.github.io/product-taxonomy/releases/2024-07/ was versioned at 2024-07, which was the same as the latest Shopify API version.
The following Shopify API GraphQL Node.js code works; it creates the product as intended with the desired product category.
import "@shopify/shopify-api/adapters/node";
import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api";
import { restResources } from "@shopify/shopify-api/rest/admin/2024-07";
const shopify = shopifyApi({
apiSecretKey: Shopify_App_API_secret_key, // Note: this is the API Secret Key, NOT the API access token
apiVersion: ApiVersion.July24,
isCustomStoreApp: true, // this MUST be set to true (default is false)
adminApiAccessToken: Shopify_Admin_API_Access_Token, // Note: this is the API access token, NOT the API Secret Key
isEmbeddedApp: false,
hostName: Shopify_Host_Name,
// Mount REST resources.
restResources,
});
const shopifySession = shopify.session.customAppSession(Shopify_Host_Name);
const shopifyClient = new shopify.clients.Graphql({session:shopifySession});
const data = await shopifyClient.query({
data: `mutation {
productCreate(input: {
handle: "test-product",
title: "Sweet new product",
descriptionHtml: "test",
vendor: "JadedPixel",
category: "gid://shopify/TaxonomyCategory/fr",
productType: "Snowboard",
tags: "new",
status: DRAFT,
templateSuffix: "ltl-shipping"
}) {
product {
id
}
}
}`,
});
This is an accepted solution.
I found a solution.
After thinking about the problem a bit, I thought maybe using the latest Shopify API might solve the problem, since the product category ID from https://shopify.github.io/product-taxonomy/releases/2024-07/ was versioned at 2024-07, which was the same as the latest Shopify API version.
The following Shopify API GraphQL Node.js code works; it creates the product as intended with the desired product category.
import "@shopify/shopify-api/adapters/node";
import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api";
import { restResources } from "@shopify/shopify-api/rest/admin/2024-07";
const shopify = shopifyApi({
apiSecretKey: Shopify_App_API_secret_key, // Note: this is the API Secret Key, NOT the API access token
apiVersion: ApiVersion.July24,
isCustomStoreApp: true, // this MUST be set to true (default is false)
adminApiAccessToken: Shopify_Admin_API_Access_Token, // Note: this is the API access token, NOT the API Secret Key
isEmbeddedApp: false,
hostName: Shopify_Host_Name,
// Mount REST resources.
restResources,
});
const shopifySession = shopify.session.customAppSession(Shopify_Host_Name);
const shopifyClient = new shopify.clients.Graphql({session:shopifySession});
const data = await shopifyClient.query({
data: `mutation {
productCreate(input: {
handle: "test-product",
title: "Sweet new product",
descriptionHtml: "test",
vendor: "JadedPixel",
category: "gid://shopify/TaxonomyCategory/fr",
productType: "Snowboard",
tags: "new",
status: DRAFT,
templateSuffix: "ltl-shipping"
}) {
product {
id
}
}
}`,
});