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