Shopify API productTaxonomyNodeId error when using productCreate in Node.js GraphQL

The following Shopify API 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 2024-07.

I used the following as the main references.

How can I fix this problem?

he error “invalid id” is likely due to the product category ID “gid://shopify/TaxonomyCategory/fr” being invalid or outdated. Here are some possible reasons and solutions:

1. Invalid Product Category ID:

  • Double-check the ID: Ensure that the ID “gid://shopify/TaxonomyCategory/fr” is correct and matches the ID of an existing product category in your Shopify store. You can find the correct ID by using the GraphQL query query { productCategories { id } }.
  • Check for changes: Product category IDs may change over time. If you obtained the ID in July 2024, it might have become invalid since then.

2. Missing or Incorrect Product Category:

  • Create the category: If the product category “fr” doesn’t exist, create it using the GraphQL query mutation { productCategoryCreate(input: { name: "fr" }) { productCategory { id } } }.
  • Update the ID: Once the category is created, replace the ID in your product creation query with the newly generated ID.

3. API Version Compatibility:

  • Check API version: Ensure that the API version you’re using (ApiVersion.April23) supports the productCategory field and its productTaxonomyNodeId property. If not, try using a different API version.

4. GraphQL Query Syntax:

  • Verify syntax: Double-check the syntax of your GraphQL query for any errors or typos.

Revised Code:

JavaScript

import "@shopify/shopify-api/adapters/node";
import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-04";    1.  github.com github.com

// ... (rest of your code)

// Replace 'gid://shopify/TaxonomyCategory/fr' with the correct product category ID
const data = await shopifyClient.query({
    data: `mutation {
        productCreate(input: {
            // ... (rest of your product creation input)
            productCategory: {
                productTaxonomyNodeId: "gid://shopify/TaxonomyCategory/your_correct_id"
            }
        }) {
            product {
                id
            }
        }
    }`,
});

By following these steps and carefully verifying the product category ID, you should be able to successfully create products with the specified category in your Shopify store.