Created
June 4, 2019 17:22
-
-
Save andreystarkov/f45c1cd3a74fd3d296859a2d9a967c4f to your computer and use it in GitHub Desktop.
cache options for apollo client
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 } from 'apollo-client' | |
import { ApolloLink, concat } from 'apollo-link' | |
import { HttpLink } from 'apollo-link-http' | |
import { InMemoryCache } from 'apollo-cache-inmemory' | |
const defaultOptions = { | |
watchQuery: { | |
fetchPolicy: 'network-only', | |
errorPolicy: 'ignore' | |
}, | |
query: { | |
fetchPolicy: 'network-only', | |
errorPolicy: 'all' | |
}, | |
mutate: { | |
fetchPolicy: 'no-cache', | |
errorPolicy: 'all' | |
} | |
} | |
export const createApolloClient = (uri = process.env.REACT_APP_SERVER_ENDPOINT, accessToken) => { | |
const httpLink = new HttpLink({ uri, credentials: 'include', fetch }) | |
const authMiddleware = new ApolloLink((operation, forward) => { | |
if (accessToken) { | |
// add the authorization to the headers | |
operation.setContext({ | |
headers: { | |
Authorization: `Bearer ${accessToken}` | |
} | |
}) | |
} | |
return forward(operation) | |
}) | |
return new ApolloClient({ | |
link: concat(authMiddleware, httpLink), // authLink.concat(httpLink), | |
cache: new InMemoryCache(), | |
defaultOptions | |
}) | |
} | |
export const client = createApolloClient() | |
export default client | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment