Bird
Raised Fist0
GraphQLquery~20 mins

Cache management in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of cache management in GraphQL?
easy
A. To temporarily store data for faster repeated requests
B. To permanently save all data in the database
C. To delete all data after each request
D. To encrypt data for security

Solution

  1. Step 1: Understand cache management purpose

    Cache management is used to store data temporarily to speed up access.
  2. Step 2: Compare options with cache purpose

    Only To temporarily store data for faster repeated requests matches the temporary storage for faster repeated requests.
  3. Final Answer:

    To temporarily store data for faster repeated requests -> Option A
  4. Quick Check:

    Cache speeds up repeated requests = A [OK]
Hint: Cache means temporary storage for speed [OK]
Common Mistakes:
  • Thinking cache stores data permanently
  • Confusing cache with encryption
  • Assuming cache deletes data immediately
2. Which of the following is the correct way to specify a cache key argument in a GraphQL query?
easy
A. query { user(id: 1) @cacheKey(key: "id") { name }
B. query { user(id: 1) @cache(key: "id") { name }
C. query { user(id: 1) @cacheKey(id) { name }
D. query { user(id: 1) @cacheKey(key: id) { name }

Solution

  1. Step 1: Identify correct syntax for cache key argument

    The cache key argument uses @cacheKey with a key string in quotes.
  2. Step 2: Check each option's syntax

    query { user(id: 1) @cacheKey(key: "id") { name } correctly uses @cacheKey(key: "id") with quotes around key name.
  3. Final Answer:

    query { user(id: 1) @cacheKey(key: "id") { name } -> Option A
  4. Quick Check:

    Cache key argument needs quotes = A [OK]
Hint: Cache keys need quotes around key name [OK]
Common Mistakes:
  • Omitting quotes around key name
  • Using wrong directive name like @cache
  • Passing key without key: label
3. Given the following GraphQL query with cache expiry set to 60 seconds:
query { product(id: 5) @cacheControl(maxAge: 60) { name price } }

What happens if you request the same product again within 30 seconds?
medium
A. An error occurs due to cache expiry mismatch
B. The server fetches fresh data ignoring the cache
C. The cache is cleared and data is refetched
D. The cached data is returned without fetching from the server

Solution

  1. Step 1: Understand maxAge cache expiry

    maxAge: 60 means cache is valid for 60 seconds after storing data.
  2. Step 2: Check request timing

    Request within 30 seconds is before expiry, so cached data is used.
  3. Final Answer:

    The cached data is returned without fetching from the server -> Option D
  4. Quick Check:

    Request before maxAge returns cache = C [OK]
Hint: Cache valid until maxAge seconds pass [OK]
Common Mistakes:
  • Assuming cache expires immediately
  • Thinking server always refetches
  • Confusing maxAge with minimum age
4. Identify the error in this GraphQL cache directive usage:
query { user(id: 10) @cacheControl(maxAge: "thirty") { name email } }
medium
A. The directive name should be @cacheKey, not @cacheControl
B. The user id must be a string, not a number
C. maxAge value must be a number, not a string
D. The query is missing a required fragment

Solution

  1. Step 1: Check maxAge argument type

    maxAge expects a numeric value representing seconds, not a string.
  2. Step 2: Analyze the given value

    "thirty" is a string, causing a type error in cacheControl directive.
  3. Final Answer:

    maxAge value must be a number, not a string -> Option C
  4. Quick Check:

    maxAge needs number, not string = B [OK]
Hint: maxAge must be numeric, no quotes [OK]
Common Mistakes:
  • Using string instead of number for maxAge
  • Confusing directive names
  • Assuming id type causes cache error
5. You want to cache a list of posts but ensure that each post is cached separately by its unique ID. Which cache management strategy should you use in your GraphQL schema?
hard
A. Cache the entire posts list as one entry without keys
B. Use a cache key argument with the post ID to store each post individually
C. Disable caching for posts to always fetch fresh data
D. Set a global cache expiry time for all posts together

Solution

  1. Step 1: Understand caching by unique keys

    Caching each post separately requires using a cache key based on post ID.
  2. Step 2: Evaluate options for separate caching

    Only Use a cache key argument with the post ID to store each post individually uses cache key argument to store posts individually by ID.
  3. Final Answer:

    Use a cache key argument with the post ID to store each post individually -> Option B
  4. Quick Check:

    Cache by unique ID key = D [OK]
Hint: Cache items individually using unique keys [OK]
Common Mistakes:
  • Caching entire list as one entry
  • Relying only on global expiry without keys
  • Disabling cache unnecessarily