1import type { Exchange } from '@urql/core';
2import { mapExchange } from '@urql/core';
3import { toe } from 'graphql-toe';
4
5/** Exchange factory that maps the fields of the data to throw an error on access if the field was errored.
6 *
7 * @returns the created throw-on-error {@link Exchange}.
8 */
9export const throwOnErrorExchange = (): Exchange => {
10 return mapExchange({
11 onResult(result) {
12 if (result.data) {
13 const errors = result.error && result.error.graphQLErrors;
14 result.data = toe({ data: result.data, errors });
15 }
16 return result;
17 },
18 });
19};