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
Cache Management with GraphQL
📖 Scenario: You are building a simple GraphQL API for a bookstore. To improve performance, you want to manage a cache of book data so that repeated requests for the same book do not always hit the database.
🎯 Goal: Create a basic cache management system using a dictionary to store book data keyed by book ID. You will set up the initial cache, configure a cache size limit, implement logic to add books to the cache, and finally complete the cache management by removing the oldest cached book when the cache exceeds the limit.
📋 What You'll Learn
Create a dictionary called cache with initial book entries.
Add a variable called cache_limit to set the maximum number of books in the cache.
Write a function called add_to_cache that adds a book to the cache and removes the oldest book if the cache size exceeds cache_limit.
Complete the cache management by updating the cache dictionary correctly when adding new books.
💡 Why This Matters
🌍 Real World
Cache management is important in APIs and databases to speed up data retrieval and reduce load on the main database.
Create a dictionary called cache with these exact entries: "book1": "GraphQL Basics", "book2": "Advanced GraphQL", and "book3": "GraphQL in Practice".
GraphQL
Hint
Use curly braces {} to create a dictionary and add the three key-value pairs exactly as shown.
2
Add a cache size limit variable
Add a variable called cache_limit and set it to 3 to limit the cache size to three books.
GraphQL
Hint
Just create a variable named cache_limit and assign it the value 3.
3
Write the function to add books to the cache
Write a function called add_to_cache that takes book_id and book_title as parameters. It should add the book to the cache dictionary. If the cache size exceeds cache_limit, remove the oldest book (the first key in the dictionary).
GraphQL
Hint
Use len(cache) to check size and next(iter(cache)) to get the oldest key.
4
Complete the cache management by updating the cache
Call the add_to_cache function to add a new book with book_id"book4" and book_title"Mastering GraphQL". This will update the cache dictionary and remove the oldest book if needed.
GraphQL
Hint
Call the function with the exact arguments to add the new book.
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
Step 1: Understand cache management purpose
Cache management is used to store data temporarily to speed up access.
Step 2: Compare options with cache purpose
Only To temporarily store data for faster repeated requests matches the temporary storage for faster repeated requests.
Final Answer:
To temporarily store data for faster repeated requests -> Option A
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
Step 1: Identify correct syntax for cache key argument
The cache key argument uses @cacheKey with a key string in quotes.
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.
Final Answer:
query { user(id: 1) @cacheKey(key: "id") { name } -> Option A
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:
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
Step 1: Check maxAge argument type
maxAge expects a numeric value representing seconds, not a string.
Step 2: Analyze the given value
"thirty" is a string, causing a type error in cacheControl directive.
Final Answer:
maxAge value must be a number, not a string -> Option C
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
Step 1: Understand caching by unique keys
Caching each post separately requires using a cache key based on post ID.
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.
Final Answer:
Use a cache key argument with the post ID to store each post individually -> Option B
Quick Check:
Cache by unique ID key = D [OK]
Hint: Cache items individually using unique keys [OK]