如何在 apollo-server-hapi graphql 上实现缓存
How to Implement caching on apollo-server-hapi graphql
我有 graphql apollo-server-hapi
。我尝试像下面这样添加缓存控制:
const graphqlOptions = {
schema,
tracing: true,
cacheControl: true,
};
但是当我尝试在模式基础上添加缓存选项时:
type Author @cacheControl(maxAge: 60) {
id: Int
firstName: String
lastName: String
posts: [Post]
}
我收到此错误消息:
Error: Unknown directive "cacheControl".
你能帮忙吗,在模式上应用缓存控制的正确方法是什么?
我按照下面的说明操作,但似乎没有用。
在了解了有关 apollo graphql 缓存的更多信息后,基本上,问题出在 apollo-server-hapi
中的 makeExecutableSchema
,没有包含 @cacheControl
的指令,因此要使其正常工作,我们只需要在 graphql 文件中定义我们自己的 @cacheControl
指令,如下所示:
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]
}
我也是 apollo-server-lambda
,主要问题来自使用 makeExecutableSchema
。 docs 提到这是由模式拼接引起的。
不幸的是,如果您使用 graphql-middleware 之类的东西,除了印度教提到的以外,没有其他办法解决这个问题。还要确保你在 apollo-server > 2.6.6.
以下内容在 "apollo-server-express": "^2.9.12"
中对我有用:
1.- 设置全局最大缓存:
var graphqlServer = new ApolloServer({
cacheControl: {
defaultMaxAge: 1000,
},
...
2.- 在您的架构中定义以下指令:
// Schema (root query)
const Query = gql`
directive @cacheControl(
maxAge: Int,
scope: CacheControlScope
) on OBJECT | FIELD | FIELD_DEFINITION
enum CacheControlScope {
PUBLIC
PRIVATE
}
type Query {
...
3.- 最后,调用它:
module.exports = `
type ArticlePage @cacheControl(maxAge: 801){
article(id: String) : Article
author(key: String) : Author
}`;
诀窍是@cacheControl(maxAge: 801)
不能高于defaultMaxAge: 1000
。
祝你好运!
我有 graphql apollo-server-hapi
。我尝试像下面这样添加缓存控制:
const graphqlOptions = {
schema,
tracing: true,
cacheControl: true,
};
但是当我尝试在模式基础上添加缓存选项时:
type Author @cacheControl(maxAge: 60) {
id: Int
firstName: String
lastName: String
posts: [Post]
}
我收到此错误消息:
Error: Unknown directive "cacheControl".
你能帮忙吗,在模式上应用缓存控制的正确方法是什么?
我按照下面的说明操作,但似乎没有用。
在了解了有关 apollo graphql 缓存的更多信息后,基本上,问题出在 apollo-server-hapi
中的 makeExecutableSchema
,没有包含 @cacheControl
的指令,因此要使其正常工作,我们只需要在 graphql 文件中定义我们自己的 @cacheControl
指令,如下所示:
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]
}
我也是 apollo-server-lambda
,主要问题来自使用 makeExecutableSchema
。 docs 提到这是由模式拼接引起的。
不幸的是,如果您使用 graphql-middleware 之类的东西,除了印度教提到的以外,没有其他办法解决这个问题。还要确保你在 apollo-server > 2.6.6.
以下内容在 "apollo-server-express": "^2.9.12"
中对我有用:
1.- 设置全局最大缓存:
var graphqlServer = new ApolloServer({
cacheControl: {
defaultMaxAge: 1000,
},
...
2.- 在您的架构中定义以下指令:
// Schema (root query)
const Query = gql`
directive @cacheControl(
maxAge: Int,
scope: CacheControlScope
) on OBJECT | FIELD | FIELD_DEFINITION
enum CacheControlScope {
PUBLIC
PRIVATE
}
type Query {
...
3.- 最后,调用它:
module.exports = `
type ArticlePage @cacheControl(maxAge: 801){
article(id: String) : Article
author(key: String) : Author
}`;
诀窍是@cacheControl(maxAge: 801)
不能高于defaultMaxAge: 1000
。
祝你好运!