如何扩展 GraphQL 调用的上下文对象?

Men*_*des 0 javascript node.js express passport.js graphql

我在标准模块中有以下 GraphQL 服务器调用:

export default () => {
    return graphqlHTTP({
        schema: schema,
        graphiql: true,
        pretty: true,
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack,
            path: error.path
        })
    });
};
Run Code Online (Sandbox Code Playgroud)

与护照一起使用:

app.use(
    "/graphql",
    passport.authenticate("jwt", { session: false }),
    appGraphQL()
);
Run Code Online (Sandbox Code Playgroud)

一切都是工作文件。Passport 扩展了 myreq以获取在我的 GraphQL 调用查询或更改时使用的已记录用户对象:

...
    resolve(source, args, context) {
        console.log("CURRENT USER OBJECT")
        console.log(context.user)
...
Run Code Online (Sandbox Code Playgroud)

一切都很好。

现在我需要扩展我的上下文以添加一些自定义解析器,所以我的第一次尝试是:

export default () => {
    return graphqlHTTP({
        schema: schema,
        graphiql: true,
        pretty: true,
        context: resolvers,
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack,
            path: error.path
        })
    });
};
Run Code Online (Sandbox Code Playgroud)

正如 GraphQL 文档所说,这会覆盖原始的 req 上下文,并且 my context.useron 查询和突变停止工作。

如何正确扩展当前上下文以添加更多字段,而不是覆盖它?另一个不成功的尝试:

export default (req) => {
    return graphqlHTTP({
        schema: schema,
        graphiql: true,
        pretty: true,
        context: {
            user: req.user,
            resolvers: resolvers
        },
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack,
            path: error.path
        })
    });
};
Run Code Online (Sandbox Code Playgroud)

这种方法不起作用......我收到以下错误:

  user: req.user,
                ^

TypeError: Cannot read property 'user' of undefined
Run Code Online (Sandbox Code Playgroud)

[编辑] 我最近的尝试来自我在Apollo Docs上的示例:

export default () => {
    return graphqlHTTP({
        schema: schema,
        graphiql: true,
        pretty: true,
        context: ({ req }) => ({
            user: req.user
        }),
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack,
            path: error.path
        })
    });
};
Run Code Online (Sandbox Code Playgroud)

现在我的上下文是我的解析器中的一个函数:

console.log(context)
console.log(context.user)
Run Code Online (Sandbox Code Playgroud)

返回:

[Function: context]
undefined
Run Code Online (Sandbox Code Playgroud)

被这个简单的事情弄疯了......

Dan*_*den 5

假设您使用的是express-graphql,graphqlHTTP是一个函数,它接受一些配置参数并返回一个快速中间件函数。通常,它是这样使用的:

app.use(
  '/graphql',
  graphqlHTTP({ ... })
)
Run Code Online (Sandbox Code Playgroud)

但是,graphqlHTTP 也可以采用返回对象的函数,而不是对象,如docs所示。

app.use(
  '/graphql',
  graphqlHTTP((req, res, graphQLParams) => {
    return { ... }
  })
)
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以在配置对象中使用req,resgraphQLParams参数。

app.use(
  '/graphql',
  graphqlHTTP((req, res, graphQLParams) => {
    return {
      schema,
      // other options
      context: {
        user: req.user,
        // whatever else you want
      }
    }
  })
)
Run Code Online (Sandbox Code Playgroud)