如何在 apollo-server-hapi graphql 上实现缓存

hbi*_*uni 2 caching hapijs graphql apollo-server

我有graphql apollo-server-hapi。我尝试添加缓存控制,如下所示:

const graphqlOptions = {
  schema,
  tracing: true,
  cacheControl: true,
};
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在架构基础上添加缓存选项时:

type Author @cacheControl(maxAge: 60) {
  id: Int
  firstName: String
  lastName: String
  posts: [Post]
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

Error: Unknown directive "cacheControl".
Run Code Online (Sandbox Code Playgroud)

你能帮忙吗,在架构上应用缓存控制的正确方法是什么?

我按照下面的说明进行操作,但似乎不起作用。

阿波罗缓存控制

hbi*_*uni 11

在了解了有关 apollo graphql 缓存的更多信息后,基本上,问题出在makeExecutableSchemafrom apollo-server-hapi,没有包含指令 for @cacheControl,因此要使其工作,我们只需要将我们自己的@cacheControl指令定义到 graphql 文件中,如下所示:

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

directive @cacheControl (
  maxAge: Int
  scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE

type Author @cacheControl(maxAge: 60) {
  id: Int
  firstName: String
  lastName: String
  posts: [Post]
}
Run Code Online (Sandbox Code Playgroud)

  • 与 `apollo-server-lambda` 的情况相同。谢谢! (3认同)