@graphql-box/worker-client 5.4.16 → 5.4.17

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@graphql-box/worker-client",
3
3
  "description": "The GraphQL Box web worker client module.",
4
- "version": "5.4.16",
4
+ "version": "5.4.17",
5
5
  "author": "Dylan Aubrey",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/graphql-box",
@@ -41,22 +41,22 @@
41
41
  "iterall": "^1.3.0",
42
42
  "lodash-es": "^4.17.21",
43
43
  "uuid": "^11.0.3",
44
- "@graphql-box/core": "5.4.10",
45
- "@graphql-box/helpers": "5.4.10"
44
+ "@graphql-box/core": "5.4.11",
45
+ "@graphql-box/helpers": "5.4.11"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "graphql": "<17",
49
- "@graphql-box/cache-manager": "5.4.11",
50
- "@graphql-box/client": "5.4.10",
51
- "@graphql-box/request-parser": "5.4.10"
49
+ "@graphql-box/cache-manager": "5.4.12",
50
+ "@graphql-box/client": "5.4.11",
51
+ "@graphql-box/request-parser": "5.4.11"
52
52
  },
53
53
  "devDependencies": {
54
54
  "cts-types": "^0.0.8",
55
55
  "del-cli": "^6.0.0",
56
56
  "graphql": "^16.9.0",
57
- "@graphql-box/cache-manager": "5.4.11",
58
- "@graphql-box/request-parser": "5.4.10",
59
- "@graphql-box/client": "5.4.10"
57
+ "@graphql-box/cache-manager": "5.4.12",
58
+ "@graphql-box/client": "5.4.11",
59
+ "@graphql-box/request-parser": "5.4.11"
60
60
  },
