@dumbql/subscriptions 0.0.2-rc.3 → 0.0.3
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/README.md +1 -1
- package/package.json +2 -2
- package/src/lib/graphql-live-query.ts +168 -0
- package/src/public-api.ts +1 -0
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dumbql/subscriptions",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "GraphQL subscriptions over WebSocket (graphql-transport-ws) — framework-agnostic core + Angular service",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"sideEffects": false,
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@angular/core": "^22.0.0",
|
|
24
|
-
"@dumbql/core": "^0.0.
|
|
24
|
+
"@dumbql/core": "^0.0.3",
|
|
25
25
|
"rxjs": "~7.8.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependenciesMeta": {
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
|
|
3
|
+
interface SubscriptionCallbacks<T> {
|
|
4
|
+
next: (data: T) => void;
|
|
5
|
+
error: (err: Error) => void;
|
|
6
|
+
complete: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface WsMessage {
|
|
10
|
+
type: string;
|
|
11
|
+
id?: string;
|
|
12
|
+
payload?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface GraphQLResponse<T> {
|
|
16
|
+
data?: T;
|
|
17
|
+
errors?: { message: string }[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const WS_PROTOCOL = 'graphql-transport-ws';
|
|
21
|
+
const CONNECTION_TIMEOUT = 10000;
|
|
22
|
+
|
|
23
|
+
function defaultWsUrl(endpoint: string): string {
|
|
24
|
+
return endpoint.replace(/^http/, 'ws');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class GraphqlLiveQuery {
|
|
28
|
+
constructor(private readonly endpoint: string) {}
|
|
29
|
+
|
|
30
|
+
async execute<T>(
|
|
31
|
+
query: string,
|
|
32
|
+
variables?: Record<string, unknown>,
|
|
33
|
+
callbacks?: Partial<SubscriptionCallbacks<T>>,
|
|
34
|
+
): Promise<() => void> {
|
|
35
|
+
const emit = {
|
|
36
|
+
next: callbacks?.next ?? (() => {}),
|
|
37
|
+
error: callbacks?.error ?? (() => {}),
|
|
38
|
+
complete: callbacks?.complete ?? (() => {}),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch(this.endpoint, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: { 'Content-Type': 'application/json' },
|
|
45
|
+
body: JSON.stringify({ query, variables }),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
emit.error(new Error(`HTTP ${response.status}`));
|
|
50
|
+
return () => {};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const json = await response.json() as GraphQLResponse<T>;
|
|
54
|
+
|
|
55
|
+
if (json.errors && json.errors.length > 0) {
|
|
56
|
+
emit.error(new Error(json.errors[0].message));
|
|
57
|
+
return () => {};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (json.data !== undefined) {
|
|
61
|
+
emit.next(json.data);
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
emit.error(err instanceof Error ? err : new Error('Live query fetch failed'));
|
|
65
|
+
return () => {};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this.subscribeToUpdates<T>(query, variables, emit);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private subscribeToUpdates<T>(
|
|
72
|
+
query: string,
|
|
73
|
+
variables?: Record<string, unknown>,
|
|
74
|
+
emit?: SubscriptionCallbacks<T>,
|
|
75
|
+
): () => void {
|
|
76
|
+
const url = defaultWsUrl(this.endpoint);
|
|
77
|
+
let ws: WebSocket;
|
|
78
|
+
let completed = false;
|
|
79
|
+
const subId =
|
|
80
|
+
typeof crypto !== 'undefined' && crypto.randomUUID
|
|
81
|
+
? crypto.randomUUID()
|
|
82
|
+
: 'sub_' + Math.random().toString(36).substring(2, 9);
|
|
83
|
+
|
|
84
|
+
const { next, error, complete } = emit ?? {
|
|
85
|
+
next: () => {},
|
|
86
|
+
error: () => {},
|
|
87
|
+
complete: () => {},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
ws = new WebSocket(url, WS_PROTOCOL);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
error(err instanceof Error ? err : new Error('WebSocket creation failed'));
|
|
94
|
+
return () => {};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const connTimeout = setTimeout(() => {
|
|
98
|
+
if (!completed) {
|
|
99
|
+
ws.close();
|
|
100
|
+
error(new Error('WebSocket connection timeout'));
|
|
101
|
+
}
|
|
102
|
+
}, CONNECTION_TIMEOUT);
|
|
103
|
+
|
|
104
|
+
ws.onopen = () => {
|
|
105
|
+
ws.send(JSON.stringify({ type: 'connection_init' }));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
ws.onmessage = (event: MessageEvent) => {
|
|
109
|
+
let msg: WsMessage;
|
|
110
|
+
try {
|
|
111
|
+
msg = JSON.parse(event.data) as WsMessage;
|
|
112
|
+
} catch {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
switch (msg.type) {
|
|
117
|
+
case 'connection_ack': {
|
|
118
|
+
clearTimeout(connTimeout);
|
|
119
|
+
ws.send(
|
|
120
|
+
JSON.stringify({
|
|
121
|
+
type: 'subscribe',
|
|
122
|
+
id: subId,
|
|
123
|
+
payload: { query, variables },
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'next': {
|
|
129
|
+
const payload = msg.payload as { data?: T; errors?: { message: string }[] };
|
|
130
|
+
if (payload.errors && payload.errors.length > 0) {
|
|
131
|
+
error(new Error(payload.errors[0].message));
|
|
132
|
+
} else if (payload.data !== undefined) {
|
|
133
|
+
next(payload.data);
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'error': {
|
|
138
|
+
const payload = msg.payload as { message?: string };
|
|
139
|
+
error(new Error(payload?.message ?? 'Subscription error'));
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case 'complete': {
|
|
143
|
+
complete();
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
ws.onerror = () => {
|
|
150
|
+
error(new Error('WebSocket connection failed'));
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
ws.onclose = () => {
|
|
154
|
+
if (!completed) {
|
|
155
|
+
complete();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return () => {
|
|
160
|
+
completed = true;
|
|
161
|
+
clearTimeout(connTimeout);
|
|
162
|
+
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
|
|
163
|
+
ws.send(JSON.stringify({ type: 'complete', id: subId }));
|
|
164
|
+
ws.close(1000);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/public-api.ts
CHANGED