Replies: 1 comment
-
Wrong repo? Also, assuming this is the wrong repo. If you do end up posting this somewhere else, please fix read up on and fix your markdown ❤️ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to use GQL server cache directly without relying on cacheControl object. In the documentation it is stated you can implement your own server side cache with ** KeyValueCache** interface this interface has basic operations for using the cache (get,set,delete), in this way i created my own redis cluster cache as the documentation showed the server is starting and working as expected also added logging to make sure the cache isn't causing problems with this interface. When i make a call for a method with this cache in the resolvers i get returned an error of cannot read properties of undefined clearly the cache isn't passed how it's supposed in the resolvers here is what i tried:
Server setup:
`async function startApolloServer(typeDefs, resolvers) {
const app = express();
const httpServer = http.createServer(app);
const Redis = require('ioredis');
const Keyv = require('keyv');
const KeyvRedis = require('@keyv/redis');
const { KeyvAdapter } = require('@apollo/utils.keyvadapter');
const serverOptions = {
typeDefs,
rejectUnauthorized: false,
resolvers,
plugins: [
responseCachePlugin.default({
sessionId: (requestContext) =>
getUserId(requestContext.request.http.headers.get('authorization')) ||
null,
}),
],
};
console.log('REDIS_HOST', process.env.REDIS_HOST);
if (process.env.REDIS_HOST) {
const cluster = new Redis.Cluster([
{ host: process.env.REDIS_HOST, port: process.env.REDIS_PORT },
]);
}
const server = new ApolloServer({
...serverOptions,
context: ({ req }) => {
return {
cache,
headers: req.headers,
// Add other context properties if needed
};
},
});
await server.start(serverOptions);
}`
And in resolvers i tried to define it:
` GetItem: async (
_,
args,
{ dataSources },
{ cache }
) => {
if (!cache) {
throw new Error('Cache not initialized');
}
OR
GetItem: async ( _, args, { dataSources, cache } )
In both cases the cache is undefined.Shouldn't we be able to acess the cache in resolvers ? If anybody has any idea please do tell. The documentation could be improved in this area where you have implemented your own backend cache and showing us how to use it, couldn't find much in 3 days of searching the internet.
reference: https://www.apollographql.com/docs/apollo-server/performance/cache-backends/#implementing-your-own-cache-backend
https://www.apollographql.com/docs/apollo-server/performance/cache-backends/#redis-cluster
Beta Was this translation helpful? Give feedback.
All reactions