当节点获取时,使用 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,context) => {
      const response = await fetch(`${process.env.APP_API_HOST}/me`,{
        headers: { Authorization: context.token },});
      const data = await response.json();
      return data.result;
    },

然后这将返回我期望的对象。

这两种方法都将令牌作为标头发送,因此不确定这是哪里出了问题,因此感谢任何帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)