123456789101112131415161718192021222324252627282930313233343536373839404142 |
- /**
- * Using next-with-apollo
- * https://github.com/lfades/next-with-apollo
- *
- * Changes:
- * * Reading endpoint and prodEndpoint from a config file
- * * Setting request to handle credentials.
- */
- import withApollo from 'next-with-apollo'
- import ApolloClient from 'apollo-client'
- import { InMemoryCache } from 'apollo-cache-inmemory'
- import { onError } from 'apollo-link-error'
- import { ApolloLink } from 'apollo-link'
- import { createUploadLink } from 'apollo-upload-client'
- import { endpoint, prodEndpoint } from '../config'
- import { resolvers, typeDefs } from './localState'
- function createClient ({ ctx, headers, initialState }) {
- return new ApolloClient({
- link: ApolloLink.from([
- onError(({ graphQLErrors, networkError }) => {
- if (graphQLErrors) {
- graphQLErrors.map(({ message, locations, path }) =>
- console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`))
- }
- if (networkError) console.log(`[Network error]: ${networkError}`)
- }),
- createUploadLink({
- uri: process.env.NODE_ENV === 'development' ? endpoint : prodEndpoint,
- headers,
- credentials: 'include'
- })
- ]),
- fetchOptions: { mode: 'no-cors' },
- cache: new InMemoryCache(),
- resolvers,
- typeDefs
- })
- }
- export default withApollo(createClient)
|