Skip to content
Discussion options

You must be logged in to vote

By default, Got doesn't cache POST requests since they're typically not idempotent. For GraphQL queries, the simplest approach is to wrap your Got calls with a caching layer:

import got from 'got';
import Keyv from 'keyv';
import KeyvRedis from '@keyv/redis';

const cache = new Keyv({
	store: new KeyvRedis('redis://localhost:6379')
});

async function cachedGraphQL(url, query, variables = {}) {
	const cacheKey = JSON.stringify({ query, variables });
	
	// Check cache first
	const cached = await cache.get(cacheKey);
	if (cached) {
		return cached;
	}
	
	// Make request if not cached
	const response = await got.post(url, {
		json: { query, variables }
	}).json();
	
	// Cache for 5 minutes
	a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants