-
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
Aliasing id field prevents proper caching #10599
Comments
Thanks for the reproductions! If you add a const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Person: {
keyFields: ["id"]
}
}
}),
link
}); does that give the behavior you expected? |
It looks like adding that manually results in the query being normalized. The generated cache key is a little bit different though Is there a reason why that needs to be added manually versus just making that the default behavior? I could see a lot of folks aliasing their id fields and not even realizing their cache is no longer working exactly as they expected. If for some reason this shouldn't be done by default, is there a way to configure the cache such that this is the default caching policy unless indicated otherwise? |
The function that automatically generates IDs like These behaviors could/should change in Apollo Client v4 (the next major version), but for now you can implement your own version of the new InMemoryCache({
dataIdFromObject(object, context) {
if (object.__typename === "Person") {
return ["id"];
}
return defaultDataIdFromObject(object, context);
}
}) Returning You could add more |
Thank you for the detailed explanation! Makes perfect sense, thank you! |
I'm closing this here since it seems that all questions have been answered :) |
This issue got me thinking about whether it might be possible to configure a blanket The approach I had in mind turned out to be slightly broken, but the bug was fairly shallow, and should be fixed in Apollo Client v3.8, once this PR is merged: #10633 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Issue Description
When aliasing an
id
field like so:The query ends up not being normalized in the cache nor are there individual cache entries for each
Person
entity.This appears to be different behavior than expected given the
id
field still appears in the cache unaliased. Also from reading other issues such as apollographql/apollo-feature-requests#363 it appears as though the caching should ignore the alias.Link to Reproduction
https://codesandbox.io/s/apollo-cache-with-id-alias-x0yhjy?file=/src/index.jsx
Reproduction Steps
As you can see rendered, the
people
query has not been normalized and there are no cache entries forPerson
.Compare this to a version of the repro with no alias for the
id
field here: https://codesandbox.io/s/apollo-cache-with-no-id-alias-e8hyf5?file=/src/index.jsx. The query has been normalized as expected and there are separate cache entries for eachPerson
entity.The text was updated successfully, but these errors were encountered: