123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true,
- });
- exports.createSourceEventStream = createSourceEventStream;
- exports.subscribe = subscribe;
- var _devAssert = require('../jsutils/devAssert.js');
- var _inspect = require('../jsutils/inspect.js');
- var _isAsyncIterable = require('../jsutils/isAsyncIterable.js');
- var _Path = require('../jsutils/Path.js');
- var _GraphQLError = require('../error/GraphQLError.js');
- var _locatedError = require('../error/locatedError.js');
- var _collectFields = require('./collectFields.js');
- var _execute = require('./execute.js');
- var _mapAsyncIterator = require('./mapAsyncIterator.js');
- var _values = require('./values.js');
- async function subscribe(args) {
-
- arguments.length < 2 ||
- (0, _devAssert.devAssert)(
- false,
- 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
- );
- const resultOrStream = await createSourceEventStream(args);
- if (!(0, _isAsyncIterable.isAsyncIterable)(resultOrStream)) {
- return resultOrStream;
- }
-
-
-
-
-
- const mapSourceToResponse = (payload) =>
- (0, _execute.execute)({ ...args, rootValue: payload });
- return (0, _mapAsyncIterator.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],
- };
- }
- async function createSourceEventStream(...rawArgs) {
- const args = toNormalizedArgs(rawArgs);
- const { schema, document, variableValues } = args;
-
- (0, _execute.assertValidExecutionArguments)(schema, document, variableValues);
-
- const exeContext = (0, _execute.buildExecutionContext)(args);
- if (!('schema' in exeContext)) {
- return {
- errors: exeContext,
- };
- }
- try {
- const eventStream = await executeSubscription(exeContext);
- if (!(0, _isAsyncIterable.isAsyncIterable)(eventStream)) {
- throw new Error(
- 'Subscription field must return Async Iterable. ' +
- `Received: ${(0, _inspect.inspect)(eventStream)}.`,
- );
- }
- return eventStream;
- } catch (error) {
-
-
- if (error instanceof _GraphQLError.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.GraphQLError(
- 'Schema is not configured to execute subscription operation.',
- {
- nodes: operation,
- },
- );
- }
- const rootFields = (0, _collectFields.collectFields)(
- schema,
- fragments,
- variableValues,
- rootType,
- operation.selectionSet,
- );
- const [responseName, fieldNodes] = [...rootFields.entries()][0];
- const fieldDef = (0, _execute.getFieldDef)(schema, rootType, fieldNodes[0]);
- if (!fieldDef) {
- const fieldName = fieldNodes[0].name.value;
- throw new _GraphQLError.GraphQLError(
- `The subscription field "${fieldName}" is not defined.`,
- {
- nodes: fieldNodes,
- },
- );
- }
- const path = (0, _Path.addPath)(undefined, responseName, rootType.name);
- const info = (0, _execute.buildResolveInfo)(
- exeContext,
- fieldDef,
- fieldNodes,
- rootType,
- path,
- );
- try {
- var _fieldDef$subscribe;
-
-
-
-
- const args = (0, _values.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 (0, _locatedError.locatedError)(
- error,
- fieldNodes,
- (0, _Path.pathToArray)(path),
- );
- }
- }
|