Skip to main content

subscribe

Callable

  • subscribe<TMaybeIncremental>(args: ExecutionArgs): PromiseOrValue<AsyncGenerator<MaybeIncrementalPayload<TMaybeIncremental>, void, void> | ExecutionResult>

  • 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.

    This function also supports experimental incremental delivery directives (@defer and @stream). To use these directives, they should be added to the schema and TS generic parameter TMaybeIncremental should be set to true (default: false).

    Note: At runtime, the schema is not checked as to whether these directives are actually present; all operations passed to execute are assumed to be valid.

    In that case, each payload may be an ExecutionResult with no hasNext (if executing the event did not use @defer or @stream), or an InitialIncrementalExecutionResult or SubsequentIncrementalExecutionResult (if executing the event used @defer or @stream). In the case of incremental execution results, each event produces a single InitialIncrementalExecutionResult followed by one or more SubsequentIncrementalExecutionResults; all but the last have hasNext: true, and the last has hasNext: false. There is no interleaving between results generated from the same original event.

    Accepts an object with named arguments.


    Type parameters

    • TMaybeIncremental: boolean = false

    Parameters

    Returns PromiseOrValue<AsyncGenerator<MaybeIncrementalPayload<TMaybeIncremental>, void, void> | ExecutionResult>