Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypePolicies merge/keyArgs declaration blocking results updates afters secondary fetch of useQuery with network-only fetchPolicy declared #9618

Open
nikse opened this issue Apr 22, 2022 · 1 comment
Labels

Comments

@nikse
Copy link

nikse commented Apr 22, 2022

Hello,

I think this is a bug w/ the apollo memory cache sort of similar to #9543, but the opposite is happening. I have a paginated useQuery via typePolicies with a network-only fetchPolicy + a cache-first nextFetchPolicy that is always returning cached data on secondary calls of the base page query.

i.e. component A spawns with a fresh new render cycle and calls the collection query getting fresh updated results, some procedures are then run and the data in the collection is updated in the database, then component B opens and runs a new version of the same collection query returning the exact same results as the first run in the data returned to the useQuery even though the network tab is showing the correct data from the execution of the query.

My guess is that the context.overwrite is false in this scenario and so the cached results are always being returned. I'm currently getting around this by skipping the initial fetch and calling refetch to get the most up to date data.

My useQuery call looks like this

    fetchPolicy: 'network-only',
    nextFetchPolicy: 'cache-first',
    variables: { id }
  })

with a paginated query looking like refetch({ variables: { after } })

and our typePolicies like

const typePolicies: TypePolicies = {
  Parent: { // parent that query lives under
    fields: {
      connectionQuery: { // connection query that returns that data we're interested in
        keyArgs: allArgsExcept(['after']),
        merge: mergeByKey('edges', 'cursor')
      }
    }
  }
}

One thing to note is that the data being returned in the connection looks something like

pageInfo: [PageInfo]
edges: [Edge]
nodes: [Node]
count: [Int]

and we are merging either edges or nodes

Intended outcome:

A new run of a useQuery with a fetchPolicy of network-only should always return the fetched data

Actual outcome:

The cached data is returned, probably due to the fetch policy/pagination code holding the results in cache so that queries with new args that aren't in the keyArgs array can be merged

How to reproduce the issue:

Versions

@nikse
Copy link
Author

nikse commented Apr 25, 2022

Digging into this issue a little more and still not sure if it's a bug or not because we can actually handle it by checking if our pagination cursor is undefined (aka fetching from the start) and then always returning the incoming results in that case. The unusual part to me is that the existing data is still there in this case, but maybe that's to be expected. Let me know, and happy to close this out if it's all expected behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants