Created
November 19, 2019 09:00
-
-
Save maiquealmeida/5061218580b3410f80a10c8ebd6dcba0 to your computer and use it in GitHub Desktop.
Apollo Client service example.
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 { 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