Cache management helps store data temporarily to make fetching faster. It reduces waiting time when you ask for the same data again.
Cache management in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
cache: {
keyArgs: [String],
merge(existing, incoming) {
return mergedData;
},
read(existing) {
return data;
},
maxAge: Number
}keyArgs defines which query arguments are used to identify cached data.
merge controls how new data is combined with existing cached data.
cache: {
keyArgs: false,
merge(existing, incoming) {
return [...(existing || []), ...incoming];
}
}cache: {
keyArgs: ['id'],
read(existing) {
return existing;
}
}cache: {
maxAge: 60000
}This example shows a simple cache configuration for user data. It merges new user data into the cache and reads it back. The maxAge sets cache expiry time (not used in this simple demo).
const cacheConfig = { keyArgs: ['userId'], merge(existing = [], incoming) { return [...existing, ...incoming]; }, read(existing) { return existing; }, maxAge: 300000 }; // Simulate cache usage let cache = []; // Incoming data from server const incomingData = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; // Merge incoming data into cache cache = cacheConfig.merge(cache, incomingData); // Read data from cache const cachedData = cacheConfig.read(cache); console.log(cachedData);
Cache keys help identify which data belongs where in the cache.
Always think about how fresh your data needs to be when setting cache expiry.
Merging data properly avoids losing information when new data arrives.
Cache management stores data temporarily to speed up repeated requests.
Use key arguments to control how data is stored and retrieved.
Set expiry times to keep data fresh and avoid stale information.
Practice
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 AQuick Check:
Cache speeds up repeated requests = A [OK]
- Thinking cache stores data permanently
- Confusing cache with encryption
- Assuming cache deletes data immediately
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 AQuick Check:
Cache key argument needs quotes = A [OK]
- Omitting quotes around key name
- Using wrong directive name like @cache
- Passing key without key: label
query { product(id: 5) @cacheControl(maxAge: 60) { name price } }What happens if you request the same product again within 30 seconds?
Solution
Step 1: Understand maxAge cache expiry
maxAge: 60 means cache is valid for 60 seconds after storing data.Step 2: Check request timing
Request within 30 seconds is before expiry, so cached data is used.Final Answer:
The cached data is returned without fetching from the server -> Option DQuick Check:
Request before maxAge returns cache = C [OK]
- Assuming cache expires immediately
- Thinking server always refetches
- Confusing maxAge with minimum age
query { user(id: 10) @cacheControl(maxAge: "thirty") { name email } }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 CQuick Check:
maxAge needs number, not string = B [OK]
- Using string instead of number for maxAge
- Confusing directive names
- Assuming id type causes cache error
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 BQuick Check:
Cache by unique ID key = D [OK]
- Caching entire list as one entry
- Relying only on global expiry without keys
- Disabling cache unnecessarily
