Skip to content

Instantly share code, notes, and snippets.

@melechi
Created March 24, 2023 05:50
Show Gist options
  • Save melechi/7b2ef30c86a704715e96e451e81263d2 to your computer and use it in GitHub Desktop.
Save melechi/7b2ef30c86a704715e96e451e81263d2 to your computer and use it in GitHub Desktop.
import { ApolloClient, createHttpLink, InMemoryCache, ApolloProvider } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
const HTTPLink = createHttpLink({
uri: process.env.REACT_APP_GAPHQL_SERVER,
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('jwtToken');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(HTTPLink)
});
export default function({children}) {
return (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment