1 | import { isAsyncIterable } from 'iterall';
|
2 | import inspect from '../jsutils/inspect';
|
3 | import { addPath, pathToArray } from '../jsutils/Path';
|
4 | import { GraphQLError } from '../error/GraphQLError';
|
5 | import { locatedError } from '../error/locatedError';
|
6 | import { assertValidExecutionArguments, buildExecutionContext, buildResolveInfo, collectFields, execute, getFieldDef, resolveFieldValueOrError } from '../execution/execute';
|
7 | import { getOperationRootType } from '../utilities/getOperationRootType';
|
8 | import mapAsyncIterator from './mapAsyncIterator';
|
9 | export function subscribe(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) {
|
10 |
|
11 |
|
12 | return arguments.length === 1 ? subscribeImpl(argsOrSchema) : subscribeImpl({
|
13 | schema: argsOrSchema,
|
14 | document: document,
|
15 | rootValue: rootValue,
|
16 | contextValue: contextValue,
|
17 | variableValues: variableValues,
|
18 | operationName: operationName,
|
19 | fieldResolver: fieldResolver,
|
20 | subscribeFieldResolver: subscribeFieldResolver
|
21 | });
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function reportGraphQLError(error) {
|
30 | if (error instanceof GraphQLError) {
|
31 | return {
|
32 | errors: [error]
|
33 | };
|
34 | }
|
35 |
|
36 | throw error;
|
37 | }
|
38 |
|
39 | function subscribeImpl(args) {
|
40 | var schema = args.schema,
|
41 | document = args.document,
|
42 | rootValue = args.rootValue,
|
43 | contextValue = args.contextValue,
|
44 | variableValues = args.variableValues,
|
45 | operationName = args.operationName,
|
46 | fieldResolver = args.fieldResolver,
|
47 | subscribeFieldResolver = args.subscribeFieldResolver;
|
48 | var sourcePromise = createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver);
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var mapSourceToResponse = function mapSourceToResponse(payload) {
|
56 | return execute(schema, document, payload, contextValue, variableValues, operationName, fieldResolver);
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 | return sourcePromise.then(function (resultOrStream) {
|
62 | return (
|
63 | isAsyncIterable(resultOrStream) ? mapAsyncIterator(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream
|
64 | );
|
65 | });
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | export function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) {
|
98 |
|
99 |
|
100 | assertValidExecutionArguments(schema, document, variableValues);
|
101 |
|
102 | try {
|
103 |
|
104 |
|
105 | var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver);
|
106 |
|
107 | if (Array.isArray(exeContext)) {
|
108 | return Promise.resolve({
|
109 | errors: exeContext
|
110 | });
|
111 | }
|
112 |
|
113 | var type = getOperationRootType(schema, exeContext.operation);
|
114 | var fields = collectFields(exeContext, type, exeContext.operation.selectionSet, Object.create(null), Object.create(null));
|
115 | var responseNames = Object.keys(fields);
|
116 | var responseName = responseNames[0];
|
117 | var fieldNodes = fields[responseName];
|
118 | var fieldNode = fieldNodes[0];
|
119 | var fieldName = fieldNode.name.value;
|
120 | var fieldDef = getFieldDef(schema, type, fieldName);
|
121 |
|
122 | if (!fieldDef) {
|
123 | throw new GraphQLError("The subscription field \"".concat(fieldName, "\" is not defined."), fieldNodes);
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | var resolveFn = fieldDef.subscribe || exeContext.fieldResolver;
|
129 | var path = addPath(undefined, responseName);
|
130 | var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path);
|
131 |
|
132 |
|
133 |
|
134 | var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, rootValue, info);
|
135 |
|
136 | return Promise.resolve(result).then(function (eventStream) {
|
137 |
|
138 | if (eventStream instanceof Error) {
|
139 | return {
|
140 | errors: [locatedError(eventStream, fieldNodes, pathToArray(path))]
|
141 | };
|
142 | }
|
143 |
|
144 |
|
145 | if (isAsyncIterable(eventStream)) {
|
146 |
|
147 | return eventStream;
|
148 | }
|
149 |
|
150 | throw new Error('Subscription field must return Async Iterable. Received: ' + inspect(eventStream));
|
151 | });
|
152 | } catch (error) {
|
153 |
|
154 |
|
155 |
|
156 | return error instanceof GraphQLError ? Promise.resolve({
|
157 | errors: [error]
|
158 | }) : Promise.reject(error);
|
159 | }
|
160 | }
|