GraphQL

GraphQL is a different way of accessing data. instead of making calls to various REST api endpoints, you can call a single GraphQL API and specify exactly what data you need, which attribute to fetch. This allows for saving bandwidth and avoiding making multiple api calls.

Data in GraphQL are modeled as nodes and edges in a graph relationship. For example, think of the shop as a node, and in a shop node, there can be edges to list of products and list of orders. With graphQL, you can fetch nested edges and specify the fields you want in a single query.

For example, let's say you want to get the shop's basic information as well as the customer name in the shop. With Rest API, you may need 2 separate API calls, one for shop and one for customers

const shop = shopify.shop.get({
  fields: 'name, email',
});
const customers = shopify.customer.list({
  fields: 'id, first_name, last_name, email',
  limit: 10,
});

With graphql, you can specify the data you want in a more intuitive way

const query = `
query {
  shop {
    id
    name
    email
  }
  customers(first:10) {
    edges {
      node {
        id
        firstName
        lastName
        email
      }
    }
  }
}`;
const data = await shopify.graphql(query);

Note that one big difference between rest and graphql is that the naming convention are different. For graphQL, all the variable names are in camelCase where as for REST api, they use underscore_case

Last updated