question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetchPolicy: cache-only return no errors when cached item is missing

See original GitHub issue

Intended outcome:

I’m using useQuery with the fetchPolicy: "cache-only" with an InMemoryCache that is primed during SSR and .restore on the client:

const { loading, data, error } = useQuery(QUERY, {
  fetchPolicy: "cache-only",
  errorPolicy: "all",
})

There are a lot of queries flying around that might invalidate data. When this happens, useQuery cannot refetch the query due to the cache policy being set to cache-only (which is good), and I expect an error to be set in the result. That way the error can be tracked since this is basically a coding error.

Actual outcome: Instead of an error being set in the result, there are warnings being logged to the console and the result is:

{ data: undefined, error: undefined, loading: false }

I need to now detect this special case and log a custom error myself.

Versions

  System:
    OS: macOS 10.15.7
  Binaries:
    Node: 14.15.1 - ~/.config/nodenv/versions/14.15.1/bin/node
    Yarn: 1.22.10 - ~/code/release-page/node_modules/.bin/yarn
    npm: 6.14.8 - ~/.config/nodenv/versions/14.15.1/bin/npm
  Browsers:
    Chrome: 87.0.4280.88
    Firefox: 84.0
    Safari: 14.0
  npmPackages:
    @apollo/client: ^3.2.5 => 3.3.2
    apollo: ^2.31.2 => 2.31.2
    apollo-link-persisted-queries: ^0.2.2 => 0.2.2

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:6

github_iconTop GitHub Comments

2reactions
jdalegonzalezcommented, Feb 13, 2021

@jlek This is probably too late to help you but here’s my tip…

Whenever this happens to me, it’s always because a query that doesn’t request a field populates the cache and a later query asks for another field and it’s missing. The apollo client very unhelpfully drops the error on the ground even though it knows what field is missing. The gross way to figure it out is to put a breakpoint in @apollo/client/core/ObservableQuery.js around line 85 (search for diff = this.queryInfo.getDiff). You will see the return of void 0 - meaning no data for you. The diff, though, knows which fields are missing and could spit them out as console.warn - it just doesn’t. Open up the diff in the watch window and you’re set.

OR… less gross… set “returnPartialData” on your query options. Dump the output to console and compare what comes back with what you queried for. The missing fields will be the ones that are the problem. Another query will have hit the same object without asking for some of the fields.

0reactions
jsutariacommented, Dec 16, 2022

Any updates here? Documentation says that the query should throw an error too. https://www.apollographql.com/docs/react/data/queries/#cache-only

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced topics on caching in Apollo Client
In cases like this, use the no-cache fetch policy: JavaScript. 1. const { loading, error, data } = useQuery(GET_DOGS, {. 2. fetchPolicy: "no-cache"....
Read more >
React Apollo: Understanding Fetch Policy with useQuery
A cache-only query throws an error if the cache does not contain data for all requested fields. cache-and-network, Apollo Client executes the ...
Read more >
Flutter: graphql_flutter FetchPolicy.noCache returns cached ...
Before adding any items to the favorites list for this new user, if I check their 'Favorites' summary page, which calls the ...
Read more >
Queries - Vue Apollo
This query would return a data object with an array of users with their id ... There is also an error Ref that...
Read more >
Tips and Tricks for working with Apollo Cache - Medium
When you make any graphQL query, by default Apollo caches the ... It constructs a unique identifier for each object returned from your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found