https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Implementing-your-own-cache-backend处的阿波罗基本示例 但是当我尝试时,它失败了。
const typeDefs = gql`
type Launch{
id: ID!
site: String
}
type Query {
launches: [Launch]! @cacheControl(maxAge: 6000)
}
`;
const resolvers = {
Query: {
launches: async (_, __, { dataSources }) => {
return dataSources.launchAPI.getAllLaunches();
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
tracing: true,
cacheControl: {
defaultMaxAge: 5,
stripFormattedExtensions: false,
calculateCacheControlHeaders: false,
},
cache: new RedisCache({
host: '127.0.0.1',
port: '6379',
connectTimeout: 5000,
reconnectOnError: function (err) {
console.log('Reconnect on error', err);
var targetError = 'READONLY';
if (err.message.slice(0, targetError.length) === targetError) {
// Only reconnect when the error starts with "READONLY"
return true;
}
},
retryStrategy: function (times) {
console.log('Redis Retry', times);
if (times >= 3) {
return undefined;
}
var delay = Math.min(times * 50, 2000);
return delay;
},
socket_keepalive: false,
// Options are passed through to the Redis client
}),
dataSources: () => {
return {
launchAPI: new LaunchAPI(),
};
},
});
server.listen().then(({ url }) => {
console.log(`? Server ready at ${url}`);
});
无论我尝试什么,@cacheControl似乎都无法缓存。如何缓存? 有一个简单的示例说明如何在Apollo Server中实现基本缓存吗?