@orion-js/graphql 3.3.21 → 3.3.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/buildSchema/getSubscriptions/index.js +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/pubsub.d.ts +3 -3
- package/lib/service/index.d.ts +1 -0
- package/lib/service/index.js +1 -0
- package/lib/service/subscription.d.ts +6 -0
- package/lib/service/subscription.js +43 -0
- package/lib/service/subscription.test.d.ts +1 -0
- package/lib/service/subscription.test.js +55 -0
- package/lib/startWebsocket.d.ts +2 -1
- package/lib/startWebsocket.js +3 -2
- package/lib/startWebsocket.test.js +2 -2
- package/lib/subscription/index.d.ts +2 -2
- package/lib/subscription/index.js +4 -4
- package/lib/types/startGraphQL.d.ts +8 -2
- package/lib/types/subscription.d.ts +8 -7
- package/package.json +6 -6
|
@@ -10,10 +10,10 @@ async function default_1(options) {
|
|
|
10
10
|
const fields = {};
|
|
11
11
|
for (const key of Object.keys(subscriptions)) {
|
|
12
12
|
const subscription = subscriptions[key];
|
|
13
|
-
subscription.
|
|
13
|
+
subscription.name = subscription.name || key;
|
|
14
14
|
const type = await (0, getType_1.default)(subscription.returns, options);
|
|
15
15
|
const args = await (0, getArgs_1.default)(subscription.params);
|
|
16
|
-
fields[
|
|
16
|
+
fields[subscription.name] = {
|
|
17
17
|
type,
|
|
18
18
|
args,
|
|
19
19
|
async subscribe(root, params, viewer) {
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/pubsub.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const setPubsub: (newPubsub:
|
|
3
|
-
export declare const getPubsub: () =>
|
|
1
|
+
import { PubSubEngine } from 'graphql-subscriptions';
|
|
2
|
+
export declare const setPubsub: (newPubsub: PubSubEngine) => void;
|
|
3
|
+
export declare const getPubsub: () => PubSubEngine;
|
package/lib/service/index.d.ts
CHANGED
package/lib/service/index.js
CHANGED
|
@@ -12,3 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./global"), exports);
|
|
14
14
|
__exportStar(require("./model"), exports);
|
|
15
|
+
__exportStar(require("./subscription"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { OrionSubscription, OrionSubscriptionOptions } from '../types/subscription';
|
|
2
|
+
export declare function Subscriptions(): ClassDecorator;
|
|
3
|
+
export declare function Subscription<T = any, ReturnType = any>(options: OrionSubscriptionOptions): (object: any, propertyName: string, index?: number) => void;
|
|
4
|
+
export declare function getServiceSubscriptions(target: any): {
|
|
5
|
+
[key: string]: OrionSubscription;
|
|
6
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getServiceSubscriptions = exports.Subscription = exports.Subscriptions = void 0;
|
|
4
|
+
const services_1 = require("@orion-js/services");
|
|
5
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
6
|
+
const __1 = require("..");
|
|
7
|
+
function Subscriptions() {
|
|
8
|
+
return function (target) {
|
|
9
|
+
(0, services_1.Service)()(target);
|
|
10
|
+
target.prototype.service = target;
|
|
11
|
+
target.prototype.serviceType = 'subscriptions';
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.Subscriptions = Subscriptions;
|
|
15
|
+
function Subscription(options) {
|
|
16
|
+
return function (object, propertyName, index) {
|
|
17
|
+
const sub = (0, __1.subscription)({
|
|
18
|
+
name: propertyName,
|
|
19
|
+
...options
|
|
20
|
+
});
|
|
21
|
+
object.subscriptions = object.subscriptions || {};
|
|
22
|
+
object.subscriptions[propertyName] = sub;
|
|
23
|
+
services_1.Container.registerHandler({
|
|
24
|
+
object,
|
|
25
|
+
propertyName,
|
|
26
|
+
index,
|
|
27
|
+
value: containerInstance => {
|
|
28
|
+
if (!object.serviceType || object.serviceType !== 'subscriptions') {
|
|
29
|
+
throw new Error('You must pass a class decorated with @Subscriptions if you want to use @Subscription');
|
|
30
|
+
}
|
|
31
|
+
return sub;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.Subscription = Subscription;
|
|
37
|
+
function getServiceSubscriptions(target) {
|
|
38
|
+
if (!target.prototype) {
|
|
39
|
+
throw new helpers_1.UserError('You must pass a class to getSubscriptions');
|
|
40
|
+
}
|
|
41
|
+
return target.prototype.subscriptions || {};
|
|
42
|
+
}
|
|
43
|
+
exports.getServiceSubscriptions = getServiceSubscriptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
const index_1 = require("./index");
|
|
14
|
+
const services_1 = require("@orion-js/services");
|
|
15
|
+
const typed_model_1 = require("@orion-js/typed-model");
|
|
16
|
+
describe('Subscriptions classes', () => {
|
|
17
|
+
it('should get the subscriptions using services', async () => {
|
|
18
|
+
let Params = class Params {
|
|
19
|
+
};
|
|
20
|
+
__decorate([
|
|
21
|
+
(0, typed_model_1.Prop)(),
|
|
22
|
+
__metadata("design:type", String)
|
|
23
|
+
], Params.prototype, "name", void 0);
|
|
24
|
+
Params = __decorate([
|
|
25
|
+
(0, typed_model_1.TypedSchema)()
|
|
26
|
+
], Params);
|
|
27
|
+
let User = class User {
|
|
28
|
+
};
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, typed_model_1.Prop)(),
|
|
31
|
+
__metadata("design:type", String)
|
|
32
|
+
], User.prototype, "name", void 0);
|
|
33
|
+
User = __decorate([
|
|
34
|
+
(0, typed_model_1.TypedSchema)()
|
|
35
|
+
], User);
|
|
36
|
+
let ExampleSubscriptionsService = class ExampleSubscriptionsService {
|
|
37
|
+
};
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, index_1.Subscription)({
|
|
40
|
+
params: Params,
|
|
41
|
+
returns: User
|
|
42
|
+
}),
|
|
43
|
+
__metadata("design:type", Object)
|
|
44
|
+
], ExampleSubscriptionsService.prototype, "onUserCreated", void 0);
|
|
45
|
+
ExampleSubscriptionsService = __decorate([
|
|
46
|
+
(0, index_1.Subscriptions)()
|
|
47
|
+
], ExampleSubscriptionsService);
|
|
48
|
+
const subscriptions = (0, index_1.getServiceSubscriptions)(ExampleSubscriptionsService);
|
|
49
|
+
expect(subscriptions.onUserCreated).toBeDefined();
|
|
50
|
+
const instance = (0, services_1.getInstance)(ExampleSubscriptionsService);
|
|
51
|
+
expect(instance.onUserCreated.publish).toBeDefined();
|
|
52
|
+
expect(instance.onUserCreated.name).toBe('onUserCreated');
|
|
53
|
+
// await instance.onUserCreated.publish({name: 'test'}, {name: 'test'})
|
|
54
|
+
});
|
|
55
|
+
});
|
package/lib/startWebsocket.d.ts
CHANGED
package/lib/startWebsocket.js
CHANGED
|
@@ -7,8 +7,9 @@ const pubsub_1 = require("./pubsub");
|
|
|
7
7
|
const http_1 = require("@orion-js/http");
|
|
8
8
|
const websockerViewer_1 = require("./websockerViewer");
|
|
9
9
|
function default_1({ schema }, options) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const pubsub = options.pubsub || new graphql_subscriptions_1.PubSub();
|
|
11
|
+
(0, pubsub_1.setPubsub)(pubsub);
|
|
12
|
+
const server = options.app || (0, http_1.getApp)();
|
|
12
13
|
if (!server) {
|
|
13
14
|
throw new Error('Error starting GraphQL WebSocket. You must start http server before starting GraphQL WebSocket');
|
|
14
15
|
}
|
|
@@ -114,8 +114,8 @@ const gqClient = async () => {
|
|
|
114
114
|
// We pass customServer instead of typical configuration of a default WebSocket server
|
|
115
115
|
const { apolloOptions, subscriptions } = await getStartServerOptions();
|
|
116
116
|
const uri = `ws://localhost:${RANDOM_WS_PORT}`;
|
|
117
|
-
const
|
|
118
|
-
(0, startWebsocket_1.default)({ schema: apolloOptions.schema }, {
|
|
117
|
+
const app = new mock_socket_with_protocol_1.Server(uri);
|
|
118
|
+
(0, startWebsocket_1.default)({ schema: apolloOptions.schema }, { app, resolvers: {} });
|
|
119
119
|
const getConnectionParams = () => {
|
|
120
120
|
return { jwt: 'hi' };
|
|
121
121
|
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
declare const createSubscription:
|
|
1
|
+
import { CreateOrionSubscriptionFunction } from '../types/subscription';
|
|
2
|
+
declare const createSubscription: CreateOrionSubscriptionFunction;
|
|
3
3
|
export default createSubscription;
|
|
@@ -8,19 +8,19 @@ const getChannelName_1 = __importDefault(require("./getChannelName"));
|
|
|
8
8
|
const resolvers_1 = require("@orion-js/resolvers");
|
|
9
9
|
const createSubscription = function (options) {
|
|
10
10
|
const subscription = {
|
|
11
|
-
|
|
11
|
+
name: options.name
|
|
12
12
|
};
|
|
13
13
|
// the publish function
|
|
14
14
|
subscription.publish = async (params, data) => {
|
|
15
15
|
const pubsub = (0, pubsub_1.getPubsub)();
|
|
16
|
-
const channelName = (0, getChannelName_1.default)(subscription.
|
|
17
|
-
await pubsub.publish(channelName, { [subscription.
|
|
16
|
+
const channelName = (0, getChannelName_1.default)(subscription.name, params);
|
|
17
|
+
await pubsub.publish(channelName, { [subscription.name]: data });
|
|
18
18
|
};
|
|
19
19
|
subscription.subscribe = async (params, viewer) => {
|
|
20
20
|
const pubsub = (0, pubsub_1.getPubsub)();
|
|
21
21
|
try {
|
|
22
22
|
await (0, resolvers_1.checkPermissions)({ params, viewer }, options);
|
|
23
|
-
const channelName = (0, getChannelName_1.default)(subscription.
|
|
23
|
+
const channelName = (0, getChannelName_1.default)(subscription.name, params);
|
|
24
24
|
return pubsub.asyncIterator(channelName);
|
|
25
25
|
}
|
|
26
26
|
catch (error) {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { GlobalResolversMap, ModelResolversMap } from '@orion-js/models';
|
|
2
2
|
import { express } from '@orion-js/http';
|
|
3
|
-
import {
|
|
3
|
+
import { OrionSubscriptionsMap } from './subscription';
|
|
4
4
|
import { GraphQLOptions } from 'apollo-server-core';
|
|
5
|
+
import { PubSubEngine } from 'graphql-subscriptions';
|
|
5
6
|
export declare type ExecuteGraphQLCache = (req: express.Request, res: express.Response, viewer: object, executeQuery: () => Promise<string>) => Promise<string>;
|
|
6
7
|
export interface ModelsResolversMap {
|
|
7
8
|
[key: string]: ModelResolversMap;
|
|
@@ -19,7 +20,7 @@ export interface StartGraphQLOptions extends Omit<GraphQLOptions, SchemaOmits> {
|
|
|
19
20
|
/**
|
|
20
21
|
* A Map with all global subscriptions
|
|
21
22
|
*/
|
|
22
|
-
subscriptions?:
|
|
23
|
+
subscriptions?: OrionSubscriptionsMap;
|
|
23
24
|
/**
|
|
24
25
|
* A function that executes the http level cache of graphql queries
|
|
25
26
|
*/
|
|
@@ -32,5 +33,10 @@ export interface StartGraphQLOptions extends Omit<GraphQLOptions, SchemaOmits> {
|
|
|
32
33
|
* Pass another express app
|
|
33
34
|
*/
|
|
34
35
|
app?: express.Application;
|
|
36
|
+
/**
|
|
37
|
+
* The pubsub provider to use. Default to the single server pubsub.
|
|
38
|
+
* If you are using multiple servers you must pass a pubsub provider like RedisPubSub
|
|
39
|
+
*/
|
|
40
|
+
pubsub?: PubSubEngine;
|
|
35
41
|
}
|
|
36
42
|
export {};
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { ResolverOptions } from '@orion-js/resolvers';
|
|
2
|
-
export interface
|
|
3
|
-
|
|
2
|
+
export interface OrionSubscription<TParams = any, ReturnType = any> {
|
|
3
|
+
name: string;
|
|
4
4
|
params: object;
|
|
5
5
|
subscribe: (callParams: object, viewer: object) => {};
|
|
6
6
|
returns: ReturnType;
|
|
7
|
-
publish: (params:
|
|
7
|
+
publish: (params: TParams, data: ReturnType) => Promise<void>;
|
|
8
8
|
}
|
|
9
|
-
export declare type
|
|
10
|
-
export interface
|
|
11
|
-
[key: string]:
|
|
9
|
+
export declare type CreateOrionSubscriptionFunction = <T = any, ReturnType = any>(options: OrionSubscriptionOptions) => OrionSubscription<T, ReturnType>;
|
|
10
|
+
export interface OrionSubscriptionsMap {
|
|
11
|
+
[key: string]: OrionSubscription;
|
|
12
12
|
}
|
|
13
|
-
export interface
|
|
13
|
+
export interface OrionSubscriptionOptions extends Omit<ResolverOptions, 'resolve'> {
|
|
14
|
+
name?: string;
|
|
14
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/graphql",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.25",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@orion-js/helpers": "^3.3.20",
|
|
17
|
-
"@orion-js/http": "^3.3.
|
|
18
|
-
"@orion-js/models": "^3.3.
|
|
19
|
-
"@orion-js/resolvers": "^3.3.
|
|
17
|
+
"@orion-js/http": "^3.3.25",
|
|
18
|
+
"@orion-js/models": "^3.3.25",
|
|
19
|
+
"@orion-js/resolvers": "^3.3.25",
|
|
20
20
|
"@orion-js/schema": "^3.3.20",
|
|
21
21
|
"@orion-js/services": "^3.3.20",
|
|
22
|
-
"@orion-js/typed-model": "^3.3.
|
|
22
|
+
"@orion-js/typed-model": "^3.3.25",
|
|
23
23
|
"apollo-server-core": "3.5.0",
|
|
24
24
|
"graphql-iso-date": "^3.6.1",
|
|
25
25
|
"graphql-subscriptions": "^2.0.0",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "ed68403cef114f593dd16535db8993560f4f1849"
|
|
49
49
|
}
|