Search code examples
reactjsgraphqlapolloapollo-clientreact-apollo

How to handle generic pagination after updateQuery from fetchMore is deprecated


I'm currently using apollo graphql client for fetching all data from the backend. My react app has many pages that require fetching paginated lists of things and so I relied on the (now marked deprecated) updateQuery function inside the fetchMore api to handle the pagination. I made a helper function where you pass in the useQuery hook results that will handle the fetchMore/updateQuery stuff for you so it can append the next page of results to the existing list. This makes it easier to consume and add new paginated lists in the future when we want to make more queries for listed data. UpdateQuery docs: https://www.apollographql.com/docs/react/v2/data/pagination/#offset-based

const fetchNextPage = React.useCallback((variables: <GenericType>) => {
  ...
        // fetch the next page:
        fetchMore({
            variables: {
                ...variables,
            },
            updateQuery: (prev, { fetchMoreResult }) => {
                 prev.concat(fetchMoreResult);
            }),
...
return fetchNextPage // exported out for consumption

However, looking at the docs and online suggestions, it seems after updateQuery gets deprecated, the only way to do pagination moving forward will be to define a field policy and use the merge function in the InMemoryCache. https://www.apollographql.com/docs/react/pagination/core-api/#defining-a-field-policy

While I can do this, the amount of work needed to update every single existing query for listed items (let alone the boilerplate code needed to add a new list query) is rather tedious and unnecessary. Is it possible to define a generic way of doing these fetchMore paginations without having to keep making a field policy for every type we care about?

Thanks in advance!


Solution

  • maybe it will help someone:

    Remove nagging deprecation warning about passing an options.updateQuery function to fetchMore.

    updateQuery will be supported