withApollo.js 1.3 KB

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