【发布时间】:2021-02-09 22:07:23
【问题描述】:
我有一个使用 apollo-server-plugin-response-cache 插件的 Apollo GraphQL 服务器,我需要根据传入的参数确定是否要写入缓存。我已经设置了插件,并且正在使用 shouldWriteToCache 挂钩。我可以打印出传递给钩子的GraphQLRequestContext 对象,我可以看到完整的请求源,但request.variables 是空的。除了解析 query 本身之外,我如何在这个钩子中访问解析器的实际参数? (在下面的例子中,我需要param2的值。)
阿波罗服务器:
new ApolloServer({
introspection: true,
playground: true,
subscriptions: false,
typeDefs,
resolvers,
cacheControl: {
defaultMaxAge: 60
},
plugins: [
apolloServerPluginResponseCache({
cache, // This is a "apollo-server-cache-redis" instance
shouldWriteToCache: (requestContext) => {
// I get a lot of info here, including the source query, but not the
// parsed out query variables
console.log(requestContext.request);
// What I want to do here is:
return !context.request.variables.param2
// but `variables` is empty, and I can't see that value parsed anywhere else
}
})
]
})
这是我的解析器:
export async function exapi(variables, context) {
// in here I use context.param1 and context.param2
// ...
}
我也试过了:
export async function exapi(variables, { param1, param2 }) {
// ...
}
这是我从上面的代码中注销的内容:
{
query: '{\n' +
' exapi(param1: "value1", param2: true) {\n' +
' records\n' +
' }\n' +
'}\n',
operationName: null,
variables: {}, // <-- this is empty?! How can I get param2's value??
extensions: undefined,
http: Request {
size: 0,
timeout: 0,
follow: 20,
compress: true,
counter: 0,
agent: undefined,
[Symbol(Body internals)]: { body: null, disturbed: false, error: null },
[Symbol(Request internals)]: {
method: 'POST',
redirect: 'follow',
headers: [Headers],
parsedURL: [Url],
signal: null
}
}
}
【问题讨论】:
标签: node.js typescript graphql apollo-server