withApollo.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Using next-with-apollo
  3. * https://github.com/lfades/next-with-apollo
  4. *
  5. * Changes:
  6. * * Reading endpoint and prodEndpoint from a config file
  7. * * Setting request to handle credentials.
  8. */
  9. import withApollo from 'next-with-apollo'
  10. import ApolloClient from 'apollo-client'
  11. import { InMemoryCache } from 'apollo-cache-inmemory'
  12. import { onError } from 'apollo-link-error'
  13. import { ApolloLink } from 'apollo-link'
  14. const cache = new InMemoryCache()
  15. function createClient({ ctx, headers }) {
  16. return new ApolloClient({
  17. link: ApolloLink.from([
  18. onError(({ graphQLErrors, networkError }) => {
  19. if (graphQLErrors) {
  20. graphQLErrors.map(({ message, locations, path }) =>
  21. console.log(
  22. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  23. )
  24. )
  25. }
  26. if (networkError) console.log(`[Network error]: ${networkError}`)
  27. })
  28. ]),
  29. fetchOptions: { mode: 'no-cors' },
  30. cache
  31. })
  32. }
  33. export default withApollo(createClient)