-
-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An exchange for all errors #225
Comments
Hiya, no need to be sorry! This is a very valid question and we're still working on more documentation around custom exchanges. So, generally you'll want to run a fire-and-forget request after a GraphQL request has either had a network error or GraphQL errors, I presume? Exchanges are bi-directional. So suppose you have the default order: The operation result stream goes the other way. This is how it works because every exchange receives a stream of operations. It can then transform this stream and call This means that the simplest exchange just forwards all operations and returns an unaltered stream of results: Specifically, it won't need to alter the operations probably, but it will need to look at the results from the import { filter, pipe, tap } from 'wonka';
import { Exchange } from 'urql';
export const errorExchange: Exchange = ({ forward }) => ops$ => {
return pipe(
forward(ops$),
tap(({ error }) => {
// If the OperationResult has an error send a request to sentry
if (error) {
// the error is a CombinedError with networkError and graphqlErrors properties
sentryFireAndForgetHere() // Whatever error reporting you have
}
})
);
}; So we don't alter the input or output streams but instead just Hope this helps! Let me know if you still need some more input 👍 |
@kitten this is awesome, thanks! I completely missed that exchanges go in both ways. I think providing a default error exchange that calls some function you give it would be a useful thing to provide for sure. For now I'll take that and put it into our codebase :) |
@kitten thanks for your help but unfortunately I'm not having much luck with this. Here's how we create our client: const defaultExchanges = [
dedupExchange,
cacheExchange,
errorExchange, // this is our custom one
fetchExchange,
];
export const createGraphQLClient = (extraOpts = {}) =>
createClient({
url: '/graphql',
fetchOptions: {
credentials: 'same-origin',
headers: {
'X-CSRFToken': getCookie('csrftoken'),
},
},
exchanges: defaultExchanges,
...extraOpts,
});
const defaultClient = createGraphQLClient(); And here's the import { pipe, tap } from 'wonka';
export const errorExchange = ({ forward }) => ops$ => {
console.log('error exchange was used');
return pipe(
forward(ops$),
tap(result => {
console.log('GOT HERE', result);
})
);
}; If I purposefully break the query, I don't see the console.log in the browser. I do see the The error does make it through to the Additionally, even if I make the query succeed, I don't see the Any pointers would be very welcome, thank you! |
FWIW, since Google led me here ... URQL now includes an
|
And for others, this errorExchange has been deprecated. https://formidable.com/open-source/urql/docs/api/core/#errorexchange-deprecated |
Hello,
Sorry if I'm missing a trick, but I can't figure out how to create an exchange that runs after the
fetchExchange
. I want to do this because we'd like to log to Sentry every time a GraphQL request fails, so we're aware of it.The problem is that I don't think
fetchExchange
ever forwards things on (other than things that aren't a thing it understands), and therefore any exchange that's afterfetchExchange
won't get any data.Is that correct, and if so, is there a way around this? The other thing I can think of is making our own
fetchExchange
that does this but I'm not sure if that's a great idea.Thanks!
Jack
The text was updated successfully, but these errors were encountered: