@graphql-tools/executor-apollo-link
Version:
A set of utils for faster development of GraphQL tools
44 lines (43 loc) • 1.64 kB
JavaScript
var _a;
import * as apolloImport from '@apollo/client';
import { isAsyncIterable } from '@graphql-tools/utils';
const apollo = (_a = apolloImport === null || apolloImport === void 0 ? void 0 : apolloImport.default) !== null && _a !== void 0 ? _a : apolloImport;
function createApolloRequestHandler(executor) {
return function ApolloRequestHandler(operation) {
return new apollo.Observable(observer => {
const executionRequest = {
document: operation.query,
variables: operation.variables,
operationName: operation.operationName,
extensions: operation.extensions,
context: operation.getContext(),
};
Promise.resolve(executor(executionRequest))
.then(async (results) => {
if (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);
}
});
});
};
}
export class ExecutorLink extends apollo.ApolloLink {
constructor(executor) {
super(createApolloRequestHandler(executor));
}
}