Skip to content

Instantly share code, notes, and snippets.

@stubailo
Created November 26, 2018 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stubailo/610f6319ef551499ffa0883d47df5d9f to your computer and use it in GitHub Desktop.
Save stubailo/610f6319ef551499ffa0883d47df5d9f to your computer and use it in GitHub Desktop.
A provider for mocking errors with Apollo Client
export const ErrorProvider = (props) => {
// This is just a link that swallows all operations and returns the same thing
// for every request: The specified error.
const link = new ApolloLink((operation) => {
return new Observable((observer) => {
observer.next({
errors: props.graphQLErrors || [
{message: 'Unspecified error from ErrorProvider.'},
],
});
observer.complete();
});
});
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
});
return (
<ApolloProvider client={client}>
{props.children}
</ApolloProvider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment