TanStackT
TanStack3mo ago
3 replies
popular-magenta

How to return extra pagination info from queryFn in TanStack Table?

My API returns something like this:

{
  data: object[]
  pagination_info: { total_count: number }
}


But queryFn in queryCollectionOptions only allows returning an array of objects.

so my hack is for now adding the count to every object:

queryFn: async (ctx) => {
  const parsed = parseLoadSubsetOptions(ctx.meta?.loadSubsetOptions)
  const { data } = await client.v1.getApiContacts({
    ...baseParams,
    offset: 0,
    limit: parsed.limit,
    sort_field: parsed.sorts[0]?.field[0] as string,
    sort_direction: parsed.sorts[0]?.direction,
  }, { signal: ctx.signal })

  return (
    data.response?.data?.map((contact) => ({
      ...contact,
      total_count: data.response?.pagination?.total_count,
    })) ?? []
  )
}


This works, but it feels hacky since I’m duplicating total_count into every item.
Is there a cleaner way to pass back both the data array and the pagination info to TanStack’s queryFn?

@Kyle Mathews maybe you have an idea here since i see you are involved with the pagination stuff ? thanks in advance for the help 🙂
Was this page helpful?