Paginations
When fetching connections, you need to specify exactly how many items you want to fetch. And higher limit will contribute to higher query cost and subject to rate limiting. Therefore, it's important to not request an unrealistically high limit.
What is better is start with a slightly lower limit, and fetch more items if necessary. this will require paginating the graphql requests. To do that you will need to request pageInfo and cursor properties in the query. See this guide for more details.
Here is an example of continuously fetching for more line items from an order until all items are fetched.
1
​
2
const PAGE_SIZE = 15;
3
let has_next_page = false;
4
let line_item_array = [];
5
​
6
// get all line items for the order
7
do {
8
let after_query = '';
9
​
10
// generate the `after` filter
11
if (has_next_page && line_item_array.length > 0) {
12
const cursor = line_item_array[line_item_array.length - 1].cursor;
13
after_query = `, after: "${cursor}"`;
14
}
15
​
16
const query = `{
17
order(id: "${gql_order_id}") {
18
lineItems(first: ${PAGE_SIZE} ${after_query}) {
19
pageInfo{
20
hasNextPage
21
}
22
edges {
23
cursor
24
node {
25
sku
26
quantity
27
product {
28
legacyResourceId
29
tags
30
}
31
}
32
}
33
}
34
}
35
}`;
36
const { order } = await shopify.graphql(query);
37
​
38
line_item_array = line_item_array.concat(order.lineItems.edges);
39
has_next_page = order.lineItems.pageInfo.hasNextPage;
40
} while (has_next_page);
Copied!
Last modified 1yr ago
Copy link