@hubspire/cache-directive
v1.2.9
Published
A hubspire package for apollo graphql server caching using directives.
Downloads
607
Keywords
Readme
Cache Directive
A cache directive package for apollo graphql server for cache setting and cache invalidation. This package will help you to manage query caching.
Installation
Cache Directive Package requires Node.js v16+ to run.
Install the dependencies and devDependencies and start the server.
cd cache-directive
npm i
npm run build
How to implement ?
import { CacheContext } from "@hubspire/cache-directive";
export interface MyContext {
cacheContext: CacheContext;
... // other context fields
}
import { CacheService, responseCachePlugin, cacheDirectiveTransformer } from "@hubspire/cache-directive";
import { makeExecutableSchema } from '@graphql-tools/schema';
const cacheService = await CacheService.start({
cache_prefix: "cache_prefix",
redis_host: String(process.env.REDIS_HOST),
redis_port: Number(process.env.REDIS_PORT),
});
const server = new ApolloServer<MyContext>({
schema: cacheDirectiveTransformer(makeExecutableSchema({
typeDefs,
resolvers
})),
plugins: [
responseCachePlugin<MyContext>()
],
context: async ({ req }) => ({
cacheContext: {
cache: cacheService,
sessionId: req?.headers?.authorization?.split(" ")[1] || null,
},
}),
});
directive @cacheSet(
type: String!
identifier: String!
maxAge: Int = 900 # apply 900 seconds as the efault expire time for cache
) on FIELD_DEFINITION
input CachePurgeInput {
types: [String!]!
identifier: String
}
directive @cachePurge(payloads: [CachePurgeInput!]!) on FIELD_DEFINITION
License
MIT
Free Software, Hell Yeah!