withApollo.js 840 B

123456789101112131415161718192021222324252627282930313233
  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, { InMemoryCache } from 'apollo-boost'
  11. import { endpoint, prodEndpoint } from '../config'
  12. function createClient({ ctx, headers, initialState }) {
  13. return new ApolloClient({
  14. uri: process.env.NODE_ENV === 'development' ? endpoint : prodEndpoint,
  15. cache: new InMemoryCache().restore(initialState || {}),
  16. request: operation => {
  17. operation.setContext({
  18. fetchOptions: {
  19. credentials: 'include'
  20. },
  21. headers
  22. })
  23. },
  24. clientState: {
  25. resolvers: {},
  26. defaults: {}
  27. }
  28. })
  29. }
  30. export default withApollo(createClient)