UNPKG

@graphql-mesh/apollo-link

Version:
49 lines (48 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MeshApolloLink = void 0; const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const apolloClient = tslib_1.__importStar(require("@apollo/client")); const utils_1 = require("@graphql-tools/utils"); const promise_helpers_1 = require("@whatwg-node/promise-helpers"); const ROOT_VALUE = {}; function createMeshApolloRequestHandler(options) { return function meshApolloRequestHandler(operation) { const operationAst = (0, graphql_1.getOperationAST)(operation.query, operation.operationName); if (!operationAst) { throw new Error('GraphQL operation not found'); } const operationFn = operationAst.operation === 'subscription' ? options.subscribe : options.execute; return new apolloClient.Observable(observer => { // eslint-disable-next-line @typescript-eslint/no-floating-promises (0, promise_helpers_1.handleMaybePromise)(() => operationFn(operation.query, operation.variables, operation.getContext(), ROOT_VALUE, operation.operationName), 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(); }); } if (!observer.closed) { observer.next(results); observer.complete(); } }, error => { if (!observer.closed) { observer.error(error); } }); }); }; } class MeshApolloLink extends apolloClient.ApolloLink { constructor(options) { super(createMeshApolloRequestHandler(options)); } } exports.MeshApolloLink = MeshApolloLink;