Apollo-client: Unable to set authorization header using apollo-boost

Created on 19 Feb 2018  路  3Comments  路  Source: apollographql/apollo-client

Intended outcome:
I tried to set an authentication token following the Quickstart documentation using apollo-boost. Therefore I set the request property and tried to set the header via operation.setContext function.

import ApolloClient from "apollo-boost";
// ...
const client = new ApolloClient({
  request: async operation => {
    operation.setContext({
      headers: {
        authorization: "randomToken"
      }
    });
  },
});
//...

Actual outcome:
All further queries fail, with the error message [Network error]: TypeError: operation.setContext is not a function.

How to reproduce the issue:
I forked your pretty Pupstagram demo showcasing the problem. Please note the added request property in the ApolloClient instantiation in index.js and see the devtools for the error message.

Version

Most helpful comment

I am seeing the same

All 3 comments

I am seeing the same

Just submitted PR #3056 which fixes this problem.

What I did to set header with apollo-boost

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "@apollo/react-hooks";

const client = new ApolloClient({
  uri: "http://localhost:5000/graphql",
  request: (operation) => {
    const token = localStorage.getItem('token');
    console.log(token)
    operation.setContext({
      headers: {
        token
      }
    });
  }
});

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById("root")
);
Was this page helpful?
0 / 5 - 0 ratings