首页 > 解决方案 > 在护照 app.use() 回调中未收到 Req 和 Res 参数

问题描述

我正在我的快速中间件上构建以下路线:

app.use(
    "/graphql",
    passport.authenticate("jwt", { session: false }),
    appGraphQL()
);

该路由通过护照进行验证,然后调用我的 GraphQL 服务器。

我的 GraphQL 服务器入口代码是:

export default (req, res) => {
    console.log("RECEIVED PARAMETERS:")
    console.log(req)
    console.log(res)

    return graphqlHTTP({
        schema: schema,
        graphiql: true,
        pretty: true,
        context: {
            resolvers: null, // Will add my resolvers here
            req: req,
            res: res
        },
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack,
            path: error.path
        })
    });
};

我得到 null asreqres参数。

如何正确发送req和发送res到我的 GraphQL 代码?

标签: javascriptnode.jsexpressgraphqlpassport.js

解决方案


您正在立即执行appGraphQL

app.use(
    "/graphql",
    passport.authenticate("jwt", { session: false }),
    (req,res) => appGraphQL(req,res)
);

推荐阅读