123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- import { isAsyncIterable } from 'iterall';
- import inspect from '../jsutils/inspect';
- import { addPath, pathToArray } from '../jsutils/Path';
- import { GraphQLError } from '../error/GraphQLError';
- import { locatedError } from '../error/locatedError';
- import { type DocumentNode } from '../language/ast';
- import {
- type ExecutionResult,
- assertValidExecutionArguments,
- buildExecutionContext,
- buildResolveInfo,
- collectFields,
- execute,
- getFieldDef,
- resolveFieldValueOrError,
- } from '../execution/execute';
- import { type GraphQLSchema } from '../type/schema';
- import { type GraphQLFieldResolver } from '../type/definition';
- import { getOperationRootType } from '../utilities/getOperationRootType';
- import mapAsyncIterator from './mapAsyncIterator';
- export type SubscriptionArgs = {|
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue?: mixed,
- contextValue?: mixed,
- variableValues?: ?{ +[variable: string]: mixed, ... },
- operationName?: ?string,
- fieldResolver?: ?GraphQLFieldResolver<any, any>,
- subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>,
- |};
- declare function subscribe(
- SubscriptionArgs,
- ..._: []
- ): Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
- declare function subscribe(
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue?: mixed,
- contextValue?: mixed,
- variableValues?: ?{ +[variable: string]: mixed, ... },
- operationName?: ?string,
- fieldResolver?: ?GraphQLFieldResolver<any, any>,
- subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>,
- ): Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
- export function subscribe(
- argsOrSchema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- subscribeFieldResolver,
- ) {
-
-
- return arguments.length === 1
- ? subscribeImpl(argsOrSchema)
- : subscribeImpl({
- schema: argsOrSchema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- subscribeFieldResolver,
- });
- }
- function reportGraphQLError(error) {
- if (error instanceof GraphQLError) {
- return { errors: [error] };
- }
- throw error;
- }
- function subscribeImpl(
- args: SubscriptionArgs,
- ): Promise<AsyncIterator<ExecutionResult> | ExecutionResult> {
- const {
- schema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- subscribeFieldResolver,
- } = args;
- const sourcePromise = createSourceEventStream(
- schema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- subscribeFieldResolver,
- );
-
-
-
-
-
-
- const mapSourceToResponse = payload =>
- execute(
- schema,
- document,
- payload,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- );
-
-
- return sourcePromise.then(resultOrStream =>
-
- isAsyncIterable(resultOrStream)
- ? mapAsyncIterator(
- ((resultOrStream: any): AsyncIterable<mixed>),
- mapSourceToResponse,
- reportGraphQLError,
- )
- : ((resultOrStream: any): ExecutionResult),
- );
- }
- export function createSourceEventStream(
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue?: mixed,
- contextValue?: mixed,
- variableValues?: ?{ +[variable: string]: mixed, ... },
- operationName?: ?string,
- fieldResolver?: ?GraphQLFieldResolver<any, any>,
- ): Promise<AsyncIterable<mixed> | ExecutionResult> {
-
-
- assertValidExecutionArguments(schema, document, variableValues);
- try {
-
-
- const exeContext = buildExecutionContext(
- schema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- );
-
- if (Array.isArray(exeContext)) {
- return Promise.resolve({ errors: exeContext });
- }
- const type = getOperationRootType(schema, exeContext.operation);
- const fields = collectFields(
- exeContext,
- type,
- exeContext.operation.selectionSet,
- Object.create(null),
- Object.create(null),
- );
- const responseNames = Object.keys(fields);
- const responseName = responseNames[0];
- const fieldNodes = fields[responseName];
- const fieldNode = fieldNodes[0];
- const fieldName = fieldNode.name.value;
- const fieldDef = getFieldDef(schema, type, fieldName);
- if (!fieldDef) {
- throw new GraphQLError(
- `The subscription field "${fieldName}" is not defined.`,
- fieldNodes,
- );
- }
-
-
- const resolveFn = fieldDef.subscribe || exeContext.fieldResolver;
- const path = addPath(undefined, responseName);
- const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path);
-
-
-
- const result = resolveFieldValueOrError(
- exeContext,
- fieldDef,
- fieldNodes,
- resolveFn,
- rootValue,
- info,
- );
-
- return Promise.resolve(result).then(eventStream => {
-
- if (eventStream instanceof Error) {
- return {
- errors: [locatedError(eventStream, fieldNodes, pathToArray(path))],
- };
- }
-
- if (isAsyncIterable(eventStream)) {
-
- return ((eventStream: any): AsyncIterable<mixed>);
- }
- throw new Error(
- 'Subscription field must return Async Iterable. Received: ' +
- inspect(eventStream),
- );
- });
- } catch (error) {
-
-
-
- return error instanceof GraphQLError
- ? Promise.resolve({ errors: [error] })
- : Promise.reject(error);
- }
- }
|