UNPKG

@graphql-tools/executor-apollo-link

Version:

A set of utils for faster development of GraphQL tools

50 lines (49 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExecutorLink = void 0; const tslib_1 = require("tslib"); const apolloImport = tslib_1.__importStar(require("@apollo/client")); const utils_1 = require("@graphql-tools/utils"); const apollo = apolloImport?.default ?? apolloImport; function createApolloRequestHandler(executor) { return function ApolloRequestHandler(operation) { return new apollo.Observable(observer => { (0, utils_1.fakePromise)() .then(() => executor({ document: operation.query, variables: operation.variables, operationName: operation.operationName, extensions: operation.extensions, context: operation.getContext(), })) .then(results => { if ((0, utils_1.isAsyncIterable)(results)) { return (0, utils_1.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); } }); }); }; } class ExecutorLink extends apollo.ApolloLink { constructor(executor) { super(createApolloRequestHandler(executor)); } } exports.ExecutorLink = ExecutorLink;