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.