UNPKG

@graphprotocol/client-auto-pagination

Version:

`graph-client` implements automatic pagination using `first:` and `after:` filters of `graph-node`.

19 lines (18 loc) 821 B
import { isAsyncIterable, mapAsyncIterator } from "@graphql-tools/utils"; import { DEFAULT_OPTIONS, transformExecutionRequest, transformExecutionResponse } from "./shared.js"; export function useAutoPagination(options = DEFAULT_OPTIONS) { return { onSubgraphExecute({ executionRequest, setExecutionRequest }) { setExecutionRequest(transformExecutionRequest(executionRequest, options)); return ({ result, setResult }) => { if (isAsyncIterable(result)) { const iterator = result[Symbol.asyncIterator](); setResult(mapAsyncIterator(iterator, transformExecutionResponse)); } else { setResult(transformExecutionResponse(result)); } }; } }; }