@constructive-io/graphql-codegen 4.39.0 → 4.39.1

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.
@@ -41,6 +41,7 @@ export type {
41
41
  SubscriptionFieldMeta,
42
42
  SubscriptionOperation,
43
43
  Unsubscribe,
44
+ WsClient,
44
45
  } from './realtime';
45
46
  export { RealtimeManager } from './realtime';
46
47
 
@@ -9,9 +9,38 @@
9
9
  * Any changes here will affect all generated ORM clients.
10
10
  */
11
11
 
12
- // graphql-ws is loaded lazily so that importing this module does not
13
- // throw when the package is absent (e.g. CLI-only consumers).
14
- type WsClient = import('graphql-ws').Client;
12
+ // Minimal type shims so this module compiles without graphql-ws
13
+ // installed. Consumers supply a WsClient via RealtimeConfig;
14
+ // the SDK itself never imports or requires graphql-ws.
15
+
16
+ interface WsGraphQLError {
17
+ readonly message: string;
18
+ readonly [key: string]: unknown;
19
+ }
20
+
21
+ interface WsExecutionResult<TData = Record<string, unknown>> {
22
+ data?: TData | null;
23
+ errors?: readonly WsGraphQLError[];
24
+ extensions?: Record<string, unknown>;
25
+ }
26
+
27
+ interface WsSink<T> {
28
+ next(value: T): void;
29
+ error(error: unknown): void;
30
+ complete(): void;
31
+ }
32
+
33
+ /**
34
+ * Minimal interface matching the graphql-ws Client.
35
+ * Consumers pass a concrete instance via RealtimeConfig.client.
36
+ */
37
+ export interface WsClient {
38
+ subscribe<TData = Record<string, unknown>>(
39
+ payload: { query: string; variables?: Record<string, unknown> },
40
+ sink: WsSink<WsExecutionResult<TData>>,
41
+ ): () => void;
42
+ dispose(): void;
43
+ }
15
44
 
16
45
  // ============================================================================
17
46
  // Types
@@ -85,40 +114,32 @@ export interface SubscriptionFieldMeta {
85
114
  /**
86
115
  * Configuration for the realtime (WebSocket) connection.
87
116
  * Pass this as the `realtime` option in OrmClientConfig.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * import { createClient } from 'graphql-ws';
121
+ *
122
+ * const client = createOrmClient({
123
+ * endpoint: 'https://api.example.com/graphql',
124
+ * realtime: {
125
+ * client: createClient({ url: 'wss://api.example.com/graphql' }),
126
+ * },
127
+ * });
128
+ * ```
88
129
  */
89
130
  export interface RealtimeConfig {
90
- /** WebSocket endpoint URL (e.g., 'wss://api.example.com/graphql') */
91
- url: string;
92
- /**
93
- * Returns the current auth token. Called on connection init and
94
- * on reconnection so the client always sends a fresh token.
95
- */
96
- getToken?: () => string | Promise<string>;
97
131
  /**
98
- * Additional connection parameters sent during WebSocket handshake.
99
- * Merged with the authorization header from getToken().
132
+ * A graphql-ws Client instance (or any object satisfying WsClient).
133
+ * The consumer creates this themselves, giving full control over
134
+ * connection options, auth, and transport.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * import { createClient } from 'graphql-ws';
139
+ * const wsClient = createClient({ url: 'wss://...' });
140
+ * ```
100
141
  */
101
- connectionParams?: Record<string, unknown>;
102
- /**
103
- * Whether to connect lazily (on first subscribe) or eagerly.
104
- * @default true
105
- */
106
- lazy?: boolean;
107
- /**
108
- * Maximum number of reconnection attempts before giving up.
109
- * @default 5
110
- */
111
- retryAttempts?: number;
112
- /**
113
- * Delay between reconnection attempts in milliseconds,
114
- * or a function for custom backoff.
115
- * @default 1000
116
- */
117
- retryWait?: number | ((retryCount: number) => number | Promise<number>);
118
- /** Called when the WebSocket connection is established */
119
- onConnected?: () => void;
120
- /** Called when the WebSocket connection is closed */
121
- onDisconnected?: (reason?: unknown) => void;
142
+ client: WsClient;
122
143
  }
123
144
 
124
145
  // ============================================================================
@@ -126,8 +147,8 @@ export interface RealtimeConfig {
126
147
  // ============================================================================
127
148
 
128
149
  /**
129
- * Manages a single graphql-ws WebSocket connection and multiplexes
130
- * subscriptions over it. Created lazily by OrmClient when `realtime`
150
+ * Manages a graphql-ws WebSocket client and multiplexes
151
+ * subscriptions over it. Created by OrmClient when `realtime`
131
152
  * config is provided.
132
153
  */
133
154
  export class RealtimeManager {
@@ -137,56 +158,7 @@ export class RealtimeManager {
137
158
  private activeSubscriptions = 0;
138
159
 
139
160
  constructor(config: RealtimeConfig) {
140
- // eslint-disable-next-line @typescript-eslint/no-var-requires
141
- const { createClient: createWsClient } = require('graphql-ws') as typeof import('graphql-ws');
142
-
143
- const retryWait = async (retryCount: number): Promise<void> => {
144
- if (typeof config.retryWait === 'function') {
145
- const result = config.retryWait(retryCount);
146
- const ms = typeof result === 'number' ? result : await result;
147
- await new Promise<void>((resolve) => setTimeout(resolve, ms));
148
- } else {
149
- const base =
150
- typeof config.retryWait === 'number' ? config.retryWait : 1000;
151
- await new Promise<void>((resolve) =>
152
- setTimeout(resolve, base * Math.pow(2, retryCount)),
153
- );
154
- }
155
- };
156
-
157
- this.wsClient = createWsClient({
158
- url: config.url,
159
- lazy: config.lazy ?? true,
160
- retryAttempts: config.retryAttempts ?? 5,
161
- retryWait,
162
- connectionParams: async () => {
163
- const params: Record<string, unknown> = {
164
- ...config.connectionParams,
165
- };
166
- if (config.getToken) {
167
- const token = await config.getToken();
168
- params['authorization'] = `Bearer ${token}`;
169
- }
170
- return params;
171
- },
172
- on: {
173
- connecting: () => {
174
- const newState =
175
- this.connectionState === 'disconnected'
176
- ? 'connecting'
177
- : 'reconnecting';
178
- this.setConnectionState(newState);
179
- },
180
- connected: () => {
181
- this.setConnectionState('connected');
182
- config.onConnected?.();
183
- },
184
- closed: (event) => {
185
- this.setConnectionState('disconnected');
186
- config.onDisconnected?.(event);
187
- },
188
- },
189
- });
161
+ this.wsClient = config.client;
190
162
  }
191
163
 
192
164
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructive-io/graphql-codegen",
3
- "version": "4.39.0",
3
+ "version": "4.39.1",
4
4
  "description": "GraphQL SDK generator for Constructive databases with React Query hooks",
5
5
  "keywords": [
6
6
  "graphql",
@@ -100,5 +100,5 @@
100
100
  "tsx": "^4.21.0",
101
101
  "typescript": "^5.9.3"
102
102
  },
103
- "gitHead": "90016935b53d6fb84e0b83879377f0c2eb9abce6"
103
+ "gitHead": "c87dafec1ccb2c3cc62f2304584104db108a810b"
104
104
  }