zag*_*gen 5 middleware express graphql graphql-js express-graphql
我有一个关于express-graphql.\nI\xe2\x80\x99m 的问题,试图在 GraphQL 解析器之后运行中间件。
这是我的前两次尝试:
\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema\n })\n);\n\napp.use((req, res, next) => {\n console.log('middleware called');\n next();\n});\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema,\n graphiql: true,\n }), () => {\n console.log('middleware called');\n }\n);\nRun Code Online (Sandbox Code Playgroud)\n\n两者都\xe2\x80\x99t 工作。\n我猜express-graphql不是next()在某个地方打电话。
消息来源似乎证实了这一点:
\n\ntype Middleware = (request: Request, response: Response) => void;\nRun Code Online (Sandbox Code Playgroud)\n\nnext不是参数。
我尝试了这个解决方法:
\n\napp.use( (req, res, next) => {\n req.on('end', () => {\n console.log('middleware called');\n });\n next();\n});\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema\n })\n);\nRun Code Online (Sandbox Code Playgroud)\n\n这确实有效。但在实际使用中我注意到,通过突变更改的数据在回调中尚不可用(即已更新)。如果我将代码包装在 a 中,setTimeout数据就会更新。
底线:如何让中间件(或任何代码)在解析器之后运行?
\n| 归档时间: |
|
| 查看次数: |
438 次 |
| 最近记录: |