Created
March 24, 2023 05:50
-
-
Save melechi/7b2ef30c86a704715e96e451e81263d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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