Graphql request with shopify node client

Topic summary

GraphQL requests from a Node/Express app using @shopify/shopify-api fail with: “TypeError: Cannot read properties of undefined (reading ‘query’)” in the /checkout-update handler.

  • App setup: shopifyApi is initialized with API key/secret, scopes (read_checkouts, unauthenticated_read_product_listings), hostName, LATEST_API_VERSION, and a privateAppStorefrontAccessToken.

  • Route logic: parses webhook body, then runs two GraphQL queries (shop { name } and first 3 products) via shopify.graphQLClient.query(…).

  • Error context: the exception implies the object before .query is undefined, so the GraphQL client is not available when invoked. The code snippet is central to diagnosing the issue.

  • Suggested next steps (from Shopify staff):
    • Review the official Node library repo for configuration/usage patterns: https://github.com/Shopify/shopify-api-js
    • Test the queries in the GraphiQL app to isolate whether the problem is with the queries or the Node implementation.

Status: No confirmed resolution; guidance provided, user still seeking help.

Summarized with AI on January 16. AI used: gpt-5.

Hi all, I am having a rough time making a simple graphql query from my basic node app. I keep getting:

“Error in /checkout-update: TypeError: Cannot read properties of undefined (reading ‘query’)”

require('dotenv').config();
const { shopifyApi, LATEST_API_VERSION } = require('@shopify/shopify-api');
const express = require('express');
const bodyParser = require('body-parser');
require('@shopify/shopify-api/adapters/node');

const app = express();
app.use(bodyParser.raw({ type: 'application/json' }));

try {
  const shopify = shopifyApi({
    apiKey: process.env.API_KEY,
    apiSecretKey: process.env.API_SECRET_KEY,
    scopes: ['read_checkouts', 'unauthenticated_read_product_listings'],
    hostName: `${process.env.SHOP_NAME}.myshopify.com`,
    apiVersion: LATEST_API_VERSION,
    privateAppStorefrontAccessToken: process.env.STOREFRONT_ACCESS_TOKEN
  });
  

  app.post('/checkout-update', async (req, res) => {
    try {
      const rawData = req.body.toString('utf8');
      const parsedData = JSON.parse(rawData);
      const checkoutId = parsedData.id;
      console.log('Captured checkout ID:', checkoutId);

      const storeQuery = `
        query {
          shop {
            name
          }
        }
      `;

      const storeResponse = await shopify.graphQLClient.query({
        query: storeQuery,
      });

      console.log('Store Name:', storeResponse.data.shop.name);

      // Query for first 3 products via the Storefront API
      const productQuery = `
        query {
          products (first: 3) {
            edges {
              node {
                id
                title
              }
            }
          }
        }
      `;

      const productResponse = await shopify.graphQLClient.query({
        query: productQuery,
      });

      console.log('First 3 Products:', productResponse.data.products.edges);

      res.status(200).end();
    } catch (innerErr) {
      console.error('Error in /checkout-update:', innerErr);
      res.status(500).end();
    }
  });

  app.listen(3003, () => {
    console.log('Server is running on port 3003');
  });
} catch (outerErr) {
  console.error('Error in setting up Shopify API:', outerErr);
}

Any help would be appreciated. Willing to compensate you for your time, feel free to DM.

1 Like

Hey @mikesynan !

Thank you for bringing this issue to our attention. If this problem pertains to our Node library, I recommend referring to our GitHub repository. It contains a wealth of information that could potentially help you resolve the issue. Here is the link to the repository: https://github.com/Shopify/shopify-api-js

In addition to that you might find it beneficial to step through the process using an API client, such as our Graphiql app. This can assist in pinpointing whether the issue lies within your specific API queries or if it’s related to the Node implementation itself.

Hope that helps!

  • Kyle G.