ApolloClient not setting authorization header

ApolloClient not setting authorization header


0

I have an expo app that is using a graphql api endpoint. I am attempting to pass an authorization token with each request, but my set up does not seems to be setting or sending my authorization header. In my graphql.config.yml I can set the authorization header here like this and it works:

schema: schema.graphql
extensions:
  endpoints:
    Dev Backend:
      url: https://localhost:8000/graphql
      introspect: true
      headers:
        authorization: "My token"

But in my actual client setup code it is not setting my authorization header. The headers being logged on the backend look like this: Headers({‘content-length’: ‘113’, ‘content-type’: ‘application/json; charset=UTF-8’, ‘host’: ‘localhost:8000’, ‘connection’: ‘Keep-Alive’, ‘user-agent’: ‘Apache-HttpClient/4.5.14 (Java/17.0.7)’, ‘accept-encoding’: ‘gzip,deflate’})

Any help on this would be greatly appreciated.

import {ApolloClient, InMemoryCache, ApolloLink, concat} from '@apollo/client';
import {setContext} from '@apollo/client/link/context';
import {getUserSession} from "./session-handler";
import {createUploadLink} from "apollo-upload-client"; // Import AsyncStorage from the correct package

// Create an HTTP link to your GraphQL server
const httpLink = createUploadLink({
  uri: 'https://localhost:8000/',
  credentials: 'include',
});

const authMiddleware = new ApolloLink((operation, forward) => {
  // add the authorization to the headers
  operation.setContext(({ headers = {} }) => ({
    headers: {
      ...headers,
      authorization: "THIS IS A TEST TOKEN",
    }
  }));

  return forward(operation);
})

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: concat(authMiddleware, httpLink),
});

export {
  client
};

Share
Improve this question

1


Load 3 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *