Created
November 26, 2018 17:14
-
-
Save stubailo/610f6319ef551499ffa0883d47df5d9f to your computer and use it in GitHub Desktop.
A provider for mocking errors with Apollo Client
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
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