Skip to content

Instantly share code, notes, and snippets.

@maiquealmeida
Created November 19, 2019 09:00
Show Gist options
  • Save maiquealmeida/5061218580b3410f80a10c8ebd6dcba0 to your computer and use it in GitHub Desktop.
Save maiquealmeida/5061218580b3410f80a10c8ebd6dcba0 to your computer and use it in GitHub Desktop.
Apollo Client service example.
import { ApolloClient } from "apollo-client";
import { ApolloLink } from "apollo-link";
import { HttpLink } from "apollo-link-http";
import { onError } from "apollo-link-error";
import { InMemoryCache } from "apollo-cache-inmemory";
import { getToken } from "./token";
import * as Toast from "./notification";
const httpLink = new HttpLink({
uri: process.env.REACT_APP_API_URL,
headers: {
authorization: getToken()
}
});
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
// do something with graphql error
for (let x = 0; x < graphQLErrors.length; x++) {
let err = graphQLErrors[x];
console.log(">>>>> err", err);
switch (err.message) {
case "not-found":
Toast.showError(
"Usuário não encontrado. Verifique e tente novamente."
);
break;
default:
}
}
console.log("graphQLErrors =====> ", graphQLErrors);
}
if (networkError) {
// do something with network error
console.log("networkError =====> ", networkError);
}
});
const link = ApolloLink.from([errorLink, httpLink]);
const client = new ApolloClient({
link,
cache: new InMemoryCache()
});
export default client;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment