123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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 { assertValidExecutionArguments, buildExecutionContext, buildResolveInfo, collectFields, execute, getFieldDef, resolveFieldValueOrError } from '../execution/execute';
- import { getOperationRootType } from '../utilities/getOperationRootType';
- import mapAsyncIterator from './mapAsyncIterator';
- export function subscribe(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) {
-
-
- return arguments.length === 1 ? subscribeImpl(argsOrSchema) : subscribeImpl({
- schema: argsOrSchema,
- document: document,
- rootValue: rootValue,
- contextValue: contextValue,
- variableValues: variableValues,
- operationName: operationName,
- fieldResolver: fieldResolver,
- subscribeFieldResolver: subscribeFieldResolver
- });
- }
- function reportGraphQLError(error) {
- if (error instanceof GraphQLError) {
- return {
- errors: [error]
- };
- }
- throw error;
- }
- function subscribeImpl(args) {
- var schema = args.schema,
- document = args.document,
- rootValue = args.rootValue,
- contextValue = args.contextValue,
- variableValues = args.variableValues,
- operationName = args.operationName,
- fieldResolver = args.fieldResolver,
- subscribeFieldResolver = args.subscribeFieldResolver;
- var sourcePromise = createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver);
-
-
-
-
-
- var mapSourceToResponse = function mapSourceToResponse(payload) {
- return execute(schema, document, payload, contextValue, variableValues, operationName, fieldResolver);
- };
-
- return sourcePromise.then(function (resultOrStream) {
- return (
- isAsyncIterable(resultOrStream) ? mapAsyncIterator(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream
- );
- });
- }
- export function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) {
-
-
- assertValidExecutionArguments(schema, document, variableValues);
- try {
-
-
- var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver);
- if (Array.isArray(exeContext)) {
- return Promise.resolve({
- errors: exeContext
- });
- }
- var type = getOperationRootType(schema, exeContext.operation);
- var fields = collectFields(exeContext, type, exeContext.operation.selectionSet, Object.create(null), Object.create(null));
- var responseNames = Object.keys(fields);
- var responseName = responseNames[0];
- var fieldNodes = fields[responseName];
- var fieldNode = fieldNodes[0];
- var fieldName = fieldNode.name.value;
- var fieldDef = getFieldDef(schema, type, fieldName);
- if (!fieldDef) {
- throw new GraphQLError("The subscription field \"".concat(fieldName, "\" is not defined."), fieldNodes);
- }
-
- var resolveFn = fieldDef.subscribe || exeContext.fieldResolver;
- var path = addPath(undefined, responseName);
- var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path);
-
-
- var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, rootValue, info);
- return Promise.resolve(result).then(function (eventStream) {
-
- if (eventStream instanceof Error) {
- return {
- errors: [locatedError(eventStream, fieldNodes, pathToArray(path))]
- };
- }
- if (isAsyncIterable(eventStream)) {
-
- return eventStream;
- }
- 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);
- }
- }
|