UNPKG

@graphql-yoga/apollo-link

Version:
55 lines (54 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YogaLink = void 0; const tslib_1 = require("tslib"); const apolloImport = tslib_1.__importStar(require("@apollo/client")); const url_loader_1 = require("@graphql-tools/url-loader"); const utils_1 = require("@graphql-tools/utils"); const apollo = apolloImport?.default ?? apolloImport; function createYogaApolloRequestHandler(options) { const urlLoader = new url_loader_1.UrlLoader(); const executor = urlLoader.getExecutorAsync(options.endpoint, { subscriptionsProtocol: url_loader_1.SubscriptionProtocol.SSE, multipart: true, ...options, }); return function graphQLYogaApolloRequestHandler(operation) { return new apollo.Observable((observer) => { const executionRequest = { document: operation.query, variables: operation.variables, operationName: operation.operationName, extensions: operation.extensions, context: operation.getContext(), }; executor(executionRequest) .then(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(); } }) .catch((error) => { if (!observer.closed) { observer.error(error); } }); }); }; } class YogaLink extends apollo.ApolloLink { constructor(options) { super(createYogaApolloRequestHandler(options)); } } exports.YogaLink = YogaLink;