What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

GraphQL get all values

Solved

GraphQL get all values

LePong
Excursionist
25 1 21

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!

Accepted Solution (1)
Alex
Shopify Staff
1561 81 343

This is an accepted solution.

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!

Alex | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 9 (9)

Alex
Shopify Staff
1561 81 343

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.

Alex | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

LePong
Excursionist
25 1 21

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!

Alex
Shopify Staff
1561 81 343

This is an accepted solution.

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!

Alex | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

LePong
Excursionist
25 1 21

Awesome!

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

Cheers!

Codesto1
Shopify Partner
41 3 5

@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?

Hisham
Tourist
6 0 1

For anyone wanting an example here, take a look at this, it uses Python + GraphQL + Pagination to get orders.

 

https://github.com/mardambey/shopify-python-graphql-pagination

Jroyce1180
Pathfinder
86 4 22

Anyone have an example of pagination using qraphql with react & node?

lguerra10
Excursionist
14 0 10

This example gets reviews on a product carried as metafields, one per review

First call to the api

queryString = `
{

product(id: "gid://shopify/Product/${productId}") {
 
title
metafields(first: ${numProducts} after: "${cursor}") {
pageInfo {
hasNextPage
hasPreviousPage
}
 
edges {
cursor
node {
namespace
key
value
}
}
}
}
 
 
} `
 
calls after first
 
queryString = `
{

product(id: "gid://shopify/Product/${productId}") {
 
title
metafields(first: ${numProducts} reverse: true) {
pageInfo {
hasNextPage
hasPreviousPage
}
 
edges {
cursor
node {
namespace
key
value
}
}
}
}
} `
lguerra10
Excursionist
14 0 10

Sorry mistake in prior posting

 

first api call should be

 

queryString = `
{

product(id: "gid://shopify/Product/${productId}") {
 
title
metafields(first: ${numProducts} after: "${cursor}" reverse: true) {
pageInfo {
hasNextPage
hasPreviousPage
}
 
edges {
cursor
node {
namespace
key
value
}
}
}
}
 
 
} `