我有带有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".
您能帮忙吗?在架构上应用缓存控制的正确方法是什么?
我听从下面的指示,但似乎不起作用。
发布于 2018-10-22 00:37:23
在了解了有关在阿波罗图形we上缓存的更多信息之后,基本上,问题是来自makeExecutableSchema
的apollo-server-hapi
,没有包含@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]
}
发布于 2019-12-05 18:40:55
在"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
。
祝好运!
发布于 2019-07-09 17:42:12
我也是apollo-server-lambda
,主要问题来自于使用makeExecutableSchema
。文档提到这是由模式拼接引起的。
不幸的是,如果您使用像graphql-中间件这样的东西,除了hinduni提到的内容之外,没有其他办法。还要确保你在阿波罗服务器上> 2.6.6.
https://stackoverflow.com/questions/52922080
复制