Skip to content

Instantly share code, notes, and snippets.

@ali
Last active June 15, 2020 12:06
Show Gist options
  • Save ali/4884425affbe43f9db85726fecf7d033 to your computer and use it in GitHub Desktop.
Save ali/4884425affbe43f9db85726fecf7d033 to your computer and use it in GitHub Desktop.
network interface for Apollo Client that throws away errors
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