When I make call apolloClient.watch() using the AllPostsQuery(), I get a RecordSet with Record keys that is prefixed with #QUERY_ROOT. Then when I call apolloClient.perform(), using UpvotePostMutation(postId: postId), I get a RecordSet with Record keys that is prefixed with #MUTATION_ROOT. Since the keys don't match, the cache does not get updated. I tried this with both the _SQLiteNormalizedCache_ and the _InMemoryNormalizedCache_.
RecordSet from call to watch().
â–¿ [#QUERY_ROOT.posts.0 -> ["title": Introduction to GraphQL, "__typename": Post, "id": 1, "votes": 978, "author": -> #QUERY_ROOT.posts.0.author], #QUERY_ROOT -> ["posts": [-> #QUERY_ROOT.posts.0, -> #QUERY_ROOT.posts.1, -> #QUERY_ROOT.posts.2]], #QUERY_ROOT.posts.1 -> ["title": GraphQL Rocks, "__typename": Post, "id": 2, "votes": 660, "author": -> #QUERY_ROOT.posts.1.author], #QUERY_ROOT.posts.0.author -> ["firstName": Tom, "lastName": Coleman, "__typename": Author], #QUERY_ROOT.posts.2.author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author], #QUERY_ROOT.posts.2 -> ["title": Advanced GraphQL, "__typename": Post, "id": 3, "votes": 746, "author": -> #QUERY_ROOT.posts.2.author], #QUERY_ROOT.posts.1.author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author]]
RecordSet from call to perform().
â–¿ [#MUTATION_ROOT.upvotePost(postId:3) -> ["title": Advanced GraphQL, "__typename": Post, "id": 3, "votes": 747, "author": -> #MUTATION_ROOT.upvotePost(postId:3).author], #MUTATION_ROOT -> ["upvotePost(postId:3)": -> #MUTATION_ROOT.upvotePost(postId:3)], #MUTATION_ROOT.upvotePost(postId:3).author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author]]
Note how the keys from the mutation does not match the existing query keys. Any idea why I am getting these Record keys?
RecordSet from call to watch()
â–¿ [#1.author -> ["firstName": Tom, "lastName": Coleman, "__typename": Author], #2 -> ["title": GraphQL Rocks, "__typename": Post, "id": 2, "votes": 660, "author": -> #2.author], #1 -> ["title": Introduction to GraphQL, "__typename": Post, "id": 1, "votes": 978, "author": -> #1.author], #QUERY_ROOT -> ["posts": [-> #1, -> #2, -> #3]], #3 -> ["title": Advanced GraphQL, "__typename": Post, "id": 3, "votes": 744, "author": -> #3.author], #2.author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author], #3.author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author]]
RecordSet from call to perform()
â–¿ [#3 -> ["title": Advanced GraphQL, "__typename": Post, "id": 3, "votes": 745, "author": -> #3.author], #MUTATION_ROOT -> ["upvotePost(postId:3)": -> #3], #3.author -> ["firstName": Sashko, "lastName": Stubailo, "__typename": Author]]
If you want to use the id field as the cache key, you'll have to configure the client to do so:
apolloClient.cacheKeyForObject = { $0["id"] }
Note that this assumes id is unique across types. If that isn't the case, you may want to add __typename to the key:
apolloClient.cacheKeyForObject = { [$0["__typename"], $0["id"]] }
Thank you for the reminder. apolloClient.cacheKeyForObject was what I was missing.
Error at path "allMessageConnection": missingValue getting this issue while creating the new message
In AWs app sync
Most helpful comment
If you want to use the
idfield as the cache key, you'll have to configure the client to do so:Note that this assumes
idis unique across types. If that isn't the case, you may want to add__typenameto the key: