GraphQL get all values

Solved
Excursionist
24 1 3

Hi there.

For GraphQL requests that the value count can change over time, but probably won't be very high, for my example let's say collections.

 

What's the best practice for requesting all of the available collections on the store?

If I'm correct, I think making a request like collections(first:100) when there are only say 10, is bad. So what's the best way to make sure I always get all of my collections, knowing that the number may grow, but "definitely" won't grow TOO high, and also without pagination, as I'm just putting the collections into an array to be used for various things.

 

Thanks!

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1555 80 253

Pagination is essentially the solution here. I'm not sure I understand your reasoning for not wanting to paginate. If you're just storing it into an array, you can use several requests to build that array before doing anything with it. If you could for example request all collections, and say the shop you're querying this on has 10,000 collections as an extreme example, we would just fail the requests anyway since it would be in great excess of the query cost limit. As you probably already know, we use query cost limit estimates to decline potentially oversized queries before it hits our codebase. We couldn't reliably estimate all of a resource on any given shop very well (at least from what I can tell).

 

The added benefit to pagination is that you're in control of the size of the data you receive in a response, which I believe is deeply woven into GraphQL's purpose and design.

 

Cheers.

0 Likes
Highlighted
Excursionist
24 1 3

Hi Alex.

Thanks for the reply.

I think I'm missing one part of the pagination approach. How does my code know when to stop? Excuse the ignorance cause I'm still wrapping my head around GQL.

I understand that I can request the first 50, then the next 50 by indicating that I want the next 50 after the initial first 50... but where does it end? Does it just stop on it's own?

 

My example is, I am retrieving all of the collections on the store. Which at any point, the number may change as my business members are the ones controlling it. So I can't hardcode the number to retrieve, and I don't get how I know when to stop requesting the "next" 5 or 10 or whatever.

 

Hope that makes sense and I don't sound too ignorant :(

Thanks!

0 Likes
Highlighted

Success.

Shopify Staff
Shopify Staff
1555 80 253

No sweat! So you can check the pageInfo object's hasNextPage value. Here's an example of pagination:

 

{
  customers(first: 10, after: "eyJsYXN0X2lkIjo1NDUyOTYxODc0NDgsImxhc3RfdmFsdWUiOjU0NTI5NjE4NzQ0OH0=") {
		pageInfo {
      hasNextPage
    }
    edges {
      cursor
      node {
        id
      }
    }
  }
}

The above query gets the first 10 customers, and it also specifies an after parameter, whose value is a cursor. Just inside the customers query you can see me accessing a pageInfo object which contains a value for hasNextPage. This will be true or false, telling you if there's another page to paginate through. The last CustomerEdge on the list of returned customers will have a cursor value (the rest of them will too, but we want to get everything after that cursor). We use this in the after parameter you see that I initially mentioned.

 

We have a guide on this you can refer back to as well: https://help.shopify.com/en/api/getting-started/shopify-and-graphql/pagination

 

Hope that helps!

0 Likes
Highlighted
Excursionist
24 1 3

Awesome!

Thanks so much for the information Alex. I'll check out the links.

Cheers!

0 Likes
Highlighted
Shopify Partner
11 1 0

@Alex 

I'm trying to query an after like the example above.  The original query is like this:

{
    orders(first: 10, reverse: true, query: "created_at:${timeRange}" ) {
      pageInfo {
        hasNextPage
      }
      edges {
        cursor...

incomplete code above.  From there I get the cursor of the last item.  I then query another:

orders(first: 10, after: ${queryAfterCursor} ) {
      pageInfo {
        hasNextPage
      }
      edges {
        cursor
        node {...

The problem is that the second query returns from the very first order on my shopify orders list.  It appears that "after" doesn't start right after last one but backs up to the first order.  Any idea what I'm doing wrong?

0 Likes