I have service Test-Service which provides some data via graphql and Apollo Gateway. And I would like to cache requests on Apollo gateway, but it doesn’t work, I still see in logs of Test-Service that it receives requests. Here is code of Apollo Gateway
import {ApolloServer} from'@apollo/server'
import {ApolloGateway, IntrospectAndCompose} from '@apollo/gateway'
import {hiveApollo} from'@graphql-hive/client'
import {KeyvAdapter} from"@apollo/utils.keyvadapter"
import {ApolloServerPluginCacheControl} from"@apollo/server/plugin/cacheControl"
import {startStandaloneServer} from'@apollo/server/standalone'
// const Keyv = require("keyv");
import {InMemoryLRUCache} from "@apollo/utils.keyvaluecache"
import responseCachePlugin from '@apollo/server-plugin-response-cache';
const gateway = new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{name: 'test-service', url: 'http://test-service-svc/graphql'}
],
pollIntervalInMs: 5000
})
})
async function startApolloServer() {
const server = new ApolloServer({
gateway,
plugins: [
responseCachePlugin()
],
// cache: new KeyvAdapter(new Keyv({
// url: "redis://graphql-gateway-redis:6379",
// sentinels: [
// {host: "graphql-gateway-redis", port: 26379}
// ]
// })
// )
cache: new InMemoryLRUCache(),
cacheControl: {
defaultMaxAge: 50,
},
});
const {url} = await startStandaloneServer(server, {
listen: {port: 4000}
})
console.log(`🚀 Server ready at: ${url}`)
}
startApolloServer();
As you can see I also tried using redis. And here is example schema:
type Query {
getInfo(
userId: Int
): [Response]
getInfoCached(
userId: Int
): [Response] @cacheControl(maxAge: 30)
}
type Response @cacheControl(maxAge: 30) {
id: Int,
createdAt: DateTime, @cacheControl(maxAge: 10),
firstName: String
}