123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- import { devAssert } from '../jsutils/devAssert.mjs';
- import { inspect } from '../jsutils/inspect.mjs';
- import { isAsyncIterable } from '../jsutils/isAsyncIterable.mjs';
- import { addPath, pathToArray } from '../jsutils/Path.mjs';
- import { GraphQLError } from '../error/GraphQLError.mjs';
- import { locatedError } from '../error/locatedError.mjs';
- import { collectFields } from './collectFields.mjs';
- import {
- assertValidExecutionArguments,
- buildExecutionContext,
- buildResolveInfo,
- execute,
- getFieldDef,
- } from './execute.mjs';
- import { mapAsyncIterator } from './mapAsyncIterator.mjs';
- import { getArgumentValues } from './values.mjs';
- export async function subscribe(args) {
-
- arguments.length < 2 ||
- devAssert(
- false,
- 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
- );
- const resultOrStream = await createSourceEventStream(args);
- if (!isAsyncIterable(resultOrStream)) {
- return resultOrStream;
- }
-
-
-
-
-
- const mapSourceToResponse = (payload) =>
- execute({ ...args, rootValue: payload });
- return mapAsyncIterator(resultOrStream, mapSourceToResponse);
- }
- function toNormalizedArgs(args) {
- const firstArg = args[0];
- if (firstArg && 'document' in firstArg) {
- return firstArg;
- }
- return {
- schema: firstArg,
-
- document: args[1],
- rootValue: args[2],
- contextValue: args[3],
- variableValues: args[4],
- operationName: args[5],
- subscribeFieldResolver: args[6],
- };
- }
- export async function createSourceEventStream(...rawArgs) {
- const args = toNormalizedArgs(rawArgs);
- const { schema, document, variableValues } = args;
-
- assertValidExecutionArguments(schema, document, variableValues);
-
- const exeContext = buildExecutionContext(args);
- if (!('schema' in exeContext)) {
- return {
- errors: exeContext,
- };
- }
- try {
- const eventStream = await executeSubscription(exeContext);
- if (!isAsyncIterable(eventStream)) {
- throw new Error(
- 'Subscription field must return Async Iterable. ' +
- `Received: ${inspect(eventStream)}.`,
- );
- }
- return eventStream;
- } catch (error) {
-
-
- if (error instanceof GraphQLError) {
- return {
- errors: [error],
- };
- }
- throw error;
- }
- }
- async function executeSubscription(exeContext) {
- const { schema, fragments, operation, variableValues, rootValue } =
- exeContext;
- const rootType = schema.getSubscriptionType();
- if (rootType == null) {
- throw new GraphQLError(
- 'Schema is not configured to execute subscription operation.',
- {
- nodes: operation,
- },
- );
- }
- const rootFields = collectFields(
- schema,
- fragments,
- variableValues,
- rootType,
- operation.selectionSet,
- );
- const [responseName, fieldNodes] = [...rootFields.entries()][0];
- const fieldDef = getFieldDef(schema, rootType, fieldNodes[0]);
- if (!fieldDef) {
- const fieldName = fieldNodes[0].name.value;
- throw new GraphQLError(
- `The subscription field "${fieldName}" is not defined.`,
- {
- nodes: fieldNodes,
- },
- );
- }
- const path = addPath(undefined, responseName, rootType.name);
- const info = buildResolveInfo(
- exeContext,
- fieldDef,
- fieldNodes,
- rootType,
- path,
- );
- try {
- var _fieldDef$subscribe;
-
-
-
-
- const args = getArgumentValues(fieldDef, fieldNodes[0], variableValues);
-
-
- const contextValue = exeContext.contextValue;
-
- const resolveFn =
- (_fieldDef$subscribe = fieldDef.subscribe) !== null &&
- _fieldDef$subscribe !== void 0
- ? _fieldDef$subscribe
- : exeContext.subscribeFieldResolver;
- const eventStream = await resolveFn(rootValue, args, contextValue, info);
- if (eventStream instanceof Error) {
- throw eventStream;
- }
- return eventStream;
- } catch (error) {
- throw locatedError(error, fieldNodes, pathToArray(path));
- }
- }
|