Skip to content

Instantly share code, notes, and snippets.

@andreystarkov
Created June 4, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreystarkov/f45c1cd3a74fd3d296859a2d9a967c4f to your computer and use it in GitHub Desktop.
Save andreystarkov/f45c1cd3a74fd3d296859a2d9a967c4f to your computer and use it in GitHub Desktop.
cache options for apollo client
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