UNPKG

@graphql-tools/graphql

Version:
166 lines (165 loc) 8.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSourceEventStream = exports.subscribe = void 0; const devAssert_js_1 = require("../jsutils/devAssert.js"); const inspect_js_1 = require("../jsutils/inspect.js"); const isAsyncIterable_js_1 = require("../jsutils/isAsyncIterable.js"); const Path_js_1 = require("../jsutils/Path.js"); const GraphQLError_js_1 = require("../error/GraphQLError.js"); const locatedError_js_1 = require("../error/locatedError.js"); const collectFields_js_1 = require("./collectFields.js"); const execute_js_1 = require("./execute.js"); const mapAsyncIterator_js_1 = require("./mapAsyncIterator.js"); const values_js_1 = require("./values.js"); /** * Implements the "Subscribe" algorithm described in the GraphQL specification. * * Returns a Promise which resolves to either an AsyncIterator (if successful) * or an ExecutionResult (error). The promise will be rejected if the schema or * other arguments to this function are invalid, or if the resolved event stream * is not an async iterable. * * If the client-provided arguments to this function do not result in a * compliant subscription, a GraphQL Response (ExecutionResult) with * descriptive errors and no data will be returned. * * If the source stream could not be created due to faulty subscription * resolver logic or underlying systems, the promise will resolve to a single * ExecutionResult containing `errors` and no `data`. * * If the operation succeeded, the promise resolves to an AsyncIterator, which * yields a stream of ExecutionResults representing the response stream. * * Accepts either an object with named arguments, or individual arguments. */ async function subscribe(args) { // Temporary for v15 to v16 migration. Remove in v17 (0, devAssert_js_1.devAssert)(arguments.length < 2, 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.'); const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver, } = args; const resultOrStream = await createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver); if (!(0, isAsyncIterable_js_1.isAsyncIterable)(resultOrStream)) { return resultOrStream; } // For each payload yielded from a subscription, map it over the normal // GraphQL `execute` function, with `payload` as the rootValue. // This implements the "MapSourceToResponseEvent" algorithm described in // the GraphQL specification. The `execute` function provides the // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the // "ExecuteQuery" algorithm, for which `execute` is also used. const mapSourceToResponse = (payload) => (0, execute_js_1.execute)({ schema, document, rootValue: payload, contextValue, variableValues, operationName, fieldResolver, }); // Map every source value to a ExecutionResult value as described above. return (0, mapAsyncIterator_js_1.mapAsyncIterator)(resultOrStream, mapSourceToResponse); } exports.subscribe = subscribe; /** * Implements the "CreateSourceEventStream" algorithm described in the * GraphQL specification, resolving the subscription source event stream. * * Returns a Promise which resolves to either an AsyncIterable (if successful) * or an ExecutionResult (error). The promise will be rejected if the schema or * other arguments to this function are invalid, or if the resolved event stream * is not an async iterable. * * If the client-provided arguments to this function do not result in a * compliant subscription, a GraphQL Response (ExecutionResult) with * descriptive errors and no data will be returned. * * If the the source stream could not be created due to faulty subscription * resolver logic or underlying systems, the promise will resolve to a single * ExecutionResult containing `errors` and no `data`. * * If the operation succeeded, the promise resolves to the AsyncIterable for the * event stream returned by the resolver. * * A Source Event Stream represents a sequence of events, each of which triggers * a GraphQL execution for that event. * * This may be useful when hosting the stateful subscription service in a * different process or machine than the stateless GraphQL execution engine, * or otherwise separating these two steps. For more on this, see the * "Supporting Subscriptions at Scale" information in the GraphQL specification. */ async function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver) { // If arguments are missing or incorrectly typed, this is an internal // developer mistake which should throw an early error. (0, execute_js_1.assertValidExecutionArguments)(schema, document, variableValues); // If a valid execution context cannot be created due to incorrect arguments, // a "Response" with only errors is returned. const exeContext = (0, execute_js_1.buildExecutionContext)({ schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver, }); // Return early errors if execution context failed. if (!('schema' in exeContext)) { return { errors: exeContext }; } try { const eventStream = await executeSubscription(exeContext); // Assert field returned an event stream, otherwise yield an error. if (!(0, isAsyncIterable_js_1.isAsyncIterable)(eventStream)) { throw new Error('Subscription field must return Async Iterable. ' + `Received: ${(0, inspect_js_1.inspect)(eventStream)}.`); } return eventStream; } catch (error) { // If it GraphQLError, report it as an ExecutionResult, containing only errors and no data. // Otherwise treat the error as a system-class error and re-throw it. if (error instanceof GraphQLError_js_1.GraphQLError) { return { errors: [error] }; } throw error; } } exports.createSourceEventStream = createSourceEventStream; async function executeSubscription(exeContext) { var _a; const { schema, fragments, operation, variableValues, rootValue } = exeContext; const rootType = schema.getSubscriptionType(); if (rootType == null) { throw new GraphQLError_js_1.GraphQLError('Schema is not configured to execute subscription operation.', { nodes: operation }); } const rootFields = (0, collectFields_js_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet); const [responseName, fieldNodes] = [...rootFields.entries()][0]; const fieldDef = (0, execute_js_1.getFieldDef)(schema, rootType, fieldNodes[0]); if (!fieldDef) { const fieldName = fieldNodes[0].name.value; throw new GraphQLError_js_1.GraphQLError(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes }); } const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name); const info = (0, execute_js_1.buildResolveInfo)(exeContext, fieldDef, fieldNodes, rootType, path); try { // Implements the "ResolveFieldEventStream" algorithm from GraphQL specification. // It differs from "ResolveFieldValue" due to providing a different `resolveFn`. // Build a JS object of arguments from the field.arguments AST, using the // variables scope to fulfill any variable references. const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues); // The resolve function's optional third argument is a context value that // is provided to every resolve function within an execution. It is commonly // used to represent an authenticated user, or request-specific caches. const contextValue = exeContext.contextValue; // Call the `subscribe()` resolver or the default resolver to produce an // AsyncIterable yielding raw payloads. const resolveFn = (_a = fieldDef.subscribe) !== null && _a !== void 0 ? _a : exeContext.subscribeFieldResolver; const eventStream = await resolveFn(rootValue, args, contextValue, info); if (eventStream instanceof Error) { throw eventStream; } return eventStream; } catch (error) { throw (0, locatedError_js_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path)); } }