0
votes

Apollo graphql requests follow an initialization function that accepts http headers.
I want to attach the authentication token to the Authorization header.

A user may login / logout and switch account.
Should I execute the init function with the token header on any auth change?
Or is there a better way?

1

1 Answers

0
votes

you can make function like this

const createApolloClient = (authToken) => {
  return new ApolloClient({
    link: new HttpLink({
      uri: 'your url',
      headers: {
        Authorization: `Bearer ${authToken}`
      }
    }),
    cache: new InMemoryCache(),
  });
 };

and call it like this

var idToken = "abc" //fetch and store your token here   
  const client = createApolloClient(idToken);
   return (
    <ApolloProvider client={client}>
       <div>
       </div>
    </ApolloProvider>