-
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
Apollo cache: reuse same derived data between different fields #8052
Comments
@jdmoliner Two notes that I hope will get you unstuck, though I imagine you'll have further questions:
read(_, { readField, cache, toReference }) {
const userRef = toReference(cache.identify({ __typename: "User", ... }));
const amountUser = readField("amount", userRef);
...
}
read(_, { readField, cache, toReference, storage }) {
// Returned cached storage.odd value, if defined:
if (storage.odd !== void 0) return storage.odd;
const userRef = toReference(cache.identify({ __typename: "User", ... }));
const amountUser = readField("amount", userRef);
...
// Cache the final result:
return storage.odd = odd;
} This cache.modify({
id: cache.identify({ __typename: "Match", ... }),
fields: {
odd(value, { storage }) {
// Clear the cached storage.odd value:
delete storage.odd;
// Preserve any existing value:
return value;
},
},
}) Please let me know if I can clarify anything about these somewhat obscure corners of the type/field policies API. |
Argh, now that I suggested I'll have to keep thinking about the best way to invalidate |
@benjamn that's just the problem I have now, as I can't clear storage cache in typePolicies, when I try later to make a mutation or cache.modify in my App I get storage cache. Has something occurred to you? |
@josedavid2021 I'm thinking of introducing a new field policy function called new InMemoryCache({
typePolicies: {
SomeType: {
fields: {
someField: {
read(existing, { storage }) {
if ("cached" in storage) return storage.cached;
...
return storage.cached = computeResult(...);
},
// Called just before this field is evicted/deleted/removed from the cache:
finalize(existing, { storage }) {
delete storage.cached;
},
},
},
},
},
}) What do you think about that? |
@benjamn that would be great! |
As I described in this comment: #8052 (comment)
As I described in this comment, though I decided to call the new function `drop` instead of `finalize`: #8052 (comment)
@benjamn This is my solution until then:
Then query like this:
How about? |
@josedavid2021 That's very clever, and seems like it should work! However, I hope we can come up with something (along the lines of #8078) so you won't need that |
As I described in this comment, though I decided to call the new function `drop` instead of `finalize`: apollographql/apollo-client#8052 (comment)
I have an server schema like this:
Then I need to compute derived data before render. Then I want to do something like this:
Then query like this:
But I'm executing same complex operation twice. An idea would be return nested fields:
Then query like this:
But I'm creating an type
computed
that I don't want. Is there an optimal way to solve this?The text was updated successfully, but these errors were encountered: