@graphql-tools/executor-apollo-link
Version:
A set of utils for faster development of GraphQL tools
52 lines (51 loc) • 1.94 kB
JavaScript
import * as apolloImport from '@apollo/client';
import { isAsyncIterable } from '@graphql-tools/utils';
const apollo = apolloImport?.default ?? 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(),
};
let disposed = false;
let dispose = () => {
disposed = true;
};
(async function execution() {
const results = await executor(executionRequest);
// request couldve been disposed before getting results
if (disposed)
return;
if (isAsyncIterable(results)) {
const asyncIterator = results[Symbol.asyncIterator]();
dispose = () => {
asyncIterator.return?.();
};
try {
for await (const result of { [Symbol.asyncIterator]: () => asyncIterator }) {
observer.next(result);
}
}
catch (e) {
observer.error(e);
}
}
else {
observer.next(results);
}
})()
.then(() => observer.complete())
.catch(e => observer.error(e));
return () => dispose();
});
};
}
export class ExecutorLink extends apollo.ApolloLink {
constructor(executor) {
super(createApolloRequestHandler(executor));
}
}