UNPKG

@graphql-mesh/apollo-link

Version:
47 lines (46 loc) 1.88 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 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 => { (0, utils_1.mapMaybePromise)(operationFn(operation.query, operation.variables, operation.getContext(), ROOT_VALUE, operation.operationName), async (results) => { if ((0, utils_1.isAsyncIterable)(results)) { for await (const result of results) { if (observer.closed) { return; } observer.next(result); } observer.complete(); } else { 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;