How to execute a simple GraphQL query after creating node app using Shopify CLI?

Does anyone have an example of how to execute a query to Shopify Admin API using GraphQL? I see the example on the Shopify Admin docs (https://shopify.dev/api/admin-graphql#client_libraries), but I am not sure where this code should be written and if I am missing any type of import statements, etc. I have created the default tempalte app using “shopify app create node” command, but can’t seem to get a simple query to work. Please help!

Here is the sample code on the Shopfy Admin Doc website:

const queryString = { products (first: 3) { edges { node { id title } } } }
// session is built as part of the OAuth process
const client = new Shopify.Clients.Graphql(
session.shop,
session.accessToken
);
const products = await client.query({
data: queryString,
});

Hi @nathan-help do you see any specific errors?

Try referencing https://github.com/Shopify/shopify-node-api/blob/main/docs/usage/graphql.md

I’d suggest following the dev doc’s Build and app example first to build up your foundations: https://shopify.dev/apps/getting-started/build-app-example

The backend section has a more in-depth example of query usage

https://shopify.dev/apps/getting-started/build-app-example/backend#step-2-add-the-api-layer

If your familiar with the insomnia rest client that has an export feature for things like curl to then adapt

Goodluck!

Hi Paul,

Thanks for the response. Here is my code:

import { Shopify } from “@shopify/shopify-api”;

export async function testQuery(){

const session = await Shopify.Utils.loadCurrentSession(req, res);

const client = new Shopify.Clients.Graphql(session.shop, session.accessToken);

const products = await client.query({
data: {* *products (first: 10) {* *edges {* *node {* *id* *title* *descriptionHtml* *}* *}* *}* *},
});

console.log(products);
}

I get the following error: “Uncaught TypeError: Buffer2 is undefined”

This error occurs with the import statement import { Shopify } from “@shopify/shopify-api”;

even without making the call to testQuery.

I am not sure if there is something wrong with the dependencies, but I just created the app with the ‘shopify create app node’ command in the terminal.

Did you ever resolve this? Facing a similar problem…