UNPKG

@graphql-tools/executor

Version:

Fork of GraphQL.js' execute function

55 lines (54 loc) 1.85 kB
import { isAsyncIterable } from '@graphql-tools/utils'; import { getOperationAST } from 'graphql'; import { execute, subscribe } from './execute.js'; import { Repeater } from '@repeaterjs/repeater'; import { ValueOrPromise } from 'value-or-promise'; export function normalizedExecutor(args) { const operationAST = getOperationAST(args.document, args.operationName); if (operationAST == null) { throw new Error('Must provide an operation.'); } if (operationAST.operation === 'subscription') { return new ValueOrPromise(() => subscribe(args)) .then((result) => { if (isAsyncIterable(result)) { return new Repeater(async (push, stop) => { let stopped = false; stop.then(() => { stopped = true; }); for await (const value of result) { if (stopped) { break; } push(value); } stop(); }); } return result; }) .resolve(); } return new ValueOrPromise(() => execute(args)) .then((result) => { if ('initialResult' in result) { return new Repeater(async (push, stop) => { let stopped = false; stop.then(() => { stopped = true; }); push(result.initialResult); for await (const value of result.subsequentResults) { if (stopped) { break; } push(value); } stop(); }); } return result; }) .resolve(); }