1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var graphql_1 = require("graphql");
|
14 | var values_1 = require("graphql/execution/values");
|
15 | var validation_1 = require("./validation");
|
16 | var ValidationError = (function (_super) {
|
17 | __extends(ValidationError, _super);
|
18 | function ValidationError(errors) {
|
19 | var _this = _super.call(this) || this;
|
20 | _this.errors = errors;
|
21 | _this.message = 'Subscription query has validation errors';
|
22 | return _this;
|
23 | }
|
24 | return ValidationError;
|
25 | }(Error));
|
26 | exports.ValidationError = ValidationError;
|
27 | var SubscriptionManager = (function () {
|
28 | function SubscriptionManager(options) {
|
29 | this.pubsub = options.pubsub;
|
30 | this.schema = options.schema;
|
31 | this.setupFunctions = options.setupFunctions || {};
|
32 | this.subscriptions = {};
|
33 | this.maxSubscriptionId = 0;
|
34 | }
|
35 | SubscriptionManager.prototype.publish = function (triggerName, payload) {
|
36 | this.pubsub.publish(triggerName, payload);
|
37 | };
|
38 | SubscriptionManager.prototype.subscribe = function (options) {
|
39 | var _this = this;
|
40 | var parsedQuery = graphql_1.parse(options.query);
|
41 | var errors = graphql_1.validate(this.schema, parsedQuery, graphql_1.specifiedRules.concat([validation_1.subscriptionHasSingleRootField]));
|
42 | if (errors.length) {
|
43 | return Promise.reject(new ValidationError(errors));
|
44 | }
|
45 | var args = {};
|
46 | var subscriptionName = '';
|
47 | parsedQuery.definitions.forEach(function (definition) {
|
48 | if (definition.kind === 'OperationDefinition') {
|
49 | var rootField = definition.selectionSet.selections[0];
|
50 | subscriptionName = rootField.name.value;
|
51 | var fields = _this.schema.getSubscriptionType().getFields();
|
52 | args = values_1.getArgumentValues(fields[subscriptionName], rootField, options.variables);
|
53 | }
|
54 | });
|
55 | var triggerMap;
|
56 | if (this.setupFunctions[subscriptionName]) {
|
57 | triggerMap = this.setupFunctions[subscriptionName](options, args, subscriptionName);
|
58 | }
|
59 | else {
|
60 | triggerMap = (_a = {}, _a[subscriptionName] = {}, _a);
|
61 | }
|
62 | var externalSubscriptionId = this.maxSubscriptionId++;
|
63 | this.subscriptions[externalSubscriptionId] = [];
|
64 | var subscriptionPromises = [];
|
65 | Object.keys(triggerMap).forEach(function (triggerName) {
|
66 | var _a = triggerMap[triggerName], _b = _a.channelOptions, channelOptions = _b === void 0 ? {} : _b, _c = _a.filter, filter = _c === void 0 ? function () { return true; } : _c;
|
67 | var onMessage = function (rootValue) {
|
68 | return Promise.resolve().then(function () {
|
69 | if (typeof options.context === 'function') {
|
70 | return options.context();
|
71 | }
|
72 | return options.context;
|
73 | }).then(function (context) {
|
74 | return Promise.all([
|
75 | context,
|
76 | filter(rootValue, context),
|
77 | ]);
|
78 | }).then(function (_a) {
|
79 | var context = _a[0], doExecute = _a[1];
|
80 | if (!doExecute) {
|
81 | return;
|
82 | }
|
83 | graphql_1.execute(_this.schema, parsedQuery, rootValue, context, options.variables, options.operationName).then(function (data) { return options.callback(null, data); });
|
84 | }).catch(function (error) {
|
85 | options.callback(error);
|
86 | });
|
87 | };
|
88 | subscriptionPromises.push(_this.pubsub.subscribe(triggerName, onMessage, channelOptions)
|
89 | .then(function (id) { return _this.subscriptions[externalSubscriptionId].push(id); }));
|
90 | });
|
91 | return Promise.all(subscriptionPromises).then(function () { return externalSubscriptionId; });
|
92 | var _a;
|
93 | };
|
94 | SubscriptionManager.prototype.unsubscribe = function (subId) {
|
95 | var _this = this;
|
96 | this.subscriptions[subId].forEach(function (internalId) {
|
97 | _this.pubsub.unsubscribe(internalId);
|
98 | });
|
99 | delete this.subscriptions[subId];
|
100 | };
|
101 | return SubscriptionManager;
|
102 | }());
|
103 | exports.SubscriptionManager = SubscriptionManager;
|
104 |
|
\ | No newline at end of file |