Cache data may be lost when replacing the users field of a Query object.
Closed this issue · 2 comments
Cache data may be lost when replacing the users field of a Query object.
To address this problem (which is not a bug in Apollo Client), either ensure all objects of type UsersQuery have an ID or a custom merge function, or define a custom merge function for the Query.users field, so InMemoryCache can safely merge these objects:
existing: {"__typename":"UsersQuery","getById({"id":"2"})":null}
incoming: {"__typename":"UsersQuery","getById({"id":"3"})":null}
For more information about these options, please refer to the documentation:
- Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers
- Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects
This is a bug caused the name space setup on the graphql server in our example:
extend type Query {
"Returns the current user as defined by the authentication headers"
self: User
"Provides name spaced users functionality"
users: UsersQuery!
}
"Provides name spaced users functionality"
type UsersQuery {
"Returns the user record matching the provided id"
getById(id: ID!): User
}
extend type Mutation {
"Provides name spaced users functionality"
users: UsersMutations!
}
"Provides name spaced users functionality"
type UsersMutations {
"Saves the user and returns the updated copy"
saveUser(user: UserInput!): User
}
There's no way Apollo can clearly identify how to merge the name spaces. We need to explicit tell Apollo how to deal with it. In this case, it's just "merge all of it together, there is no unique id on this level."
This bug would not show in the code base as defined. It requires firing the getUserById functionality with two different sets of variables:
getUserById({ variables: { id: "2" } });
getUserById({ variables: { id: "3" } });