I have some questions about how to fit apollo client cache together with a specific way we are organizing our queries.
So the way we organize our queries is that we group queries on the same resource under a single namespace. E.g. we have two resources, users and accounts, and our queries look like this.
type Query {
user: UserQueries
account: AccountQueries
}
type UserQueries {
all: [User!]!
byId(id: ID!): User
}
type AccountQueries {
all: [Account!]!
byId(id: ID!): Account
}
type User {...}
type Account {...}
Since neither Queries type provides an ID field, by default the response from a second query under UserQueries will replace the data from the first query in the cache, i.e. first I make an all query, and I'll get back some data that gets stored in the cache under the user field. And then if I make a byId query, the data in the cache under user will get replaced by the new data.
Some questions about this process:
- I noticed that even after the
alldata is replaced by thebyIddata, the previously fetched users (the normalized ones) are still available in the cache, when I thought they would be garbage collected since the references are lost. Does the garbage collector not garbage collect them immediately, but rather run on a certain cadence? Or am I completely misunderstanding how gc works? - Is there another way, other than generating a unique ID for the query types
user, to preserve both responses? - Is it even a good idea to namespace queries like this?
Thanks folks!