|
1 |
| -import ApolloClient from 'apollo-boost'; |
| 1 | +import { ApolloClient } from 'apollo-client'; |
| 2 | +import { InMemoryCache } from 'apollo-cache-inmemory'; |
| 3 | +import { HttpLink } from 'apollo-link-http'; |
| 4 | +import { onError } from 'apollo-link-error'; |
| 5 | +import { ApolloLink } from 'apollo-link'; |
2 | 6 |
|
3 |
| -// TODO: TASK 1. migrate from apollo-boost |
| 7 | +const globalLoader = new ApolloLink((operation, forward) => { |
| 8 | + // Use Mobx or Redux (or other) for a global state manager |
| 9 | + console.log('increment loading count'); |
| 10 | + return forward(operation).map((response) => { |
| 11 | + console.log('decrement loading count'); |
| 12 | + return response; |
| 13 | + }); |
| 14 | +}); |
4 | 15 |
|
5 | 16 | export default new ApolloClient({
|
6 |
| - uri: 'http://localhost:8000/graphql', |
7 |
| - credentials: 'same-origin', |
| 17 | + link: ApolloLink.from([ |
| 18 | + onError(({ graphQLErrors, networkError }) => { |
| 19 | + if (graphQLErrors) { |
| 20 | + graphQLErrors.forEach(({ message, locations, path }) => ( |
| 21 | + console.log(`[GraphQL error]: Message: ${message}, Location: ${JSON.stringify(locations)}, Path: ${path}`) |
| 22 | + )); |
| 23 | + } |
| 24 | + if (networkError) { |
| 25 | + console.log(`[Network error]: ${networkError}`); |
| 26 | + } |
| 27 | + }), |
| 28 | + globalLoader, |
| 29 | + new HttpLink({ |
| 30 | + uri: 'http://localhost:8000/graphql', |
| 31 | + credentials: 'same-origin', |
| 32 | + }), |
| 33 | + ]), |
| 34 | + cache: new InMemoryCache(), |
8 | 35 | });
|
0 commit comments