withApollo.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 { ApolloLink } from 'apollo-link'
  12. import { InMemoryCache } from 'apollo-cache-inmemory'
  13. import { HttpLink } from 'apollo-link-http'
  14. import { onError } from 'apollo-link-error'
  15. const cache = new InMemoryCache()
  16. const httpLink = new HttpLink({ uri: 'http://localhost:8801/', credentials: 'include' })
  17. const errorLink = onError(({ graphQLErrors, networkError, response }) => {
  18. console.log('ERRORS', graphQLErrors, networkError)
  19. if (graphQLErrors) {
  20. graphQLErrors.map(({ message, locations, path }) => {
  21. console.log(`[GraphQL error] Message: ${message}, Location: ${locations}, Path: ${path}`)
  22. })
  23. }
  24. if (networkError) {
  25. console.log(`[Network error] ${networkError}`)
  26. }
  27. })
  28. const link = ApolloLink.from([
  29. errorLink,
  30. httpLink
  31. ])
  32. function createClient ({ ctx, headers }) {
  33. return new ApolloClient({
  34. link,
  35. cache
  36. })
  37. }
  38. export default withApollo(createClient)