Created
May 20, 2018 15:52
-
-
Save pongsatt/8ac28d37f1eeae61048ffef61a86224b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloClient } from 'apollo-client'; | |
import { HttpLink } from 'apollo-link-http'; | |
import { InMemoryCache } from 'apollo-cache-inmemory'; | |
import { onError } from 'apollo-link-error'; | |
import { ApolloLink, RequestHandler, Operation, NextLink } from 'apollo-link'; | |
import store from './'; | |
const cache = new InMemoryCache(); | |
const link = new HttpLink({ uri: 'http://localhost:4000' }); | |
const middleLink = new ApolloLink((operation: Operation, forward?: NextLink): any => { | |
store.dispatch('busy', true); | |
return forward && forward(operation).map((response) => { | |
store.dispatch('busy', false); | |
return response; | |
}); | |
}); | |
const errorLink = onError((error: any) => { | |
store.dispatch('error', error); | |
store.dispatch('busy', false); | |
}); | |
const client = new ApolloClient({ | |
link: errorLink.concat(middleLink).concat(link), | |
cache | |
}); | |
export default client; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment