UNPKG

3.37 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2import { DocumentNode } from '../language/ast';
3import {
4 ExecutionResult,
5 ExecutionResultDataDefault,
6} from '../execution/execute';
7import { GraphQLSchema } from '../type/schema';
8import { GraphQLFieldResolver } from '../type/definition';
9
10export interface SubscriptionArgs {
11 schema: GraphQLSchema;
12 document: DocumentNode;
13 rootValue?: any;
14 contextValue?: any;
15 variableValues?: Maybe<Record<string, any>>;
16 operationName?: Maybe<string>;
17 fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
18 subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
19}
20
21/**
22 * Implements the "Subscribe" algorithm described in the GraphQL specification.
23 *
24 * Returns a Promise which resolves to either an AsyncIterator (if successful)
25 * or an ExecutionResult (client error). The promise will be rejected if a
26 * server error occurs.
27 *
28 * If the client-provided arguments to this function do not result in a
29 * compliant subscription, a GraphQL Response (ExecutionResult) with
30 * descriptive errors and no data will be returned.
31 *
32 * If the the source stream could not be created due to faulty subscription
33 * resolver logic or underlying systems, the promise will resolve to a single
34 * ExecutionResult containing `errors` and no `data`.
35 *
36 * If the operation succeeded, the promise resolves to an AsyncIterator, which
37 * yields a stream of ExecutionResults representing the response stream.
38 *
39 * Accepts either an object with named arguments, or individual arguments.
40 */
41export function subscribe<TData = ExecutionResultDataDefault>(
42 args: SubscriptionArgs,
43): Promise<
44 AsyncIterableIterator<ExecutionResult<TData>> | ExecutionResult<TData>
45>;
46
47export function subscribe<TData = ExecutionResultDataDefault>(
48 schema: GraphQLSchema,
49 document: DocumentNode,
50 rootValue?: any,
51 contextValue?: any,
52 variableValues?: Maybe<{ [key: string]: any }>,
53 operationName?: Maybe<string>,
54 fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
55 subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
56): Promise<
57 AsyncIterableIterator<ExecutionResult<TData>> | ExecutionResult<TData>
58>;
59
60/**
61 * Implements the "CreateSourceEventStream" algorithm described in the
62 * GraphQL specification, resolving the subscription source event stream.
63 *
64 * Returns a Promise<AsyncIterable>.
65 *
66 * If the client-provided invalid arguments, the source stream could not be
67 * created, or the resolver did not return an AsyncIterable, this function will
68 * will throw an error, which should be caught and handled by the caller.
69 *
70 * A Source Event Stream represents a sequence of events, each of which triggers
71 * a GraphQL execution for that event.
72 *
73 * This may be useful when hosting the stateful subscription service in a
74 * different process or machine than the stateless GraphQL execution engine,
75 * or otherwise separating these two steps. For more on this, see the
76 * "Supporting Subscriptions at Scale" information in the GraphQL specification.
77 */
78export function createSourceEventStream<TData = ExecutionResultDataDefault>(
79 schema: GraphQLSchema,
80 document: DocumentNode,
81 rootValue?: any,
82 contextValue?: any,
83 variableValues?: { [key: string]: any },
84 operationName?: Maybe<string>,
85 fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
86): Promise<AsyncIterable<any> | ExecutionResult<TData>>;