I have two simple queries:
query GerMenuA {
menus {
menuA {
items {
label
}
}
}
}
query GerMenuB {
menus {
menuB {
items {
label
}
}
}
}
But in the console I see a warning:
Cache data may be lost when replacing the menus field of a Query object.
existing: {"__typename":"Menu","menuA":[{...}]}
incoming: {"__typename":"Menu","menuB":[{...}]}
Is there a way just do not merge them and remove the warning? Because if specify in typePolicies
Menu: { merge: true }
or
Menu: { merge: false }
it is not what I want, because these are different data and these two queues do not need to be merged in any way. Also, I don't have an id field and keyFields will not work for this case, because labels could be the same for both menus
So your question is hard to answer without your schema typedefs. But let's suppose it's something like this:
The warning is essentially saying that without any
keyArgsApollo Cache has no way to normalize themenusQuery. Keep in mind from their standpoint- you have a singlemenusroot query. (i.e.GerMenuAandGerMenuBare client side queries not root queries).(Side note- since you don't have
idfields see disabling normalization.)Option 1: Separate Your Queries
Apollo cache will now store
menuAandmenuBas separate queries. If you want to be safe you can set your type policies:keyFields: falsetells AC to storeMenuunder it's parent query.keyArgs: falsesays that bothmenuAandmenuBare singleton queries. Cache policy will default tomerge: falseso that existing data will be replaced by incoming.Option 2: Define Query Parameter
In this option, you add a
nameparameter to yourmenusquery:Apollo cache will now store
menus:{name:menuA}andmenus:{name:menuB}as separate cache objects. Again, if you want to be safe you can set your type policies:Again, the cache policy will default to
merge: falseso that existing data will be replaced by incoming.Option 3: Define Merge Policy
We've covered why the existing schema is confusing to Apollo Cache. But if you're really set on it, the remaining thing to do is define a merge policy:
merge: truetells Apollo Cache to merge the results ofGerMenuAandGerMenuBinto a singleMenuscache object with bothmenuAandmenuBproperties. Without it each time you ran a query you'd blow away the results of the previous query.