Graphql request with shopify node client

mikesynan
Shopify Partner
3 0 1

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.
 

Reply 1 (1)

ShopifyDevSup
Shopify Staff
1322 218 457

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.

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