javascript - apollo-client 没有响应头

标签 javascript vue.js graphql apollo apollo-client

我有一个 graphql api 和一个带有 apollo client 的 vue.js 前端用于从后端请求数据。到目前为止它工作正常。

服务器在我需要的每个响应 header 中使用新的 JWT-Token 进行响应。所以解决方案应该很简单.. a afterware它从每个响应中获取新 key 并更新相应的存储条目。 但是 .. 标题是空的。

这是后期:

apolloClient.networkInterface.useAfter([{
    applyAfterware({ response }, next) {
        if (process.env.NODE_ENV === 'development') {
            console.log('got new response', response);
        }
        if (response.status === 401) {
            store.commit('logout');
            store.commit('routeLogin');
        }
        next();
    }
}]);

这是响应的样子:

screenshot from devtools console.log

这是实际发送的内容:

enter image description here

我错过了什么吗?

更新

我找到了 similar case in the issue tracker这已通过添加 Access-Control-Expose-Headers 解决。所以我将它们添加到我的服务器配置中。现在响应看起来像这样,而 apollos header 对象仍然是空的:

enter image description here

最佳答案

您必须移除 {}。

来自:

applyAfterware({res}, next) {

到:

applyAfterware(res, next) {

现在您获得了完整的 header 信息。

apolloClient.networkInterface.useAfter([{
applyAfterware(res, next) {
        console.log("response:", res.options.headers);
        if (res.response.status === 401) {
            store.commit('logout');
            store.commit('routeLogin');
            // logout();
        }
        next();
    }
}]);

关于javascript - apollo-client 没有响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46257557/

相关文章:

javascript - 使内容可编辑部分中的 h2 标签不可删除

javascript - 如何通过使用 PHP、javascript 和 html 单击从数据库检索的数据来编辑每行属性

vue.js - 如何从 Vuetify v-select 中移除焦点?

javascript - 当滚动到页面末尾时更新...但不断更新

vue.js - 与 Vue 3 开 Jest - ReferenceError : define is not defined

graphql - 为突变 GraphQL 提供显式类型

javascript - 匹配二维或一维数组中的数组

javascript - javascript 在模糊表单验证方面遇到问题

sequelize.js - GraphQL 和 graphql-sequelize-schema-generator

node.js - GraphQL 所需的最低 Node 版本?