@graphql-tools/executor-graphql-ws 0.0.6 → 0.0.7
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/cjs/index.js +46 -13
- package/esm/index.js +46 -13
- package/package.json +1 -1
- package/typings/index.d.cts +6 -3
- package/typings/index.d.ts +6 -3
package/cjs/index.js
CHANGED
|
@@ -2,20 +2,54 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildGraphQLWSExecutor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
6
|
const repeater_1 = require("@repeaterjs/repeater");
|
|
6
7
|
const graphql_1 = require("graphql");
|
|
7
8
|
const graphql_ws_1 = require("graphql-ws");
|
|
8
9
|
const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
function isClient(client) {
|
|
11
|
+
return 'subscribe' in client;
|
|
12
|
+
}
|
|
13
|
+
function buildGraphQLWSExecutor(clientOptionsOrClient) {
|
|
14
|
+
let graphqlWSClient;
|
|
15
|
+
if (isClient(clientOptionsOrClient)) {
|
|
16
|
+
graphqlWSClient = clientOptionsOrClient;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
graphqlWSClient = (0, graphql_ws_1.createClient)({
|
|
20
|
+
webSocketImpl: isomorphic_ws_1.default,
|
|
21
|
+
lazy: true,
|
|
22
|
+
...clientOptionsOrClient,
|
|
23
|
+
});
|
|
24
|
+
if (clientOptionsOrClient.onClient) {
|
|
25
|
+
clientOptionsOrClient.onClient(graphqlWSClient);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return function GraphQLWSExecutor(executionRequest) {
|
|
29
|
+
const { document, variables, operationName, extensions, operationType = (0, utils_1.getOperationASTFromRequest)(executionRequest).operation, } = executionRequest;
|
|
17
30
|
const query = (0, graphql_1.print)(document);
|
|
18
|
-
|
|
31
|
+
if (operationType === 'subscription') {
|
|
32
|
+
return new repeater_1.Repeater(function repeaterExecutor(push, stop) {
|
|
33
|
+
const unsubscribe = graphqlWSClient.subscribe({
|
|
34
|
+
query,
|
|
35
|
+
variables,
|
|
36
|
+
operationName,
|
|
37
|
+
extensions,
|
|
38
|
+
}, {
|
|
39
|
+
next(data) {
|
|
40
|
+
return push(data);
|
|
41
|
+
},
|
|
42
|
+
error(error) {
|
|
43
|
+
return stop(error);
|
|
44
|
+
},
|
|
45
|
+
complete() {
|
|
46
|
+
return stop();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return stop.finally(unsubscribe);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
19
53
|
const unsubscribe = graphqlWSClient.subscribe({
|
|
20
54
|
query,
|
|
21
55
|
variables,
|
|
@@ -23,16 +57,15 @@ function buildGraphQLWSExecutor(url, webSocketImpl = isomorphic_ws_1.default, co
|
|
|
23
57
|
extensions,
|
|
24
58
|
}, {
|
|
25
59
|
next(data) {
|
|
26
|
-
return
|
|
60
|
+
return resolve(data);
|
|
27
61
|
},
|
|
28
62
|
error(error) {
|
|
29
|
-
return
|
|
63
|
+
return reject(error);
|
|
30
64
|
},
|
|
31
65
|
complete() {
|
|
32
|
-
|
|
66
|
+
unsubscribe();
|
|
33
67
|
},
|
|
34
68
|
});
|
|
35
|
-
return stop.finally(unsubscribe);
|
|
36
69
|
});
|
|
37
70
|
};
|
|
38
71
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,17 +1,51 @@
|
|
|
1
|
+
import { getOperationASTFromRequest } from '@graphql-tools/utils';
|
|
1
2
|
import { Repeater } from '@repeaterjs/repeater';
|
|
2
3
|
import { print } from 'graphql';
|
|
3
4
|
import { createClient } from 'graphql-ws';
|
|
4
5
|
import WebSocket from 'isomorphic-ws';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
function isClient(client) {
|
|
7
|
+
return 'subscribe' in client;
|
|
8
|
+
}
|
|
9
|
+
export function buildGraphQLWSExecutor(clientOptionsOrClient) {
|
|
10
|
+
let graphqlWSClient;
|
|
11
|
+
if (isClient(clientOptionsOrClient)) {
|
|
12
|
+
graphqlWSClient = clientOptionsOrClient;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
graphqlWSClient = createClient({
|
|
16
|
+
webSocketImpl: WebSocket,
|
|
17
|
+
lazy: true,
|
|
18
|
+
...clientOptionsOrClient,
|
|
19
|
+
});
|
|
20
|
+
if (clientOptionsOrClient.onClient) {
|
|
21
|
+
clientOptionsOrClient.onClient(graphqlWSClient);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return function GraphQLWSExecutor(executionRequest) {
|
|
25
|
+
const { document, variables, operationName, extensions, operationType = getOperationASTFromRequest(executionRequest).operation, } = executionRequest;
|
|
13
26
|
const query = print(document);
|
|
14
|
-
|
|
27
|
+
if (operationType === 'subscription') {
|
|
28
|
+
return new Repeater(function repeaterExecutor(push, stop) {
|
|
29
|
+
const unsubscribe = graphqlWSClient.subscribe({
|
|
30
|
+
query,
|
|
31
|
+
variables,
|
|
32
|
+
operationName,
|
|
33
|
+
extensions,
|
|
34
|
+
}, {
|
|
35
|
+
next(data) {
|
|
36
|
+
return push(data);
|
|
37
|
+
},
|
|
38
|
+
error(error) {
|
|
39
|
+
return stop(error);
|
|
40
|
+
},
|
|
41
|
+
complete() {
|
|
42
|
+
return stop();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return stop.finally(unsubscribe);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
15
49
|
const unsubscribe = graphqlWSClient.subscribe({
|
|
16
50
|
query,
|
|
17
51
|
variables,
|
|
@@ -19,16 +53,15 @@ export function buildGraphQLWSExecutor(url, webSocketImpl = WebSocket, connectio
|
|
|
19
53
|
extensions,
|
|
20
54
|
}, {
|
|
21
55
|
next(data) {
|
|
22
|
-
return
|
|
56
|
+
return resolve(data);
|
|
23
57
|
},
|
|
24
58
|
error(error) {
|
|
25
|
-
return
|
|
59
|
+
return reject(error);
|
|
26
60
|
},
|
|
27
61
|
complete() {
|
|
28
|
-
|
|
62
|
+
unsubscribe();
|
|
29
63
|
},
|
|
30
64
|
});
|
|
31
|
-
return stop.finally(unsubscribe);
|
|
32
65
|
});
|
|
33
66
|
};
|
|
34
67
|
}
|
package/package.json
CHANGED
package/typings/index.d.cts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
1
|
import { Executor } from '@graphql-tools/utils';
|
|
3
|
-
import
|
|
4
|
-
|
|
2
|
+
import { Client, ClientOptions } from 'graphql-ws';
|
|
3
|
+
interface GraphQLWSExecutorOptions extends ClientOptions {
|
|
4
|
+
onClient?: (client: Client) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorOptions | Client): Executor;
|
|
7
|
+
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
1
|
import { Executor } from '@graphql-tools/utils';
|
|
3
|
-
import
|
|
4
|
-
|
|
2
|
+
import { Client, ClientOptions } from 'graphql-ws';
|
|
3
|
+
interface GraphQLWSExecutorOptions extends ClientOptions {
|
|
4
|
+
onClient?: (client: Client) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorOptions | Client): Executor;
|
|
7
|
+
export {};
|