UNPKG

@graphql-tools/executor-urql-exchange

Version:
81 lines (80 loc) 3.46 kB
import { filter, make, merge, mergeMap, pipe, share, takeUntil } from 'wonka'; import { fakePromise, isAsyncIterable } from '@graphql-tools/utils'; import { makeErrorResult, makeResult, mergeResultPatch, } from '@urql/core'; export function executorExchange(executor) { function makeYogaSource(operation) { const extraFetchOptions = typeof operation.context.fetchOptions === 'function' ? operation.context.fetchOptions() : operation.context.fetchOptions; const executionRequest = { document: operation.query, operationType: operation.kind, variables: operation.variables, context: operation.context, extensions: { endpoint: operation.context.url, fetch: operation.context.fetch, useGETForQueries: operation.context.preferGetMethod, headers: extraFetchOptions?.headers, method: extraFetchOptions?.method, }, }; return make(observer => { let ended = false; fakePromise() .then(() => executor(executionRequest)) .then(result => { if (ended || !result) { return; } if (!isAsyncIterable(result)) { observer.next(makeResult(operation, result)); observer.complete(); } else { let prevResult = null; return fakePromise().then(async () => { for await (const value of result) { if (value) { if (prevResult && value.incremental) { prevResult = mergeResultPatch(prevResult, value); } else { prevResult = makeResult(operation, value); } observer.next(prevResult); } if (ended) { break; } } observer.complete(); }); } }) .catch(error => { observer.next(makeErrorResult(operation, error)); }) .finally(() => { ended = true; observer.complete(); }); return () => { ended = true; }; }); } return function executorExchangeFn({ forward }) { return function executorExchangeIO(ops$) { const sharedOps$ = share(ops$); const executedOps$ = pipe(sharedOps$, filter((operation) => operation.kind === 'query' || operation.kind === 'mutation' || operation.kind === 'subscription'), mergeMap((operation) => { const teardown$ = pipe(sharedOps$, filter((op) => op.kind === 'teardown' && op.key === operation.key)); return pipe(makeYogaSource(operation), takeUntil(teardown$)); })); const forwardedOps$ = pipe(sharedOps$, filter((operation) => operation.kind === 'teardown'), forward); return merge([executedOps$, forwardedOps$]); }; }; }