@comity/graphql-client-ws 0.9.0
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/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/ws-transport.js +143 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/ws-transport.js +139 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/ws-transport.d.ts +32 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Filippo Bovo and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @comity/graphql-client-ws
|
|
2
|
+
|
|
3
|
+
WebSocket transport for `@comity/graphql-client`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
Provides a GraphQL transport implementation that executes operations over GraphQL WebSocket clients.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Scope
|
|
14
|
+
|
|
15
|
+
This package:
|
|
16
|
+
|
|
17
|
+
- ✅ implements the GraphQL transport contract
|
|
18
|
+
- ✅ exposes the WebSocket transport class
|
|
19
|
+
|
|
20
|
+
This package does NOT:
|
|
21
|
+
|
|
22
|
+
- ❌ define GraphQL client contracts
|
|
23
|
+
- ❌ own query building
|
|
24
|
+
- ❌ embed application-specific GraphQL policy
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Public API
|
|
29
|
+
|
|
30
|
+
- `WsGraphqlTransport`
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
No exhaustive reference; see docs for constraints.
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Documentation
|
|
37
|
+
|
|
38
|
+
- docs/overview.md
|
|
39
|
+
- docs/conventions.md
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Related Packages
|
|
44
|
+
|
|
45
|
+
- @comity/graphql-client
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Status
|
|
50
|
+
|
|
51
|
+
Stable
|
|
52
|
+
|
|
53
|
+
_Review Completed: 2026-07-25_
|
|
54
|
+
_Reviewer: Hobiri MAGI (DeepSeek v4 Pro)_
|
|
55
|
+
_Compliance Score: 99.5% (Green)_
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsGraphqlTransport = void 0;
|
|
4
|
+
var ws_transport_js_1 = require("./ws-transport.js");
|
|
5
|
+
Object.defineProperty(exports, "WsGraphqlTransport", { enumerable: true, get: function () { return ws_transport_js_1.WsGraphqlTransport; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsGraphqlTransport = void 0;
|
|
4
|
+
const errors_1 = require("@comity/graphql-client/errors");
|
|
5
|
+
const graphql_ws_1 = require("graphql-ws");
|
|
6
|
+
/**
|
|
7
|
+
* GraphQL WebSocket transport implementation.
|
|
8
|
+
*/
|
|
9
|
+
class WsGraphqlTransport {
|
|
10
|
+
/** The GraphQL WebSocket client instance. */
|
|
11
|
+
#client;
|
|
12
|
+
/**
|
|
13
|
+
* @param options - The options for configuring the WsGraphqlTransport.
|
|
14
|
+
*/
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.#client =
|
|
17
|
+
"client" in options
|
|
18
|
+
? options.client
|
|
19
|
+
: (0, graphql_ws_1.createClient)({
|
|
20
|
+
url: options.url,
|
|
21
|
+
connectionParams: options.connectionParams ?? {},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @inheritdoc
|
|
26
|
+
*/
|
|
27
|
+
async execute(request) {
|
|
28
|
+
const iterator = this.subscribe(request)[Symbol.asyncIterator]();
|
|
29
|
+
const result = await iterator.next();
|
|
30
|
+
if (result.done) {
|
|
31
|
+
throw new errors_1.GraphqlClientError("protocol_error");
|
|
32
|
+
}
|
|
33
|
+
return result.value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritdoc
|
|
37
|
+
*/
|
|
38
|
+
subscribe(request) {
|
|
39
|
+
const client = this.#client;
|
|
40
|
+
return {
|
|
41
|
+
/**
|
|
42
|
+
* Implements the async iterator protocol to allow consuming GraphQL subscription responses as an async iterable.
|
|
43
|
+
*
|
|
44
|
+
* @returns An async iterator that yields GraphQL responses for the subscription request.
|
|
45
|
+
*/
|
|
46
|
+
[Symbol.asyncIterator]() {
|
|
47
|
+
let done = false;
|
|
48
|
+
let resolve = null;
|
|
49
|
+
const queue = [];
|
|
50
|
+
const dispose = client.subscribe({
|
|
51
|
+
query: request.query,
|
|
52
|
+
variables: request.variables,
|
|
53
|
+
operationName: request.operationName,
|
|
54
|
+
}, {
|
|
55
|
+
/**
|
|
56
|
+
* Handles the next value in the GraphQL subscription.
|
|
57
|
+
*
|
|
58
|
+
* @param data The data received from the subscription.
|
|
59
|
+
*/
|
|
60
|
+
next: (data) => {
|
|
61
|
+
if (resolve) {
|
|
62
|
+
resolve({
|
|
63
|
+
value: data,
|
|
64
|
+
done: false,
|
|
65
|
+
});
|
|
66
|
+
resolve = null;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
queue.push(data);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* Handles errors that occur during the GraphQL subscription.
|
|
74
|
+
*
|
|
75
|
+
* @param cause - The error received from the subscription.
|
|
76
|
+
*/
|
|
77
|
+
error: (cause) => {
|
|
78
|
+
throw new errors_1.GraphqlClientError("transport_error", { cause });
|
|
79
|
+
},
|
|
80
|
+
/**
|
|
81
|
+
* Handles the completion of the GraphQL subscription.
|
|
82
|
+
*/
|
|
83
|
+
complete: () => {
|
|
84
|
+
done = true;
|
|
85
|
+
if (resolve) {
|
|
86
|
+
resolve({
|
|
87
|
+
value: undefined,
|
|
88
|
+
done: true,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves the next value from the async iterator.
|
|
96
|
+
*
|
|
97
|
+
* @returns A promise that resolves to the next value in the iterator.
|
|
98
|
+
*/
|
|
99
|
+
next() {
|
|
100
|
+
if (queue.length > 0) {
|
|
101
|
+
return Promise.resolve({
|
|
102
|
+
value: queue.shift(),
|
|
103
|
+
done: false,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (done) {
|
|
107
|
+
return Promise.resolve({
|
|
108
|
+
value: undefined,
|
|
109
|
+
done: true,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return new Promise((r) => {
|
|
113
|
+
resolve = r;
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
/**
|
|
117
|
+
* Handles the return of the async iterator.
|
|
118
|
+
*
|
|
119
|
+
* @returns A promise that resolves when the iterator is returned.
|
|
120
|
+
*/
|
|
121
|
+
return() {
|
|
122
|
+
dispose();
|
|
123
|
+
return Promise.resolve({
|
|
124
|
+
value: undefined,
|
|
125
|
+
done: true,
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
/**
|
|
129
|
+
* Handles errors thrown within the async iterator.
|
|
130
|
+
*
|
|
131
|
+
* @param e The error to be thrown.
|
|
132
|
+
*/
|
|
133
|
+
throw(e) {
|
|
134
|
+
dispose();
|
|
135
|
+
throw e;
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.WsGraphqlTransport = WsGraphqlTransport;
|
|
143
|
+
//# sourceMappingURL=ws-transport.js.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { GraphqlClientError } from "@comity/graphql-client/errors";
|
|
2
|
+
import { createClient } from "graphql-ws";
|
|
3
|
+
/**
|
|
4
|
+
* GraphQL WebSocket transport implementation.
|
|
5
|
+
*/
|
|
6
|
+
export class WsGraphqlTransport {
|
|
7
|
+
/** The GraphQL WebSocket client instance. */
|
|
8
|
+
#client;
|
|
9
|
+
/**
|
|
10
|
+
* @param options - The options for configuring the WsGraphqlTransport.
|
|
11
|
+
*/
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.#client =
|
|
14
|
+
"client" in options
|
|
15
|
+
? options.client
|
|
16
|
+
: createClient({
|
|
17
|
+
url: options.url,
|
|
18
|
+
connectionParams: options.connectionParams ?? {},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @inheritdoc
|
|
23
|
+
*/
|
|
24
|
+
async execute(request) {
|
|
25
|
+
const iterator = this.subscribe(request)[Symbol.asyncIterator]();
|
|
26
|
+
const result = await iterator.next();
|
|
27
|
+
if (result.done) {
|
|
28
|
+
throw new GraphqlClientError("protocol_error");
|
|
29
|
+
}
|
|
30
|
+
return result.value;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @inheritdoc
|
|
34
|
+
*/
|
|
35
|
+
subscribe(request) {
|
|
36
|
+
const client = this.#client;
|
|
37
|
+
return {
|
|
38
|
+
/**
|
|
39
|
+
* Implements the async iterator protocol to allow consuming GraphQL subscription responses as an async iterable.
|
|
40
|
+
*
|
|
41
|
+
* @returns An async iterator that yields GraphQL responses for the subscription request.
|
|
42
|
+
*/
|
|
43
|
+
[Symbol.asyncIterator]() {
|
|
44
|
+
let done = false;
|
|
45
|
+
let resolve = null;
|
|
46
|
+
const queue = [];
|
|
47
|
+
const dispose = client.subscribe({
|
|
48
|
+
query: request.query,
|
|
49
|
+
variables: request.variables,
|
|
50
|
+
operationName: request.operationName,
|
|
51
|
+
}, {
|
|
52
|
+
/**
|
|
53
|
+
* Handles the next value in the GraphQL subscription.
|
|
54
|
+
*
|
|
55
|
+
* @param data The data received from the subscription.
|
|
56
|
+
*/
|
|
57
|
+
next: (data) => {
|
|
58
|
+
if (resolve) {
|
|
59
|
+
resolve({
|
|
60
|
+
value: data,
|
|
61
|
+
done: false,
|
|
62
|
+
});
|
|
63
|
+
resolve = null;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
queue.push(data);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* Handles errors that occur during the GraphQL subscription.
|
|
71
|
+
*
|
|
72
|
+
* @param cause - The error received from the subscription.
|
|
73
|
+
*/
|
|
74
|
+
error: (cause) => {
|
|
75
|
+
throw new GraphqlClientError("transport_error", { cause });
|
|
76
|
+
},
|
|
77
|
+
/**
|
|
78
|
+
* Handles the completion of the GraphQL subscription.
|
|
79
|
+
*/
|
|
80
|
+
complete: () => {
|
|
81
|
+
done = true;
|
|
82
|
+
if (resolve) {
|
|
83
|
+
resolve({
|
|
84
|
+
value: undefined,
|
|
85
|
+
done: true,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves the next value from the async iterator.
|
|
93
|
+
*
|
|
94
|
+
* @returns A promise that resolves to the next value in the iterator.
|
|
95
|
+
*/
|
|
96
|
+
next() {
|
|
97
|
+
if (queue.length > 0) {
|
|
98
|
+
return Promise.resolve({
|
|
99
|
+
value: queue.shift(),
|
|
100
|
+
done: false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (done) {
|
|
104
|
+
return Promise.resolve({
|
|
105
|
+
value: undefined,
|
|
106
|
+
done: true,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return new Promise((r) => {
|
|
110
|
+
resolve = r;
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
/**
|
|
114
|
+
* Handles the return of the async iterator.
|
|
115
|
+
*
|
|
116
|
+
* @returns A promise that resolves when the iterator is returned.
|
|
117
|
+
*/
|
|
118
|
+
return() {
|
|
119
|
+
dispose();
|
|
120
|
+
return Promise.resolve({
|
|
121
|
+
value: undefined,
|
|
122
|
+
done: true,
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* Handles errors thrown within the async iterator.
|
|
127
|
+
*
|
|
128
|
+
* @param e The error to be thrown.
|
|
129
|
+
*/
|
|
130
|
+
throw(e) {
|
|
131
|
+
dispose();
|
|
132
|
+
throw e;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=ws-transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WsGraphqlTransport } from "./ws-transport.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { GraphqlRequest, GraphqlResponse, GraphqlTransport } from "@comity/graphql-client";
|
|
2
|
+
import type { Client } from "graphql-ws";
|
|
3
|
+
/**
|
|
4
|
+
* Options for the WsGraphqlTransport class.
|
|
5
|
+
*/
|
|
6
|
+
export type WsGraphqlTransportOptions = {
|
|
7
|
+
/** The URL of the GraphQL WebSocket server. */
|
|
8
|
+
url: string;
|
|
9
|
+
/** Optional connection parameters for the GraphQL WebSocket client. */
|
|
10
|
+
connectionParams?: Record<string, unknown>;
|
|
11
|
+
} | {
|
|
12
|
+
/** The GraphQL WebSocket client instance to use for the transport. */
|
|
13
|
+
client: Client;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* GraphQL WebSocket transport implementation.
|
|
17
|
+
*/
|
|
18
|
+
export declare class WsGraphqlTransport implements GraphqlTransport {
|
|
19
|
+
#private;
|
|
20
|
+
/**
|
|
21
|
+
* @param options - The options for configuring the WsGraphqlTransport.
|
|
22
|
+
*/
|
|
23
|
+
constructor(options: WsGraphqlTransportOptions);
|
|
24
|
+
/**
|
|
25
|
+
* @inheritdoc
|
|
26
|
+
*/
|
|
27
|
+
execute<T>(request: GraphqlRequest): Promise<GraphqlResponse<T>>;
|
|
28
|
+
/**
|
|
29
|
+
* @inheritdoc
|
|
30
|
+
*/
|
|
31
|
+
subscribe<T>(request: GraphqlRequest): AsyncIterable<GraphqlResponse<T>>;
|
|
32
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@comity/graphql-client-ws",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "GraphQL WebSocket client module for Comity framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"private": false,
|
|
7
|
+
"author": "Filippo Bovo <hello@filippobovo.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"comity": {
|
|
10
|
+
"layer": "technology-adapter",
|
|
11
|
+
"implements": "@comity/graphql-client"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/comityjs/framework#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/comityjs/framework.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/comityjs/framework/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=24.0.0"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"comity",
|
|
26
|
+
"comityjs",
|
|
27
|
+
"graphql",
|
|
28
|
+
"subscriptions",
|
|
29
|
+
"typescript",
|
|
30
|
+
"websocket"
|
|
31
|
+
],
|
|
32
|
+
"files": [
|
|
33
|
+
"./dist",
|
|
34
|
+
"!./dist/**/*.map"
|
|
35
|
+
],
|
|
36
|
+
"main": "./dist/cjs/index.js",
|
|
37
|
+
"module": "./dist/esm/index.js",
|
|
38
|
+
"types": "./dist/types/index.d.ts",
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/types/index.d.ts",
|
|
43
|
+
"default": "./dist/esm/index.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/types/index.d.ts",
|
|
47
|
+
"default": "./dist/cjs/index.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./package.json": "./package.json"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"registry": "https://registry.npmjs.org",
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"sideEffects": false,
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"graphql-ws": "^6.0.0",
|
|
59
|
+
"@comity/graphql-client": "0.9.0"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@comity/graphql-client": "0.9.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@types/node": "^24.13.4",
|
|
66
|
+
"graphql-ws": "^6.2.1",
|
|
67
|
+
"typescript": "^5.9.3"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "node ../../scripts/build.mjs",
|
|
71
|
+
"dev": "node ../../scripts/build.mjs --watch",
|
|
72
|
+
"test": "vitest run --coverage",
|
|
73
|
+
"type-check": "tsc -p tsconfig.json --noEmit",
|
|
74
|
+
"lint": "eslint --ext .ts src"
|
|
75
|
+
}
|
|
76
|
+
}
|