-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 read returning nonexistent reference returns {} instead of query result #6844
Comments
Often returning a dangling reference is fine, because it looks like missing data to the client, which can trigger a network request to fill in the missing data (depending on your However, if you want to avoid returning a dangling reference from a const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
personByIdWithTypePolicies: {
read: (q, { args, toReference, canRead }) => {
const ref = toReference({
__typename: "Person",
id: args.id
});
return canRead(ref) ? ref : q;
}
}
}
}
}
}),
link
}); Does that make sense / meet your needs? |
This looks like a helpful workaround, but I believe the behavior is incorrect. In this case, it is making a network request for the missing data, but still returning the dangling reference via |
@benjamn I'm a bit confused about the workaround. Should we always check |
I ran into this issue as well, and was able to resolve it with @benjamn's workaround. I saw that there was some discussion about checking canRead automatically in #6425 and I feel like if it isn't the default behavior, then apollo should at least provide a better debugging experience for when dangling references are returned. In my case the entire |
Intended outcome:
Was trying to follow the docs for cache redirects using field policy read functions. As an example:
Actual outcome:
When the given ID does not correspond to an object that exists, the result given by the cache is
{}
instead of falling back to the query result ofnull
.For the given query (
personById
andpersonByIdWithTypePolicies
are implemented exactly the same in the schema, but the latter uses the above type policy):The result is:
I would expect the last item to be
"missingPersonWithTypePolicies": null
, because that's what came back from the schema.How to reproduce the issue:
Here is a CodeSandbox repro containing and demonstrating the above examples: https://codesandbox.io/s/confident-swartz-psh7v
Versions
(This is from my local environment where I first detected the issue, not the CodeSandbox above. If it matters, I'm using Ubuntu within WSL 2 for Windows.)
The text was updated successfully, but these errors were encountered: