graphqlshopifygraphql-jsshopify-apishopify-api-node

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 https://shopify.github.io/product-taxonomy/releases/2024-07/.

I used the following as the main references.

How can I fix this problem?


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.

    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  
                }
            }
        }`,
    });