61
61
  "keywords": [
62
62
  "client",
@@ -26,7 +26,7 @@ export const logRequest = () => {
26
26
  descriptor.value = async function descriptorValue(...args: Parameters<Descriptor>): ReturnType<Descriptor> {
27
27
  return new Promise(resolve => {
28
28
  void (async () => {
29
- const { debugManager, ...otherContext } = args[2];
29
+ const { data, debugManager } = args[2];
30
30
 
31
31
  if (!debugManager) {
32
32
  resolve(await method.apply(this, args));
@@ -37,9 +37,10 @@ export const logRequest = () => {
37
37
  const startTime = debugManager.now();
38
38
 
39
39
  debugManager.log(REQUEST_EXECUTED, {
40
- context: { ...otherContext, operationName: derivedOperationName },
41
- options: args[1],
42
- request: args[0],
40
+ data: {
41
+ ...data,
42
+ ...(!data.operationName && derivedOperationName ? { operationName: derivedOperationName } : undefined),
43
+ },
43
44
  stats: { startTime },
44
45
  });
45
46
 
@@ -53,10 +54,10 @@ export const logRequest = () => {
53
54
  }
54
55
 
55
56
  debugManager.log(REQUEST_RESOLVED, {
56
- context: { ...otherContext, operationName: derivedOperationName },
57
- options: args[1],
58
- request: args[0],
59
- result,
57
+ data: {
58
+ ...data,
59
+ ...(!data.operationName && derivedOperationName ? { operationName: derivedOperationName } : undefined),
60
+ },
60
61
  stats: { duration, endTime, startTime },
61
62
  });
62
63
  })();
@@ -24,7 +24,7 @@ export const logSubscription = () => {
24
24
  descriptor.value = async function descriptorValue(...args: Parameters<Descriptor>): ReturnType<Descriptor> {
25
25
  return new Promise(resolve => {
26
26
  void (async () => {
27
- const { debugManager, ...otherContext } = args[2];
27
+ const { data, debugManager } = args[2];
28
28
 
29
29
  if (!debugManager) {
30
30
  resolve(await method.apply(this, args));
@@ -35,9 +35,10 @@ export const logSubscription = () => {
35
35
  const startTime = debugManager.now();
36
36
 
37
37
  debugManager.log(SUBSCRIPTION_EXECUTED, {
38
- context: { ...otherContext, operationName: derivedOperationName },
39
- options: args[1],
40
- request: args[0],
38
+ data: {
39
+ ...data,
40
+ ...(!data.operationName && derivedOperationName ? { operationName: derivedOperationName } : undefined),
41
+ },
41
42
  stats: { startTime },
42
43
  });
43
44
 
package/src/main.ts CHANGED
@@ -23,13 +23,12 @@ import {
23
23
  import { type RequestParserDef } from '@graphql-box/request-parser';
24
24
  import { EventEmitter } from 'eventemitter3';
25
25
  import { OperationTypeNode } from 'graphql';
26
- import { isError } from 'lodash-es';
26
+ import { isError, merge } from 'lodash-es';
27
27
  import { v4 as uuid } from 'uuid';
28
28
  import { GRAPHQL_BOX, MESSAGE, REQUEST, SUBSCRIBE } from './constants.ts';
29
29
  import { logRequest } from './debug/logRequest.ts';
30
30
  import { logSubscription } from './debug/logSubscription.ts';
31
31
  import {
32
- type MessageContext,
33
32
  type MessageRequestPayload,
34
33
  type MessageResponsePayload,
35
34
  type PendingResolver,
@@ -38,21 +37,12 @@ import {
38
37
  } from './types.ts';
39
38
 
40
39
  export class WorkerClient {
41
- private static _getMessageContext({
42
- hasDeferOrStream = false,
43
- initiator,
44
- operation,
45
- requestID,
46
- }: RequestContext): MessageContext {
47
- return { hasDeferOrStream, initiator, operation, requestID };
48
- }
49
-
50
40
  private static _resolve(
51
41
  { cacheMetadata, ...rest }: PartialResponseData,
52
42
  options: RequestOptions,
53
- { requestID }: RequestContext,
43
+ { data }: RequestContext,
54
44
  ): PartialRequestResult {
55
- const result: PartialRequestResult = { ...rest, requestID };
45
+ const result: PartialRequestResult = { ...rest, requestID: data.requestID };
56
46
 
57
47
  if (options.returnCacheMetadata && cacheMetadata) {
58
48
  result._cacheMetadata = cacheMetadata;
@@ -72,8 +62,10 @@ export class WorkerClient {
72
62
  return;
73
63
  }
74
64
 
65
+ const { operation, requestID } = context.data;
66
+ const { hasDeferOrStream } = context.deprecated;
75
67
  const { _cacheMetadata, ...otherProps } = result;
76
- const response: PartialRequestResult = { ...deserializeErrors(otherProps), requestID: context.requestID };
68
+ const response: PartialRequestResult = { ...deserializeErrors(otherProps), requestID };
77
69
 
78
70
  if (_cacheMetadata) {
79
71
  response._cacheMetadata = rehydrateCacheMetadata(_cacheMetadata);
@@ -81,41 +73,38 @@ export class WorkerClient {
81
73
 
82
74
  if (method === SUBSCRIBE) {
83
75
  this._debugManager?.log(SUBSCRIPTION_RESOLVED, {
84
- context,
85
- result: response,
76
+ data: context.data,
86
77
  stats: { endTime: this._debugManager.now() },
87
78
  });
88
79
 
89
- this._eventEmitter.emit(context.requestID, response);
90
- } else if (context.hasDeferOrStream) {
91
- const pending = this._pending.get(context.requestID);
80
+ this._eventEmitter.emit(requestID, response);
81
+ } else if (hasDeferOrStream) {
82
+ const pending = this._pending.get(requestID);
92
83
 
93
84
  if (pending) {
94
- const eventAsyncIterator = new EventAsyncIterator<PartialRequestResult>(this._eventEmitter, context.requestID);
85
+ const eventAsyncIterator = new EventAsyncIterator<PartialRequestResult>(this._eventEmitter, requestID);
95
86
  pending.resolve(eventAsyncIterator.getIterator());
96
87
  }
97
88
 
98
89
  this._debugManager?.log(REQUEST_RESOLVED, {
99
- context,
100
- result: response,
90
+ data: context.data,
101
91
  stats: { endTime: this._debugManager.now() },
102
92
  });
103
93
 
104
- this._eventEmitter.emit(context.requestID, response);
94
+ this._eventEmitter.emit(requestID, response);
105
95
  } else {
106
- const pending = this._pending.get(context.requestID);
96
+ const pending = this._pending.get(requestID);
107
97
 
108
98
  if (!pending) {
109
99
  return;
110
100
  }
111
101
 
112
102
  this._debugManager?.log(REQUEST_RESOLVED, {
113
- context,
114
- result: response,
103
+ data: context.data,
115
104
  stats: { endTime: this._debugManager.now() },
116
105
  });
117
106
 
118
- if (context.operation === OperationTypeNode.QUERY && pending.requestData && pending.options && pending.context) {
107
+ if (operation === OperationTypeNode.QUERY && pending.requestData && pending.options && pending.context) {
119
108
  void this._cacheManager.cacheQuery(
120
109
  pending.requestData,
121
110
  undefined,
@@ -136,11 +125,11 @@ export class WorkerClient {
136
125
  * it is for communicating with the worker cache that the worker
137
126
  * client is using within the worker.
138
127
  */
139
- private _cache: CoreWorker;
140
- private _cacheManager: CacheManagerDef;
141
- private _debugManager: DebugManagerDef | null;
142
- private _eventEmitter: EventEmitter;
143
- private _experimentalDeferStreamSupport: boolean;
128
+ private readonly _cache: CoreWorker;
129
+ private readonly _cacheManager: CacheManagerDef;
130
+ private readonly _debugManager: DebugManagerDef | undefined;
131
+ private readonly _eventEmitter: EventEmitter;
132
+ private readonly _experimentalDeferStreamSupport: boolean;
144
133
  private _messageQueue: MessageRequestPayload[] = [];
145
134
  private _pending: PendingTracker = new Map();
146
135
  private _requestParser: RequestParserDef;
@@ -175,7 +164,7 @@ export class WorkerClient {
175
164
 
176
165
  this._cache = options.cache;
177
166
  this._cacheManager = options.cacheManager;
178
- this._debugManager = options.debugManager ?? null;
167
+ this._debugManager = options.debugManager;
179
168
  this._eventEmitter = new EventEmitter();
180
169
  this._experimentalDeferStreamSupport = options.experimentalDeferStreamSupport ?? false;
181
170
  this._requestParser = options.requestParser;
@@ -205,19 +194,27 @@ export class WorkerClient {
205
194
  }
206
195
 
207
196
  public async mutate(request: string, options: RequestOptions = {}, context: PartialRequestContext = {}) {
208
- return this._request(request, options, this._getRequestContext(OperationTypeNode.MUTATION, request, context));
197
+ return this._request(
198
+ request,
199
+ options,
200
+ this._getRequestContext(OperationTypeNode.MUTATION, request, options, context),
201
+ );
209
202
  }
210
203
 
211
204
  public async query(request: string, options: RequestOptions = {}, context: PartialRequestContext = {}) {
212
- return this._query(request, options, this._getRequestContext(OperationTypeNode.QUERY, request, context));
205
+ return this._query(request, options, this._getRequestContext(OperationTypeNode.QUERY, request, options, context));
213
206
  }
214
207
 
215
208
  public async request(request: string, options: RequestOptions = {}, context: PartialRequestContext = {}) {
216
- return this._request(request, options, this._getRequestContext(OperationTypeNode.QUERY, request, context));
209
+ return this._request(request, options, this._getRequestContext(OperationTypeNode.QUERY, request, options, context));
217
210
  }
218
211
 
219
- public async subscribe(request: string, options: RequestOptions = {}) {
220
- return this._subscribe(request, options, this._getRequestContext(OperationTypeNode.SUBSCRIPTION, request));
212
+ public async subscribe(request: string, options: RequestOptions = {}, context: PartialRequestContext = {}) {
213
+ return this._subscribe(
214
+ request,
215
+ options,
216
+ this._getRequestContext(OperationTypeNode.SUBSCRIPTION, request, options, context),
217
+ );
221
218
  }
222
219
 
223
220
  public set worker(worker: Worker) {
@@ -237,24 +234,34 @@ export class WorkerClient {
237
234
  private _getRequestContext(
238
235
  operation: OperationTypeNode,
239
236
  request: string,
240
- context: PartialRequestContext = {},
237
+ options: RequestOptions,
238
+ context: PartialRequestContext,
241
239
  ): RequestContext {
242
- return {
243
- debugManager: this._debugManager,
244
- experimentalDeferStreamSupport: this._experimentalDeferStreamSupport,
245
- fieldTypeMap: new Map(),
246
- filteredRequest: '',
247
- operation,
248
- operationName: '',
249
- originalRequestHash: hashRequest(request),
250
- parsedRequest: '',
251
- queryFiltered: false,
252
- request,
253
- requestComplexity: null,
254
- requestDepth: null,
255
- requestID: uuid(),
256
- ...context,
257
- };
240
+ return merge(
241
+ {
242
+ data: {
243
+ batched: options.batch,
244
+ operation,
245
+ operationName: '',
246
+ originalRequestHash: hashRequest(request),
247
+ queryFiltered: false,
248
+ requestComplexity: undefined,
249
+ requestDepth: undefined,
250
+ requestID: uuid(),
251
+ tag: options.tag,
252
+ variables: options.variables,
253
+ },
254
+ debugManager: this._debugManager,
255
+ deprecated: {
256
+ experimentalDeferStreamSupport: this._experimentalDeferStreamSupport,
257
+ },
258
+ fieldTypeMap: new Map(),
259
+ filteredRequest: '',
260
+ parsedRequest: '',
261
+ request,
262
+ },
263
+ context,
264
+ );
258
265
  }
259
266
 
260
267
  private async _query(
@@ -274,7 +281,10 @@ export class WorkerClient {
274
281
  return await new Promise((resolve: PendingResolver) => {
275
282
  if (this._worker) {
276
283
  this._worker.postMessage({
277
- context: WorkerClient._getMessageContext(context),
284
+ context: {
285
+ data: context.data,
286
+ deprecated: context.deprecated,
287
+ },
278
288
  method: REQUEST,
279
289
  options,
280
290
  request,
@@ -282,7 +292,10 @@ export class WorkerClient {
282
292
  });
283
293
  } else {
284
294
  this._messageQueue.push({
285
- context: WorkerClient._getMessageContext(context),
295
+ context: {
296
+ data: context.data,
297
+ deprecated: context.deprecated,
298
+ },
286
299
  method: REQUEST,
287
300
  options,
288
301
  request,
@@ -290,14 +303,14 @@ export class WorkerClient {
290
303
  });
291
304
  }
292
305
 
293
- this._pending.set(context.requestID, { context, options, requestData, resolve });
306
+ this._pending.set(context.data.requestID, { context, options, requestData, resolve });
294
307
  });
295
308
  } catch (error) {
296
309
  const confirmedError = isError(error)
297
310
  ? error
298
311
  : new Error('@graphql-box/worker-client request had an unexpected error.');
299
312
 
300
- return { errors: [confirmedError], requestID: context.requestID };
313
+ return { errors: [confirmedError], requestID: context.data.requestID };
301
314
  }
302
315
  }
303
316
 
@@ -318,13 +331,16 @@ export class WorkerClient {
318
331
  private async _request(
319
332
  request: string,
320
333
  options: RequestOptions,
321
- context: RequestContext,
334
+ { data, deprecated }: RequestContext,
322
335
  ): Promise<PartialRequestResult | AsyncIterableIterator<PartialRequestResult | undefined>> {
323
336
  try {
324
337
  return await new Promise((resolve: PendingResolver) => {
325
338
  if (this._worker) {
326
339
  this._worker.postMessage({
327
- context: WorkerClient._getMessageContext(context),
340
+ context: {
341
+ data,
342
+ deprecated,
343
+ },
328
344
  method: REQUEST,
329
345
  options,
330
346
  request,
@@ -332,7 +348,10 @@ export class WorkerClient {
332
348
  });
333
349
  } else {
334
350
  this._messageQueue.push({
335
- context: WorkerClient._getMessageContext(context),
351
+ context: {
352
+ data,
353
+ deprecated,
354
+ },
336
355
  method: REQUEST,
337
356
  options,
338
357
  request,
@@ -340,14 +359,14 @@ export class WorkerClient {
340
359
  });
341
360
  }
342
361
 
343
- this._pending.set(context.requestID, { resolve });
362
+ this._pending.set(data.requestID, { resolve });
344
363
  });
345
364
  } catch (error) {
346
365
  const confirmedError = isError(error)
347
366
  ? error
348
367
  : new Error('@graphql-box/worker-client request had an unexpected error.');
349
368
 
350
- return { errors: [confirmedError], requestID: context.requestID };
369
+ return { errors: [confirmedError], requestID: data.requestID };
351
370
  }
352
371
  }
353
372
 
@@ -355,12 +374,15 @@ export class WorkerClient {
355
374
  private _subscribe(
356
375
  request: string,
357
376
  options: RequestOptions,
358
- context: RequestContext,
377
+ { data, deprecated }: RequestContext,
359
378
  ): Promise<PartialRequestResult | AsyncIterableIterator<PartialRequestResult | undefined>> {
360
379
  try {
361
380
  if (this._worker) {
362
381
  this._worker.postMessage({
363
- context: WorkerClient._getMessageContext(context),
382
+ context: {
383
+ data,
384
+ deprecated,
385
+ },
364
386
  method: SUBSCRIBE,
365
387
  options,
366
388
  request,
@@ -368,7 +390,10 @@ export class WorkerClient {
368
390
  });
369
391
  } else {
370
392
  this._messageQueue.push({
371
- context: WorkerClient._getMessageContext(context),
393
+ context: {
394
+ data,
395
+ deprecated,
396
+ },
372
397
  method: SUBSCRIBE,
373
398
  options,
374
399
  request,
@@ -376,14 +401,15 @@ export class WorkerClient {
376
401
  });
377
402
  }
378
403
 
379
- const eventAsyncIterator = new EventAsyncIterator<PartialRequestResult>(this._eventEmitter, context.requestID);
404
+ const eventAsyncIterator = new EventAsyncIterator<PartialRequestResult>(this._eventEmitter, data.requestID);
405
+
380
406
  return Promise.resolve(eventAsyncIterator.getIterator());
381
407
  } catch (error) {
382
408
  const confirmedError = isError(error)
383
409
  ? error
384
410
  : new Error('@graphql-box/worker-client subscribe had an unexpected error.');
385
411
 
386
- return Promise.resolve({ errors: [confirmedError], requestID: context.requestID });
412
+ return Promise.resolve({ errors: [confirmedError], requestID: data.requestID });
387
413
  }
388
414
  }
389
415
  }
package/src/types.ts CHANGED
@@ -6,11 +6,12 @@ import {
6
6
  type PartialRawFetchData,
7
7
  type PartialRequestResult,
8
8
  type RequestContext,
9
+ type RequestContextData,
10
+ type RequestContextDeprecated,
9
11
  type RequestData,
10
12
  type RequestOptions,
11
13
  } from '@graphql-box/core';
12
14
  import { type RequestParserDef } from '@graphql-box/request-parser';
13
- import { type OperationTypeNode } from 'graphql';
14
15
 
15
16
  export interface UserOptions {
16
17
  /**
@@ -76,10 +77,8 @@ export interface MessageResponsePayload {
76
77
  }
77
78
 
78
79
  export interface MessageContext {
79
- hasDeferOrStream: boolean;
80
- initiator?: string;
81
- operation: OperationTypeNode;
82
- requestID: string;
80
+ data: RequestContextData;
81
+ deprecated: RequestContextDeprecated;
83
82
  }
84
83
 
85
84
  export interface RegisterWorkerOptions {