【问题标题】:How to access request headers in custom graphQL resolvers when using neo4j-graphql-js?使用 neo4j-graphql-js 时如何访问自定义 graphQL 解析器中的请求标头?
【发布时间】:2020-10-01 17:48:09
【问题描述】:

尝试时:

const server = new ApolloServer({
  schema: schema,
  context: { req, driver, neo4jDatabase: process.env.NEO4J_DATABASE },
  introspection: true,
  playground: true,
})

错误:req 未定义

尝试时:

const server = new ApolloServer({
  schema: schema,
  context: ({ res }, driver, neo4jDatabase = process.env.NEO4J_DATABASE) => (
    res, driver, neo4jDatabase
  ),
  introspection: true,
  playground: true,
})

错误:未提供 Neo4j JavaScript 驱动程序实例。

我已经尝试了很多不同的方式来编写它和阅读 Apollo 源代码,但都没有成功。

【问题讨论】:

    标签: javascript neo4j graphql apollo-server neo4j-graphql-js


    【解决方案1】:

    感谢这条评论https://github.com/grand-stack/graphql-auth-directives/issues/4#issuecomment-494620480解决了它

    const server = new ApolloServer({
      schema,
      context: ({ req }) => {
        return {
          headers: req.headers, 
          driver
         };
      }
    });
    

    它确实奏效了。

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2018-02-18
      • 2018-03-01
      • 2021-12-22
      • 2022-09-26
      • 2020-10-04
      • 1970-01-01
      • 2020-07-21
      • 2020-05-10
      相关资源
      最近更新 更多