withApollo.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. import { createUploadLink } from 'apollo-upload-client'
  15. import { endpoint, prodEndpoint } from '../config'
  16. const cache = new InMemoryCache()
  17. function createClient ({ ctx, headers }) {
  18. return new ApolloClient({
  19. link: ApolloLink.from([
  20. onError(({ graphQLErrors, networkError }) => {
  21. if (graphQLErrors) {
  22. graphQLErrors.map(({ message, locations, path }) =>
  23. console.log(
  24. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  25. )
  26. )
  27. }
  28. if (networkError) console.log(`[Network error]: ${networkError}`)
  29. }),
  30. createUploadLink({
  31. uri: process.env.NODE_ENV === 'development' ? endpoint : prodEndpoint,
  32. headers,
  33. credentials: 'include'
  34. })
  35. ]),
  36. fetchOptions: { mode: 'no-cors' },
  37. cache
  38. })
  39. }
  40. export default withApollo(createClient)