Javascript 使用Apollo客户端时未向请求发送GraphQL突变?

Javascript 使用Apollo客户端时未向请求发送GraphQL突变?,javascript,reactjs,graphql,Javascript,Reactjs,Graphql,我遇到了一个问题,当从“apollo boost”向带有{apollo Client}的后端服务器发送自定义头时,无法指定URI, 所以我不得不使用“阿波罗客户端”中的{apollo客户端} 这个问题已经解决了,但是现在我的突变没有被发送到后端 我的变异: import { gql } from 'apollo-boost'; export const LOGIN_USER = gql` mutation($email: String!, $password: String!) {

我遇到了一个问题,当从“apollo boost”向带有{apollo Client}的后端服务器发送自定义头时,无法指定URI, 所以我不得不使用“阿波罗客户端”中的{apollo客户端}

这个问题已经解决了,但是现在我的突变没有被发送到后端

我的变异:

import { gql } from 'apollo-boost';

export const LOGIN_USER = gql`
  mutation($email: String!, $password: String!) {
    loginUser(email: $email, password: $password) {
      userId
      token
      expiresIn
    }
  }
`
这就是我得到的错误

"Error: Network error: Cannot read property 'token' of null"
当我把它从apollo boost中切换回来时,它又能工作了


非常感谢您的帮助

不是100%确定,但我认为错误在于:

const store=JSON.parsesessionStorage.getItem'interdevs-data'; const token=store.token; 若并没有包含键interdevs数据的项,则存储区将为空

我认为你可以通过这样做来解决它:

const store=JSON.parsesessionStorage.getItem'interdevs-data'; const令牌=存储?store.token:空;
了解如何使用apollo boost设置身份验证标头

const client = new ApolloClient({
  uri: 'http://localhost:3001/graphql',
  request: operation => {
    const ssData = JSON.parse(sessionStorage.getItem('data'));
    operation.setContext({
      headers: {
        authorization: ssData ? `Bearer ${ssData.token}` : ''
      }
    });
   }
});


哦,谢谢,太好了,我会把它添加到我的代码中。但是它返回null,因为变异没有返回一个标记,这是我的主要问题
"Error: Network error: Cannot read property 'token' of null"
const client = new ApolloClient({
  uri: 'http://localhost:3001/graphql',
  request: operation => {
    const ssData = JSON.parse(sessionStorage.getItem('data'));
    operation.setContext({
      headers: {
        authorization: ssData ? `Bearer ${ssData.token}` : ''
      }
    });
   }
});