Last active
June 15, 2020 12:06
-
-
Save ali/4884425affbe43f9db85726fecf7d033 to your computer and use it in GitHub Desktop.
network interface for Apollo Client that throws away errors
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, { createNetworkInterface } from 'apollo-client' | |
const GRAPHQL_ENDPOINT_URL = "…" | |
const baseNetworkInterface = createNetworkInterface({ uri: GRAPHQL_ENDPOINT_URL }) | |
// Create our own network interface that delegates to Apollo's and ignores errors | |
const networkInterface = { | |
query: (...args) => { | |
return baseNetworkInterface | |
.query(...args) | |
.then((res) => { | |
const { data, errors } = res | |
if (errors && errors.length > 0) { | |
errors.forEach((e) => { | |
const error = new Error(e.errorType) | |
console.error(error) | |
}) | |
// if the result contains data as well as errors, discard the errors and return a "valid" response | |
if (data !== null) { | |
return { data } | |
} | |
} | |
return res | |
}) | |
} | |
} | |
export default function configureApolloClient(options = {}) { | |
return new ApolloClient({ | |
...options, | |
networkInterface, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment