Apollo Link响应标头 [英] Apollo Link response headers

查看:293
本文介绍了Apollo Link响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个简单的React应用程序中使用最新版本的Apollo Client,我试图从响应中提取一个标头值,该标头值用于显示要返回的记录集的大小.

I am using the latest version of Apollo Client in a simple React app and I am trying to pull out a header value from the response that is being used to show the size of the record set being returned.

我很高兴这不是提供结果集大小的最优雅的方法,但这是API当前的设置方式.

I appreciate that this is not the most elegant way of providing the result set size, but that is how the API has currently been set up.

我希望使用中间件类型选项来执行此操作,但是当我检查响应对象时,我似乎无法拉出任何标头.

I was hoping to use the the middleware type options to do this, but when I inspect the response object I can't seem to pull out any headers.

网络跟踪的确显示了响应标头符合预期,因此我怀疑我误解了如何获取所需的基础对象.

The network trace does show that the response header is as expected so I suspect I am misunderstanding how to get at the underlying objects that I need.

我已经检查了文档,但是没有什么特别明显,因此这里的问题...

I have checked the documentation, but nothing stands out as obvious hence the question here ...

推荐答案

在这里找到了答案: https://github.com/apollographql/apollo-client/issues/2514

必须通过操作上下文访问它...有趣的是,开发工具似乎显示headers对象为空,但是您可以从其中提取已命名的headers ...

Have to access it via the operation context ... interestingly the dev tools appears to show that the headers object is empty, but you can then pull named headers from it ...

const afterwareLink = new ApolloLink((operation, forward) => {
  return forward(operation).map(response => {
    const context = operation.getContext();
    const { response: { headers } } = context;

    if (headers) {
      const yourHeader = headers.get('yourHeader');
    }

    return response;
  });
});

这篇关于Apollo Link响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