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
keyArgs
Apollo Cache has no way to normalize themenus
Query. Keep in mind from their standpoint- you have a singlemenus
root query. (i.e.GerMenuA
andGerMenuB
are client side queries not root queries).(Side note- since you don't have
id
fields see disabling normalization.)Option 1: Separate Your Queries
Apollo cache will now store
menuA
andmenuB
as separate queries. If you want to be safe you can set your type policies:keyFields: false
tells AC to storeMenu
under it's parent query.keyArgs: false
says that bothmenuA
andmenuB
are singleton queries. Cache policy will default tomerge: false
so that existing data will be replaced by incoming.Option 2: Define Query Parameter
In this option, you add a
name
parameter to yourmenus
query: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: false
so 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: true
tells Apollo Cache to merge the results ofGerMenuA
andGerMenuB
into a singleMenus
cache object with bothmenuA
andmenuB
properties. Without it each time you ran a query you'd blow away the results of the previous query.