UNPKG

@graphql-tools/executor-apollo-link

Version:

A set of utils for faster development of GraphQL tools

45 lines (44 loc) 1.61 kB
import * as apolloImport from '@apollo/client'; import { fakePromise, isAsyncIterable } from '@graphql-tools/utils'; const apollo = apolloImport?.default ?? apolloImport; function createApolloRequestHandler(executor) { return function ApolloRequestHandler(operation) { return new apollo.Observable(observer => { fakePromise() .then(() => executor({ document: operation.query, variables: operation.variables, operationName: operation.operationName, extensions: operation.extensions, context: operation.getContext(), })) .then(results => { if (isAsyncIterable(results)) { return fakePromise().then(async () => { for await (const result of results) { if (observer.closed) { return; } observer.next(result); } observer.complete(); }); } else if (!observer.closed) { observer.next(results); observer.complete(); } }) .catch(e => { if (!observer.closed) { observer.error(e); } }); }); }; } export class ExecutorLink extends apollo.ApolloLink { constructor(executor) { super(createApolloRequestHandler(executor)); } }