apollo-server - 使用 ApolloServer 从 type-graphql 解析器读取请求标头
问题描述
我正在按照这个示例使用 Prisma ORM、ApolloServer 和 type-graphql 来制作一个简单的博客 Web 应用程序。
我想检查 HTTP 请求标头,但无法确定如何在 type-graphql 解析器中访问 ApolloServer ,context.headers
例如:
@Query((returns) => Post, { nullable: true })
async postById(@Arg('id') id: number, @Ctx() ctx: Context) {
return ctx.prisma.post.findUnique({
where: { id },
})
}
ApolloServer 在这里设置如下:
new ApolloServer({ schema, context: context }).listen({ port: 4000 }, () =>
console.log(`Server ready at: http://localhost:4000`), )
但是上面解析器中引用的@Ctx() ctx: Context只是 Prisma 上下文(在此处定义)而不是 ApolloServer 上下文。如果我记录 ctx 对象的密钥,我会得到:[ 'prisma', '_extensionStack' ]
在过去不使用 type-graphql 装饰器的 ApolloServer 项目中,我访问了 ApolloServer 上下文,如下所示:
postById: async (_, { id }, apolloServerContext) => {... console.log(apolloServerContext.headers)
如何从使用 type-graphql 装饰器的解析器访问请求标头?
解决方案
推荐阅读
- azure-functions - 从函数应用访问虚拟网络中的 Cosmos DB
- php - 如何防止 Yii CGridView 过滤器发生变化?
- python - Flask跨域不与命名空间一起工作
- semantic-ui - 防止在显示语义 ui 模式时隐藏正文滚动条
- python - SymPy rref() 返回奇异矩阵的单位矩阵
- javascript - 使用 JQUERY / AJAX 遍历 XML 文件
- xaml - UWP 边框渲染问题
- python - 将文件对话框添加到菜单功能(打开)wxpython 3
- python - 使用 argparse 参数名称作为变量名称
- graphql - graphql 突变反应类型