Apollo cache: reuse same derived data between different fields
See original GitHub issueI have an server schema like this:
User {
id
amount
}
Match {
id
date
probability
}
Then I need to compute derived data before render. Then I want to do something like this:
const typePolicies = {
Match {
fields: {
odd: {
read (_ , { readField }) {
const amountUser = cache.readFragment({
id: 'User:{}',
fragment: gql`
fragment amountUser on User {
amount
}`
});
const probabilityMatch = readField('probability');
const calc = getCalc (amountUser, probabilityMatch); // complex operation
const odd = getOdd (calc);
return odd;
}
} ,
maxValue: {
read (_ , { readField }) {
const amountUser = cache.readFragment({
id: 'User:{}',
fragment: gql`
fragment amountUser on User {
amount
}`
});
const probabilityMatch = readField('probability');
const calc = getCalc (amountUser, probabilityMatch); // complex operation
const maxValue = getMaxValue (calc);
return maxValue;
}
}
}
}
}
Then query like this:
query Match {
Match {
id
date
odd @client
maxValue @client
}
}
But I’m executing same complex operation twice. An idea would be return nested fields:
const typePolicies = {
Match {
fields: {
computed: {
read (_ , { readField }) {
const amountUser = cache.readFragment({
id: 'User:{}',
fragment: gql`
fragment amountUser on User {
amount
}`
});
const probabilityMatch = readField('probability');
const calc = getCalc (amountUser, probabilityMatch); // complex operation
const odd = getOdd (calc);
const maxValue = getMaxValue (calc);
return {
odd,
maxValue
}
}
}
}
}
}
}
Then query like this:
query Match {
Match {
id
date
computed @client {
odd
maxValue
}
}
}
But I’m creating an type computed
that I don’t want. Is there an optimal way to solve this?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Customizing the behavior of cached fields - Apollo GraphQL
You can customize how a particular field in your Apollo Client cache is read and written. To do so, you define a field...
Read more >Advanced topics on caching in Apollo Client
This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache. Sometimes you shouldn't use the cache for...
Read more >Interacting with cached data - Apollo GraphQL Docs
If your cache contains all of the data necessary to fulfill a specified query, readQuery returns a data object in the shape of...
Read more >Reading and writing data to the cache - Apollo GraphQL Docs
You can read and write cache data using GraphQL queries that are similar (or even identical) ... If your cache contains data for...
Read more >Customizing the behavior of cached fields - Client (React)
You can customize how a particular field in your Apollo Client cache is read and written. To do so, you define a field...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jdmoliner Two notes that I hope will get you unstuck, though I imagine you’ll have further questions:
readField
instead ofcache.readFragment
:storage
in theread
function options that you can use for caching:This
storage
object is private to this field within this specific object, but the samestorage
object is provided toread
andmerge
andcache.modify
functions, so (if you later want to invalidate thestorage.odd
cache) you can either define amerge
function that allows deletingstorage.odd
(perhaps a bit awkward for a client-only field), or callcache.modify
like so:Please let me know if I can clarify anything about these somewhat obscure corners of the type/field policies API.
@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
finalize @client
trick.