UNPKG

@graphql-mesh/apollo-link

Version:
50 lines (49 loc) 1.95 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 => { Promise.resolve() .then(async () => { const results = await operationFn(operation.query, operation.variables, operation.getContext(), ROOT_VALUE, operation.operationName); 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(); } } }) .catch(error => { if (!observer.closed) { observer.error(error); } }); }); }; } class MeshApolloLink extends apolloClient.ApolloLink { constructor(options) { super(createMeshApolloRequestHandler(options)); } } exports.MeshApolloLink = MeshApolloLink;