123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.subscribe = subscribe;
- exports.createSourceEventStream = createSourceEventStream;
- var _iterall = require("iterall");
- var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
- var _Path = require("../jsutils/Path");
- var _GraphQLError = require("../error/GraphQLError");
- var _locatedError = require("../error/locatedError");
- var _execute = require("../execution/execute");
- var _getOperationRootType = require("../utilities/getOperationRootType");
- var _mapAsyncIterator = _interopRequireDefault(require("./mapAsyncIterator"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- 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.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 (0, _execute.execute)(schema, document, payload, contextValue, variableValues, operationName, fieldResolver);
- };
-
- return sourcePromise.then(function (resultOrStream) {
- return (
- (0, _iterall.isAsyncIterable)(resultOrStream) ? (0, _mapAsyncIterator.default)(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream
- );
- });
- }
- function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) {
-
-
- (0, _execute.assertValidExecutionArguments)(schema, document, variableValues);
- try {
-
-
- var exeContext = (0, _execute.buildExecutionContext)(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver);
- if (Array.isArray(exeContext)) {
- return Promise.resolve({
- errors: exeContext
- });
- }
- var type = (0, _getOperationRootType.getOperationRootType)(schema, exeContext.operation);
- var fields = (0, _execute.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 = (0, _execute.getFieldDef)(schema, type, fieldName);
- if (!fieldDef) {
- throw new _GraphQLError.GraphQLError("The subscription field \"".concat(fieldName, "\" is not defined."), fieldNodes);
- }
-
- var resolveFn = fieldDef.subscribe || exeContext.fieldResolver;
- var path = (0, _Path.addPath)(undefined, responseName);
- var info = (0, _execute.buildResolveInfo)(exeContext, fieldDef, fieldNodes, type, path);
-
-
- var result = (0, _execute.resolveFieldValueOrError)(exeContext, fieldDef, fieldNodes, resolveFn, rootValue, info);
- return Promise.resolve(result).then(function (eventStream) {
-
- if (eventStream instanceof Error) {
- return {
- errors: [(0, _locatedError.locatedError)(eventStream, fieldNodes, (0, _Path.pathToArray)(path))]
- };
- }
- if ((0, _iterall.isAsyncIterable)(eventStream)) {
-
- return eventStream;
- }
- throw new Error('Subscription field must return Async Iterable. Received: ' + (0, _inspect.default)(eventStream));
- });
- } catch (error) {
-
-
-
- return error instanceof _GraphQLError.GraphQLError ? Promise.resolve({
- errors: [error]
- }) : Promise.reject(error);
- }
- }
|