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.

Error on query cached and copied onto next query

See original GitHub issue

Intended outcome

We are using @apollo/client@3.2.4 and React

We have a query that takes variables that we are calling using the useQuery hook in our component

const { loading, error, data } = useQuery(QUERY, {
    variables: { variable1 },
  });

variable1 defaults to value1 which returns correct data from the server. This is cached as expected in the client.

{
    "data":{"array1":[{ "field1": "value1" }]}
}

The values of loading, error and data are as follows

{ loading: true, error: undefined, data: undefined }
{ loading: false, error: undefined, data: { array1:[{ field1: "value1" }]} }

We change the value of variable1 via a dropdown to value2 This rerenders the component with the value2 in variable1

The server returns an error response when it receives value2

{
    "errors":[{"message":"GraphQL Error."}],
    "data":{"array1":nulll}
}

The values of loading, error and data are as follows

{ loading: true, error: undefined, data: undefined }
{ loading: false, error: "GraphQL Error.", data: undefined }

We change the value of variable1 via a dropdown back to value1 This rerenders the component with the value1 in variable1

We expect the cached result from previous to be returned

{ loading: false, error: undefined, data: { array1:[{ field1: "value1" }]} }

Actual outcome

The cached result is returned, however the errors from the previous query are also returned, which confuses the rendering logic

{ loading: false, error: "GraphQL Error.", data: { array1:[{ field1: "value1" }]} }

Notes

This only happens when the third call is cached. If it goes through to the server it works as expected. This only happens if the most recent last call resulted in an error. I have put together a potential fix in a PR https://github.com/apollographql/apollo-client/pull/7163

Versions

System: OS: macOS 10.15.6 Binaries: Node: 13.12.0 Yarn: 1.22.5 npm: 6.14.4 Browsers: Chrome: 86.0.4240.80 Edge: 85.0.564.60 Firefox: 80.0.1 Safari: 13.1.2

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
rettgerstcommented, Jan 15, 2021

okay, here is my simple reproduction app: https://codesandbox.io/s/hopeful-meitner-hmllm?file=/src/App.tsx

leveraging the public graphql api from AniList, the app shows information about anime characters. one of the options is Katara from ATLA, which will produce an error because she is not in the AniList database.

to reproduce the issue - click on the first two options to prime the in-memory cache, then click on the Katara button to issue a request which will error. Then click on one of the other characters again. The data for the selected character is retrieved from cache and displayed onscreen, however the error from the previous request remains.

0reactions
ludwigbacklundcommented, Jan 19, 2021

@ShuPink I doubt it is by design as it only propagates the previous requests error onto the request.

Our workaround currently is to do the following

if (error && !data && !loading) {
 // Render error state
}

The only problem with this approach is that if the errorPolicy is ‘all’ it can make it quite difficult to tell the difference between error and non error states.

It works for now however.

We have the same problem and use this workaround for now. Our problem exactly mirrors the one in the reproduction app @rettgerst posted above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
If you include a field in query but don't include a value for it in data , the field's current cached value is...
Read more >
Using cached query results | BigQuery - Google Cloud
BigQuery writes all query results to a table. The table is either explicitly identified by the user (a destination table), or it is...
Read more >
React Query Error Handling | TkDodo's blog
React Query needs a rejected Promise in order to handle errors ... it in the next render cycle so that the Error Boundary...
Read more >
SELECT + INSERT + Query Cache = MySQL lock up
Try to reduce the query cache size significantly. 1G is probably too big. Start with 16M or 32M and adjust the query_cache_limit accordingly ......
Read more >
DAX and DynamoDB consistency models - AWS Documentation
Every DAX cluster has two distinct caches—an item cache and a query cache. ... the item is replicated to all the other nodes...
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