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