0
0
GraphQLquery~20 mins

Cache management in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Cache Behavior with Query Variables

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"}.

A{"GetUser:{id:1}": {"user": {"id": "1", "name": "Alice"}}, "GetUser:{id:2}": {"user": {"id": "2", "name": "Bob"}}}
B{"GetUser": {"user": {"id": "2", "name": "Bob"}}}
C{"GetUser:{id:1}": {"user": {"id": "1", "name": "Alice"}}}
D{}
Attempts:
2 left
💡 Hint

Think about how cache keys include variables to distinguish query results.

🧠 Conceptual
intermediate
2:00remaining
Cache Invalidation Strategy

Which cache invalidation strategy is best suited for a GraphQL client cache when a mutation updates a user's name?

AEvict only the cache entry for the updated user by its ID
BClear the entire cache to avoid stale data
CDo nothing and rely on automatic cache expiration
DEvict unrelated queries to force full refetch
Attempts:
2 left
💡 Hint

Think about minimizing unnecessary data loss while keeping cache fresh.

📝 Syntax
advanced
2:00remaining
Correct Cache Update After Mutation

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;
    }
  }
});
ACorrect as is, updates the 'name' field in cache for the user
BShould use cache.writeFragment instead of cache.modify
Ccache.modify cannot update scalar fields like 'name'
DThe 'fields' function should accept existing value as argument and return new value
Attempts:
2 left
💡 Hint

Check the signature of the fields functions in cache.modify.

🔧 Debug
advanced
2:00remaining
Why Cache Read Returns Null?

After running a query, reading from the cache returns null unexpectedly. Which of the following is the most likely cause?

AThe cache is corrupted and needs to be reset
BThe cache key does not match because variables differ from the query used to read
CThe query has a syntax error causing cache read failure
DThe server returned null data, so cache stores null
Attempts:
2 left
💡 Hint

Think about how cache keys are constructed with query and variables.

optimization
expert
3:00remaining
Optimizing Cache Updates for Nested Queries

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
    }
  }
}
ADo nothing and rely on polling to update the cache
BEvict the entire user cache entry to force refetch
CUse cache.modify to append the new post to the user's posts field
DWrite the entire query result again with cache.writeQuery
Attempts:
2 left
💡 Hint

Think about updating only the changed part of the cache efficiently.