Skip to content

[BUG] FetchMore + fetchPolicy: 'network-only' always triggers first page query #7307

Closed
@jgan42

Description

Hi,

Given the situation :

  • Pagination with merge function set in typePolicies from InMemoryCache
...
cache: new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        people: {
          keyArgs: false,
          merge: (existing, incoming, { args }) => {
            const offset = args?.skip || 0;
            const merged = existing ? existing.slice(0) : [];
            for (let i = 0; i < incoming.length; ++i) {
              merged[offset + i] = incoming[i];
            }
            return merged;
          }
        }
      }
    }
  }
}),
...
  • fetchPolicy set with 'network-only' or 'cache-and-network'
const { loading, data, fetchMore } = useQuery(ALL_PEOPLE, {
  variables: { skip: 0 },
  fetchPolicy: 'network-only'
});
  • Call fetchMore from useQuery
<button onClick={() => fetchMore({ variables: { skip: data?.people.length } })}>
  Next page
</button>

Bug : linked query is triggered twice

  • Once with correct pagination variables
  • Once with initial variables

Here a sandbox : https://codesandbox.io/s/black-snowflake-vdhwm

There are some logs showing the linked query fired twice (as the bug description) on "Next Page" click. It doesn't happen if fetchPolicy is unset.

Thanks for reading.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions