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
1
const shop = shopify.shop.get({
2
fields: 'name, email',
3
});
4
const customers = shopify.customer.list({
5
fields: 'id, first_name, last_name, email',
6
limit: 10,
7
});
Copied!
With graphql, you can specify the data you want in a more intuitive way
1
const query = `
2
query {
3
shop {
4
id
5
name
6
email
7
}
8
customers(first:10) {
9
edges {
10
node {
11
id
12
firstName
13
lastName
14
email
15
}
16
}
17
}
18
}`;
19
const data = await shopify.graphql(query);
Copied!
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 modified 6mo ago
Copy link