Skip to content

Instantly share code, notes, and snippets.

@krishnadeorai
Created September 7, 2022 11:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save krishnadeorai/e3e390c55737d2baff3afdba240d665e to your computer and use it in GitHub Desktop.
const { BaseRedisCache } = require('apollo-server-cache-redis');
const Redis = require('ioredis');
const apolloServerPluginResponseCache = require('apollo-server-plugin-response-cache')
const { REDIS_PORT, REDIS_HOST, REDIS_DB_APQ, REDIS_DB_CACHE, APQ_MAX_AGE, CACHE_MAX_AGE, GRAPHQL_PLAYGROUNDALWAYS, GRAPHQL_DEPTHLIMIT, GRAPHQL_AMOUNTLIMIT } = require("./custom")
const cache = new BaseRedisCache({
client: new Redis({
port: REDIS_PORT,
db: REDIS_DB_APQ,
host: REDIS_HOST
})
});
module.exports = {
graphql: {
config: {
endpoint: '/graphql',
shadowCRUD: true,
playgroundAlways: GRAPHQL_PLAYGROUNDALWAYS,
depthLimit: GRAPHQL_DEPTHLIMIT,
amountLimit: GRAPHQL_AMOUNTLIMIT,
federation: false,
apolloServer: {
tracing: false,
persistedQueries: {
ttl: APQ_MAX_AGE,
cache: cache,
},
cacheControl: { defaultMaxAge: CACHE_MAX_AGE },
cache: cache,
plugins: [apolloServerPluginResponseCache.default({
shouldReadFromCache,
shouldWriteToCache,
extraCacheKeyData,
sessionId,
}),
injectCacheControl()
]
},
},
},
};
async function sessionId(requestContext) {
// return a session ID here, if there is one for this request
return null
}
async function shouldReadFromCache(requestContext) {
// decide if we should write to the cache in this request
return true
}
async function shouldWriteToCache(requestContext) {
// decide if we should write to the cache in this request
return true
}
async function extraCacheKeyData(requestContext) {
// use this to create any extra data that can be used for the cache key
}
function injectCacheControl() {
return {
requestDidStart(requestContext) {
requestContext.overallCachePolicy.policyIfCacheable({ scope: 'PUBLIC', maxAge: CACHE_MAX_AGE });
requestContext.overallCachePolicy.scope = 'PUBLIC';
requestContext.overallCachePolicy.maxAge = CACHE_MAX_AGE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment