graphql - 节点获取时使用 RestDataSource 不会返回数据
问题描述
我正在使用 RestDataSource 从 api 获取数据。所有 'POST' 函数都工作并返回响应,但 'GET' 返回 401 错误。
constructor(@Inject(CONTEXT) context: GraphQLModules.GlobalContext) {
super();
super.initialize({ context, cache: new InMemoryLRUCache() });
this.baseURL = process.env.APP_API_HOST;
}
willSendRequest(request: RequestOptions) {
if (this.context.token) {
request.headers.set('Authorization', this.context.token);
}
}
async me() {
return await this.get('/me');
}
async login({ email, password }: MutationLoginArgs) {
return await this.post('/login', {
email,
password,
});
}
这是通过解析器调用的
Query: {
me: async (_parent, _args, context) => {
const api = context.injector.get(AuthAPI);
const reply = await api.me();
return reply.result;
},
},
我得到的响应总是来自我们的 api 的 401。node-fetch
但是,如果我像这样直接在解析器中拨打电话:
Query: {
me: async (_parent, _args, context) => {
const response = await fetch(`${process.env.APP_API_HOST}/me`, {
headers: { Authorization: context.token },
});
const data = await response.json();
return data.result;
},
},
然后这将返回我期望的对象。
两种方法都将令牌作为标头发送,因此不确定哪里出了问题,因此感谢您的帮助。
解决方案
推荐阅读
- data-structures - 环形缓冲区数据结构
- json - 在 Ansible 中检索、操作和发回 JSON
- java - 从源代码创建 jfreechart-1.5.3 JAR
- ios - 如何修改 ShareViewController 以便在共享表中选择应用程序会导致保存媒体并启动所选应用程序?
- powershell - 以 ComObject 的形式打开 PowerShell 脚本
- laravel - SassError:未定义的函数 Laravel Vue
- html - 如何在不破坏时间线格式的情况下使用溢出-x
- python - 写入串口但只接收第一个字符?
- javascript - 当提供以前用户使用的电子邮件地址时,尝试使用 Javascript 阻止我的表单提交
- python - 从嵌套结构中删除 JSON 数据对