Queries

Every graphQL query starts from a root node. To see all the available root node you can use, see the Shopify documentation.

Query root

You can query as many root nodes as you want. For example, the following query requests the shop object, a few fields, and the customers connection in a single request.

const query = `query {
  shop {
    id
    name
    email
  }
  customers(first:1) {
    edges {
      node {
        id
        displayName
        phone
      }
    }
  }
}`;
const resp = await shopify.graphql(query); 
// console.log(resp)
/*
{
  shop: {
    id: 'gid://shopify/Shop/xxxxx',
    name: 'xxxx',
    email: 'xxxx'
  },
  "customers": {
    "edges": [
      {
        "node": {
          "id": "gid://shopify/Customer/xxx",
          "displayName": "xxx",
          "phone": null
        }
      }
    ]
  }
}
*/

Fetching specific ID

You can also pass in specific ID to fetch the resource

var query = `query {
  product(id: "${gql_product_id}") {
    id
    vendor
  }
}`;

var productResult = await shopify.graphql(query);

Fetching list of ids

const gql_variantIDs = JSON.stringify(
  variantIDs.map(id => `gid://shopify/ProductVariant/${id}`),
);

const query = `
  query {
    nodes(ids: ${gql_variantIDs}) {
      ...on ProductVariant {
        id
        legacyResourceId
        sku
        product {
          id
          legacyResourceId
          title
          tags
        }
      }
    }
}`;
const { nodes } = await shopify.graphql(query);

Last updated