如何将查询参数添加到Apollo-Server-express的上下文中
我现在正在使用Apollo-Server-express:
import { ApolloServer } from 'apollo-server-express';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo';
import schema from './api';
const server = new ApolloServer({
schema,
context: async ({ req }) => {
const user = await getUser(req.headers.authorization);
return {
user,
};
},
playground: process.env.NODE_ENV === 'development',
introspection: true,
uploads: false,
});
server.applyMiddleware({
app: WebApp.connectHandlers,
path: '/graphql',
});
我想将另一个字段添加到上下文
中,该字段具有我用户访问的URL的所有查询参数。
看来对象将上下文传递给
apollo-server-express
的功能是类型 req.query 对象。但是,当我尝试访问它时:
context: async ({ req }) => {
console.log('### query', req.query);
console.log('### params', req.params);
const user = await getUser(req.headers.authorization);
return {
user,
};
},
并通过http:// localhost访问我的应用程序:3000/u/u/3q2pcjrwyiqr2ywhm/bk2cig7fn3p7z5xvy?edittoken = qdj3rryjcumxfnrnz
我看到以下日志行:
I20220417-15:08:54.477(-5)? ### query {}
I20220417-15:08:54.478(-5)? ### params undefined
I20220417-15:08:54.570(-5)? ### query {}
I20220417-15:08:54.571(-5)? ### params undefine
在创建Apollo-Server-express上下文时,访问URL查询参数的正确方法是什么?我特别尝试将 deditkoken
添加到上下文中。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也想知道。决定离开我的解决方案
在上下文中 - 不仅返回用户,还将用户obj添加到req(req.user =用户),然后返回req本身,在解析器中,您可以使用完整的req obj,
您也可以将edittoken添加到标题中,并以代码将其添加到代码中不太可见用户
,确保我们无法在您的Localhost上访问您的应用:)
Was wondering also. Decide to leave my solution
In context - return not only user, but add user obj to req (req.user = user) and return req itself and in resolvers you can use full req obj
Also you can add editToken to headers and get it in code not so visible for users
And sure we can't visit your app on your localhost :)