ourzora / zdk

MIT License
107 stars 42 forks source link

Limit for events on token endpoint? #86

Closed webel closed 1 year ago

webel commented 1 year ago

In the graphql playground I can see that I can add pagination input for events when querying the token endpoint, how would I accomplish this with the zdk? At the moment I only receive 10 events back, and I was hoping to either be able to paginate them or just set a somewhat high limit.

bjfresh commented 1 year ago

@webel the response from ZDK has cursor-based pagination baked in, so you can fetch additional pages by incrementing with the cursor. There's a fairly robust working example up at https://github.com/ourzora/nouns-marketplace/blob/main/%40filter/hooks/useTokensQuery.ts, for reference. It uses the useSWRInfinite hook to manage refreshing, which is super flexible.

Pertinent bits:

async function getNFTs(query: TokensQueryArgs): Promise<GetNFTReturnType> {
  const resp = await zdk.tokens(query)
  const tokens = resp.tokens.nodes
    /* @ts-ignore */
    .map((token) => transformNFTZDK(token, { rawData: token }))
    .map(prepareJson)
  return {
    tokens,
    nextCursor: resp.tokens.pageInfo.endCursor,  // <-- PASS THIS INTO PAGINATION
  }
}

and then note how the cursor is passed in as the after value:

      pagination: {
        after: previousPageData?.nextCursor,
        limit: PAGE_SIZE,
      },
webel commented 1 year ago

Hm, that looks like the top-level pagination for the nft endpoint, I was wondering more if I could get the next cursor for one of the nested returns (events). I guess I might just have to call events endpoint directly?

bjfresh commented 1 year ago

Ah, yes, missed that. Sorry. Yes, targeting events separately would be the way to go here.