Consider a GraphQL client cache that stores query results keyed by query and variables. If you run the following query twice with different variables, what will be the cache content after both queries?
query GetUser($id: ID!) {
user(id: $id) {
id
name
}
}First run with {"id": "1"}, second run with {"id": "2"}.
Think about how cache keys include variables to distinguish query results.
The cache stores results separately for each unique set of variables. So after running the query twice with different id values, the cache holds two entries keyed by the query and variables.
Which cache invalidation strategy is best suited for a GraphQL client cache when a mutation updates a user's name?
Think about minimizing unnecessary data loss while keeping cache fresh.
Evicting only the cache entry for the updated user keeps the cache mostly intact and ensures the updated data is fetched fresh next time.
Given this mutation and cache update snippet, which option correctly updates the cache to reflect the new user name?
mutation UpdateUserName($id: ID!, $name: String!) {
updateUser(id: $id, name: $name) {
id
name
}
}Cache update function:
cache.modify({
id: cache.identify({ __typename: 'User', id: $id }),
fields: {
name() {
return $name;
}
}
});Check the signature of the fields functions in cache.modify.
The fields functions receive the existing field value as an argument and should return the new value. The code misses the argument, so it won't update correctly.
After running a query, reading from the cache returns null unexpectedly. Which of the following is the most likely cause?
Think about how cache keys are constructed with query and variables.
If the variables used to read from the cache differ from those used to write, the cache key won't match and the read returns null.
You have a nested query fetching a user and their posts. After adding a new post via mutation, which cache update approach is most efficient to reflect the new post in the user's posts list without refetching the entire query?
query GetUserWithPosts($id: ID!) {
user(id: $id) {
id
name
posts {
id
title
}
}
}Think about updating only the changed part of the cache efficiently.
Using cache.modify to append the new post to the existing posts array updates the cache efficiently without refetching or overwriting unrelated data.