graphql - 如何使用 apollo-server 进行缓存
问题描述
https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Implementing-your-own-cache-backend上的 apollo 基本示例,他们声明执行 redis 缓存非常简单:
const { RedisCache } = require('apollo-server-cache-redis');
const server = new ApolloServer({
typeDefs,
resolvers,
cache: new RedisCache({
host: 'redis-server',
// Options are passed through to the Redis client
}),
dataSources: () => ({
moviesAPI: new MoviesAPI(),
}),
});
当我查看非redis的示例时,它指出它是一个简单{ get, set }
的缓存。这意味着我理论上应该能够做到。
cache : {
get : function() {
console.log("GET!");
},
set : function() {
console.log("SET!");
}
}
无论我尝试什么,当我使用 apollo-server 原生提供的 graphQL explorer 时,我的缓存函数都不会被调用。
我已经尝试过使用cacheControl : true
和cacheControl
设置,就像它在https://medium.com/brikl-engineering/serverless-graphql-cached-in-redis-with-apollo-server-2-0-f491695cac7f中一样。没有什么。
有没有一个例子说明如何在 Apollo 中实现不使用付费 Apollo Engine 系统的基本缓存?
解决方案
您应该能够通过实现自己的接口来使用 NPM 包“apollo-server-caching”。请参阅实现您自己的缓存,其中提供了一个示例。
推荐阅读
- php - 如何从表单获取值到我的 php 段?我的 php 段无法识别提交按钮
- reactjs - 当您在本地运行开发 API 时,在开发期间设置 API 引用的正确方法是什么?
- scala - 哪个更好:var 与 mutable.ListMap 或 var 与 immutable.ListMap?
- flutter - 如何在 Flutter 中使搜索结果可点击
- c# - 如何将基础对象的 List<> 继承为派生对象的 List<>?
- javascript - 试图理解对象映射的 reduce 函数?
- ruby - 如何测试 DelayedJob 是否正在调用另一个类的方法?
- node.js - toObjectId 用于查找和聚合的字符串数组
- c# - 对 IDisposable 的弱引用
- activemq - 如何控制 Hub/Spoke 拓扑中的帧大小