withApollo.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 { HttpLink } from 'apollo-link-http'
  13. import { onError } from 'apollo-link-error'
  14. import { ApolloLink } from 'apollo-link'
  15. const cache = new InMemoryCache()
  16. const link = new HttpLink({ uri: 'http://localhost:8801/', credentials: 'include' })
  17. const oldLink = 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. function createClient ({ ctx, headers }) {
  30. return new ApolloClient({
  31. link,
  32. cache
  33. })
  34. }
  35. export default withApollo(createClient)