I am working with Shopify’s GraphQL API to paginate through all orders using a cursor-based
pagination approach. The query successfully fetches orders in batches, and I can see the correct endCursor
being returned and logged. However, my pagination is stuck in an infinite loop where it keeps fetching the same set of orders over and over again, even though I’m updating the endCursor
for each query.
Here’s my code for fetching orders:
async getAllOrders(session) {
const _client = new shopify.api.clients.Graphql({ session })
let hasNextPage = true
let endCursor = null // Initialize to null to fetch the first page
let allOrders = []
const query = `#graphql
query ($cursor: String) {
orders(first: 3, after: $cursor) {
edges {
node {
id
}
}
pageInfo {
hasNextPage
endCursor
}
}
}`
while (hasNextPage) {
try {
console.log('Using EndCursor for Query:', endCursor)
const data = await _client.query({
data: query,
variables: {
cursor: endCursor,
},
})
const orders = data.body.data.orders.edges.map((edge) => edge.node)
allOrders.push(...orders) // Store the fetched orders
await this._dao.addAllOrders(orders)
// Update pagination state
hasNextPage = data.body.data.orders.pageInfo.hasNextPage
endCursor = data.body.data.orders.pageInfo.endCursor // Update cursor here
// Log updated state after the query
console.log('Updated EndCursor for Next Query:', endCursor)
console.log('Has Next Page:', hasNextPage)
} catch (error) {
console.error('GraphQL Error Response:', error.response) // Log the full error response
if (error instanceof GraphqlQueryError) {
throw new Error(
`${error.message}\n${JSON.stringify(error.response, null, 2)}`
)
} else {
throw error
}
}
}
return allOrders
}
Here are the logs from one of the iterations:
As you can see, the endCursor
is fetched and updated correctly, but it seems like the same orders are being fetched again with the same endCursor
, causing an infinite loop.
Is there something wrong with how I am handling the pagination logic?
Why am I fetching the same orders repeatedly despite the endCursor
being updated?
Any help or insight would be appreciated!