问题描述
我正在使用 Apollo Federation,但我有一个关于缓存的问题。
query GetPost {
post(id: "123") {
id
author {
id
name
}
}
}
发布是一种服务,作者是另一种服务。 “id”包含在 post 模型中,因为这是使用客户端 apollo 库时的预期。
现在,生成的对 post 服务的请求(由网关)非常简单,如下所示:
query {
post(id: "123") {
id
}
}
我每分钟都会收到数千个这样的信息,并希望将它们缓存起来。显然,通过 ID 获取帖子的 ID 永远不会改变,所以每次这样的请求都是一种浪费。
有没有办法在网关端缓存这个请求?这样它就不会向邮政服务发出请求。
解决方法
根据 Apollo docs,您可以通过扩展 RemoteGraphQLDataSource 类来自定义用于从实现服务中检索数据的提取器。自定义提取器可以是实现提取规范的任何函数,在本例中,我使用 node-fetch-cache 将查询持久化到网关上的磁盘。
const { ApolloGateway,RemoteGraphQLDataSource } = require("@apollo/gateway");
class CachingDataSource extends RemoteGraphQLDataSource {
fetcher = require('node-fetch-cache')('./locaCache');
}
const gateway = new ApolloGateway({
serviceList: [
{ name: "post",url: "http://posts.service" },{ name: "author",url: "http://author.service" },],buildService({ name,url }) {
if (url === "http://posts.service") {
return new CachingDataSource({ url,name });
} else {
return new RemoteGraphQLDataSource({
url,name
});
}
},});