Skip to content

Instantly share code, notes, and snippets.

@pongsatt
Created May 20, 2018 15:52
Show Gist options
  • Save pongsatt/8ac28d37f1eeae61048ffef61a86224b to your computer and use it in GitHub Desktop.
Save pongsatt/8ac28d37f1eeae61048ffef61a86224b to your computer and use it in GitHub Desktop.
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