@trackunit/react-graphql-hooks 1.7.12 → 1.7.13
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/index.cjs.js +136 -11
- package/index.esm.js +135 -12
- package/package.json +5 -5
- package/src/index.d.ts +2 -0
- package/src/translation.d.ts +6 -1
- package/src/useLazyQuery.d.ts +45 -0
- package/src/usePaginationQuery.d.ts +1 -0
- package/src/useQuery.d.ts +47 -0
package/index.cjs.js
CHANGED
|
@@ -4,9 +4,9 @@ require('react/jsx-runtime');
|
|
|
4
4
|
var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
|
|
5
5
|
var sharedUtils = require('@trackunit/shared-utils');
|
|
6
6
|
var ApolloClient = require('@apollo/client');
|
|
7
|
+
var react = require('react');
|
|
7
8
|
var reactTablePagination = require('@trackunit/react-table-pagination');
|
|
8
9
|
var esToolkit = require('es-toolkit');
|
|
9
|
-
var react = require('react');
|
|
10
10
|
|
|
11
11
|
function _interopNamespaceDefault(e) {
|
|
12
12
|
var n = Object.create(null);
|
|
@@ -92,6 +92,59 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
|
|
|
92
92
|
.filter(sharedUtils.truthy);
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* A wrapper around Apollo Client's useLazyQuery that provides stable data by default.
|
|
97
|
+
*
|
|
98
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
99
|
+
* when new data is loading, unless stableData is set to false.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```tsx
|
|
103
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
104
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument);
|
|
105
|
+
*
|
|
106
|
+
* // Execute with polling - loading will be true during polls by default
|
|
107
|
+
* executeQuery({ variables: { first: 10 }, pollInterval: 5000 });
|
|
108
|
+
*
|
|
109
|
+
* // Disable loading indicator during polling
|
|
110
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument, {
|
|
111
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
* @template TData The type of data returned by the query
|
|
115
|
+
* @template TVariables The type of variables accepted by the query
|
|
116
|
+
* @param document The GraphQL document (query)
|
|
117
|
+
* @param options The lazy query options including stableData and showLoadingDuringPolling flags
|
|
118
|
+
* @returns {Array} A tuple with the execute function and query result with stable data behavior and optional polling loading state
|
|
119
|
+
*/
|
|
120
|
+
const useLazyQuery = (document, options = {}) => {
|
|
121
|
+
const { stableData = true, showLoadingDuringPolling = true, ...apolloOptions } = options;
|
|
122
|
+
// Enable network status notifications when we want to show loading during polling
|
|
123
|
+
const apolloOptionsWithNotify = react.useMemo(() => ({
|
|
124
|
+
...apolloOptions,
|
|
125
|
+
notifyOnNetworkStatusChange: showLoadingDuringPolling || apolloOptions.notifyOnNetworkStatusChange,
|
|
126
|
+
}), [apolloOptions, showLoadingDuringPolling]);
|
|
127
|
+
const [executeQuery, result] = ApolloClient__namespace.useLazyQuery(document, apolloOptionsWithNotify);
|
|
128
|
+
const enhancedResult = react.useMemo(() => {
|
|
129
|
+
const baseResult = stableData
|
|
130
|
+
? {
|
|
131
|
+
...result,
|
|
132
|
+
data: result.data ?? result.previousData,
|
|
133
|
+
}
|
|
134
|
+
: result;
|
|
135
|
+
// If showLoadingDuringPolling is enabled, check for polling network status
|
|
136
|
+
if (showLoadingDuringPolling) {
|
|
137
|
+
const isPolling = result.networkStatus === ApolloClient__namespace.NetworkStatus.poll;
|
|
138
|
+
return {
|
|
139
|
+
...baseResult,
|
|
140
|
+
loading: baseResult.loading || isPolling,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return baseResult;
|
|
144
|
+
}, [result, stableData, showLoadingDuringPolling]);
|
|
145
|
+
return [executeQuery, enhancedResult];
|
|
146
|
+
};
|
|
147
|
+
|
|
95
148
|
/**
|
|
96
149
|
* This hook is used to fetch data from a GraphQL query and support pagination, it will help maintain the data and the pagination.
|
|
97
150
|
*
|
|
@@ -167,7 +220,7 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
167
220
|
variables: stableVariables,
|
|
168
221
|
};
|
|
169
222
|
}, [props, stableVariables]);
|
|
170
|
-
const [, { fetchMore, networkStatus }] =
|
|
223
|
+
const [, { previousData, fetchMore, networkStatus, loading: lazyLoading }] = useLazyQuery(document, {
|
|
171
224
|
...internalProps,
|
|
172
225
|
context: {
|
|
173
226
|
...internalProps.context,
|
|
@@ -176,12 +229,16 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
176
229
|
signal: abortController.signal,
|
|
177
230
|
},
|
|
178
231
|
},
|
|
232
|
+
pollInterval: internalProps.pollInterval,
|
|
179
233
|
notifyOnNetworkStatusChange: true, // Needed to update networkStatus
|
|
180
|
-
onCompleted:
|
|
234
|
+
onCompleted: completedData => {
|
|
181
235
|
if (networkStatus === ApolloClient__namespace.NetworkStatus.refetch) {
|
|
182
236
|
// trigger reset for refetchQueries for the provided document.
|
|
183
237
|
setResetTrigger(prev => prev + 1);
|
|
184
238
|
}
|
|
239
|
+
if (internalProps.onCompleted) {
|
|
240
|
+
internalProps.onCompleted(completedData);
|
|
241
|
+
}
|
|
185
242
|
},
|
|
186
243
|
// This is safe since we have no cache
|
|
187
244
|
// and without it it will make a magic extra call to the query check this
|
|
@@ -209,11 +266,10 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
209
266
|
const fetchMoreVariables = {
|
|
210
267
|
...internalProps.variables,
|
|
211
268
|
...(internalProps.variables && "page" in internalProps.variables && "pageSize" in internalProps.variables
|
|
212
|
-
?
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
269
|
+
? {
|
|
270
|
+
pageSize: variables.first,
|
|
271
|
+
page: Number(variables.after) || 0,
|
|
272
|
+
}
|
|
217
273
|
: { ...variables }),
|
|
218
274
|
};
|
|
219
275
|
fetchMore({
|
|
@@ -304,13 +360,80 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
304
360
|
}, [after]);
|
|
305
361
|
return react.useMemo(() => {
|
|
306
362
|
return {
|
|
307
|
-
data,
|
|
308
|
-
|
|
363
|
+
data: data || previousData,
|
|
364
|
+
previousData,
|
|
365
|
+
loading: isLoading || lazyLoading,
|
|
309
366
|
pagination: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage },
|
|
310
367
|
lastFetchedData,
|
|
311
368
|
reset,
|
|
312
369
|
};
|
|
313
|
-
}, [
|
|
370
|
+
}, [
|
|
371
|
+
data,
|
|
372
|
+
isLoading,
|
|
373
|
+
lazyLoading,
|
|
374
|
+
setIsLoading,
|
|
375
|
+
setPageInfo,
|
|
376
|
+
pageInfo,
|
|
377
|
+
reset,
|
|
378
|
+
nextPage,
|
|
379
|
+
previousPage,
|
|
380
|
+
lastFetchedData,
|
|
381
|
+
previousData,
|
|
382
|
+
]);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* A wrapper around Apollo Client's useQuery that provides stable data by default.
|
|
387
|
+
*
|
|
388
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
389
|
+
* when new data is loading, unless stableData is set to false.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```tsx
|
|
393
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
394
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
395
|
+
* variables: { first: 10 },
|
|
396
|
+
* pollInterval: 5000,
|
|
397
|
+
* });
|
|
398
|
+
*
|
|
399
|
+
* // Disable loading indicator during polling
|
|
400
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
401
|
+
* variables: { first: 10 },
|
|
402
|
+
* pollInterval: 5000,
|
|
403
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
404
|
+
* });
|
|
405
|
+
* ```
|
|
406
|
+
* @template TData The type of data returned by the query
|
|
407
|
+
* @template TVariables The type of variables accepted by the query
|
|
408
|
+
* @param document The GraphQL document (query)
|
|
409
|
+
* @param options The query options including stableData and showLoadingDuringPolling flags
|
|
410
|
+
* @returns {object} The query result with stable data behavior and optional polling loading state
|
|
411
|
+
*/
|
|
412
|
+
const useQuery = (document, options = {}) => {
|
|
413
|
+
const { stableData = true, showLoadingDuringPolling = true, ...apolloOptions } = options;
|
|
414
|
+
// Enable network status notifications when we want to show loading during polling
|
|
415
|
+
const apolloOptionsWithNotify = react.useMemo(() => ({
|
|
416
|
+
...apolloOptions,
|
|
417
|
+
notifyOnNetworkStatusChange: showLoadingDuringPolling || apolloOptions.notifyOnNetworkStatusChange,
|
|
418
|
+
}), [apolloOptions, showLoadingDuringPolling]);
|
|
419
|
+
const result = ApolloClient__namespace.useQuery(document, apolloOptionsWithNotify);
|
|
420
|
+
return react.useMemo(() => {
|
|
421
|
+
const baseResult = stableData
|
|
422
|
+
? {
|
|
423
|
+
...result,
|
|
424
|
+
data: result.data ?? result.previousData,
|
|
425
|
+
}
|
|
426
|
+
: result;
|
|
427
|
+
// If showLoadingDuringPolling is enabled, check for polling network status
|
|
428
|
+
if (showLoadingDuringPolling) {
|
|
429
|
+
const isPolling = result.networkStatus === ApolloClient__namespace.NetworkStatus.poll;
|
|
430
|
+
return {
|
|
431
|
+
...baseResult,
|
|
432
|
+
loading: baseResult.loading || isPolling,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
return baseResult;
|
|
436
|
+
}, [result, stableData, showLoadingDuringPolling]);
|
|
314
437
|
};
|
|
315
438
|
|
|
316
439
|
/*
|
|
@@ -323,4 +446,6 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
323
446
|
setupLibraryTranslations();
|
|
324
447
|
|
|
325
448
|
exports.unionEdgesByNodeKey = unionEdgesByNodeKey;
|
|
449
|
+
exports.useLazyQuery = useLazyQuery;
|
|
326
450
|
exports.usePaginationQuery = usePaginationQuery;
|
|
451
|
+
exports.useQuery = useQuery;
|
package/index.esm.js
CHANGED
|
@@ -2,9 +2,9 @@ import 'react/jsx-runtime';
|
|
|
2
2
|
import { registerTranslations } from '@trackunit/i18n-library-translation';
|
|
3
3
|
import { truthy, objectKeys } from '@trackunit/shared-utils';
|
|
4
4
|
import * as ApolloClient from '@apollo/client';
|
|
5
|
+
import { useMemo, useState, useRef, useEffect, useCallback } from 'react';
|
|
5
6
|
import { useRelayPagination, defaultPageSize } from '@trackunit/react-table-pagination';
|
|
6
7
|
import { isEqual } from 'es-toolkit';
|
|
7
|
-
import { useState, useRef, useEffect, useMemo, useCallback } from 'react';
|
|
8
8
|
|
|
9
9
|
var defaultTranslations = {
|
|
10
10
|
|
|
@@ -71,6 +71,59 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
|
|
|
71
71
|
.filter(truthy);
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* A wrapper around Apollo Client's useLazyQuery that provides stable data by default.
|
|
76
|
+
*
|
|
77
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
78
|
+
* when new data is loading, unless stableData is set to false.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```tsx
|
|
82
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
83
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument);
|
|
84
|
+
*
|
|
85
|
+
* // Execute with polling - loading will be true during polls by default
|
|
86
|
+
* executeQuery({ variables: { first: 10 }, pollInterval: 5000 });
|
|
87
|
+
*
|
|
88
|
+
* // Disable loading indicator during polling
|
|
89
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument, {
|
|
90
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
91
|
+
* });
|
|
92
|
+
* ```
|
|
93
|
+
* @template TData The type of data returned by the query
|
|
94
|
+
* @template TVariables The type of variables accepted by the query
|
|
95
|
+
* @param document The GraphQL document (query)
|
|
96
|
+
* @param options The lazy query options including stableData and showLoadingDuringPolling flags
|
|
97
|
+
* @returns {Array} A tuple with the execute function and query result with stable data behavior and optional polling loading state
|
|
98
|
+
*/
|
|
99
|
+
const useLazyQuery = (document, options = {}) => {
|
|
100
|
+
const { stableData = true, showLoadingDuringPolling = true, ...apolloOptions } = options;
|
|
101
|
+
// Enable network status notifications when we want to show loading during polling
|
|
102
|
+
const apolloOptionsWithNotify = useMemo(() => ({
|
|
103
|
+
...apolloOptions,
|
|
104
|
+
notifyOnNetworkStatusChange: showLoadingDuringPolling || apolloOptions.notifyOnNetworkStatusChange,
|
|
105
|
+
}), [apolloOptions, showLoadingDuringPolling]);
|
|
106
|
+
const [executeQuery, result] = ApolloClient.useLazyQuery(document, apolloOptionsWithNotify);
|
|
107
|
+
const enhancedResult = useMemo(() => {
|
|
108
|
+
const baseResult = stableData
|
|
109
|
+
? {
|
|
110
|
+
...result,
|
|
111
|
+
data: result.data ?? result.previousData,
|
|
112
|
+
}
|
|
113
|
+
: result;
|
|
114
|
+
// If showLoadingDuringPolling is enabled, check for polling network status
|
|
115
|
+
if (showLoadingDuringPolling) {
|
|
116
|
+
const isPolling = result.networkStatus === ApolloClient.NetworkStatus.poll;
|
|
117
|
+
return {
|
|
118
|
+
...baseResult,
|
|
119
|
+
loading: baseResult.loading || isPolling,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return baseResult;
|
|
123
|
+
}, [result, stableData, showLoadingDuringPolling]);
|
|
124
|
+
return [executeQuery, enhancedResult];
|
|
125
|
+
};
|
|
126
|
+
|
|
74
127
|
/**
|
|
75
128
|
* This hook is used to fetch data from a GraphQL query and support pagination, it will help maintain the data and the pagination.
|
|
76
129
|
*
|
|
@@ -146,7 +199,7 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
146
199
|
variables: stableVariables,
|
|
147
200
|
};
|
|
148
201
|
}, [props, stableVariables]);
|
|
149
|
-
const [, { fetchMore, networkStatus }] =
|
|
202
|
+
const [, { previousData, fetchMore, networkStatus, loading: lazyLoading }] = useLazyQuery(document, {
|
|
150
203
|
...internalProps,
|
|
151
204
|
context: {
|
|
152
205
|
...internalProps.context,
|
|
@@ -155,12 +208,16 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
155
208
|
signal: abortController.signal,
|
|
156
209
|
},
|
|
157
210
|
},
|
|
211
|
+
pollInterval: internalProps.pollInterval,
|
|
158
212
|
notifyOnNetworkStatusChange: true, // Needed to update networkStatus
|
|
159
|
-
onCompleted:
|
|
213
|
+
onCompleted: completedData => {
|
|
160
214
|
if (networkStatus === ApolloClient.NetworkStatus.refetch) {
|
|
161
215
|
// trigger reset for refetchQueries for the provided document.
|
|
162
216
|
setResetTrigger(prev => prev + 1);
|
|
163
217
|
}
|
|
218
|
+
if (internalProps.onCompleted) {
|
|
219
|
+
internalProps.onCompleted(completedData);
|
|
220
|
+
}
|
|
164
221
|
},
|
|
165
222
|
// This is safe since we have no cache
|
|
166
223
|
// and without it it will make a magic extra call to the query check this
|
|
@@ -188,11 +245,10 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
188
245
|
const fetchMoreVariables = {
|
|
189
246
|
...internalProps.variables,
|
|
190
247
|
...(internalProps.variables && "page" in internalProps.variables && "pageSize" in internalProps.variables
|
|
191
|
-
?
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
248
|
+
? {
|
|
249
|
+
pageSize: variables.first,
|
|
250
|
+
page: Number(variables.after) || 0,
|
|
251
|
+
}
|
|
196
252
|
: { ...variables }),
|
|
197
253
|
};
|
|
198
254
|
fetchMore({
|
|
@@ -283,13 +339,80 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
283
339
|
}, [after]);
|
|
284
340
|
return useMemo(() => {
|
|
285
341
|
return {
|
|
286
|
-
data,
|
|
287
|
-
|
|
342
|
+
data: data || previousData,
|
|
343
|
+
previousData,
|
|
344
|
+
loading: isLoading || lazyLoading,
|
|
288
345
|
pagination: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage },
|
|
289
346
|
lastFetchedData,
|
|
290
347
|
reset,
|
|
291
348
|
};
|
|
292
|
-
}, [
|
|
349
|
+
}, [
|
|
350
|
+
data,
|
|
351
|
+
isLoading,
|
|
352
|
+
lazyLoading,
|
|
353
|
+
setIsLoading,
|
|
354
|
+
setPageInfo,
|
|
355
|
+
pageInfo,
|
|
356
|
+
reset,
|
|
357
|
+
nextPage,
|
|
358
|
+
previousPage,
|
|
359
|
+
lastFetchedData,
|
|
360
|
+
previousData,
|
|
361
|
+
]);
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* A wrapper around Apollo Client's useQuery that provides stable data by default.
|
|
366
|
+
*
|
|
367
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
368
|
+
* when new data is loading, unless stableData is set to false.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```tsx
|
|
372
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
373
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
374
|
+
* variables: { first: 10 },
|
|
375
|
+
* pollInterval: 5000,
|
|
376
|
+
* });
|
|
377
|
+
*
|
|
378
|
+
* // Disable loading indicator during polling
|
|
379
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
380
|
+
* variables: { first: 10 },
|
|
381
|
+
* pollInterval: 5000,
|
|
382
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
383
|
+
* });
|
|
384
|
+
* ```
|
|
385
|
+
* @template TData The type of data returned by the query
|
|
386
|
+
* @template TVariables The type of variables accepted by the query
|
|
387
|
+
* @param document The GraphQL document (query)
|
|
388
|
+
* @param options The query options including stableData and showLoadingDuringPolling flags
|
|
389
|
+
* @returns {object} The query result with stable data behavior and optional polling loading state
|
|
390
|
+
*/
|
|
391
|
+
const useQuery = (document, options = {}) => {
|
|
392
|
+
const { stableData = true, showLoadingDuringPolling = true, ...apolloOptions } = options;
|
|
393
|
+
// Enable network status notifications when we want to show loading during polling
|
|
394
|
+
const apolloOptionsWithNotify = useMemo(() => ({
|
|
395
|
+
...apolloOptions,
|
|
396
|
+
notifyOnNetworkStatusChange: showLoadingDuringPolling || apolloOptions.notifyOnNetworkStatusChange,
|
|
397
|
+
}), [apolloOptions, showLoadingDuringPolling]);
|
|
398
|
+
const result = ApolloClient.useQuery(document, apolloOptionsWithNotify);
|
|
399
|
+
return useMemo(() => {
|
|
400
|
+
const baseResult = stableData
|
|
401
|
+
? {
|
|
402
|
+
...result,
|
|
403
|
+
data: result.data ?? result.previousData,
|
|
404
|
+
}
|
|
405
|
+
: result;
|
|
406
|
+
// If showLoadingDuringPolling is enabled, check for polling network status
|
|
407
|
+
if (showLoadingDuringPolling) {
|
|
408
|
+
const isPolling = result.networkStatus === ApolloClient.NetworkStatus.poll;
|
|
409
|
+
return {
|
|
410
|
+
...baseResult,
|
|
411
|
+
loading: baseResult.loading || isPolling,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
return baseResult;
|
|
415
|
+
}, [result, stableData, showLoadingDuringPolling]);
|
|
293
416
|
};
|
|
294
417
|
|
|
295
418
|
/*
|
|
@@ -301,4 +424,4 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
301
424
|
*/
|
|
302
425
|
setupLibraryTranslations();
|
|
303
426
|
|
|
304
|
-
export { unionEdgesByNodeKey, usePaginationQuery };
|
|
427
|
+
export { unionEdgesByNodeKey, useLazyQuery, usePaginationQuery, useQuery };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-graphql-hooks",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.13",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@apollo/client": "3.13.8",
|
|
11
11
|
"react": "19.0.0",
|
|
12
|
-
"@trackunit/i18n-library-translation": "1.7.
|
|
13
|
-
"@trackunit/shared-utils": "1.9.
|
|
14
|
-
"@trackunit/react-table-pagination": "1.7.
|
|
15
|
-
"@trackunit/react-test-setup": "1.4.
|
|
12
|
+
"@trackunit/i18n-library-translation": "1.7.11",
|
|
13
|
+
"@trackunit/shared-utils": "1.9.8",
|
|
14
|
+
"@trackunit/react-table-pagination": "1.7.8",
|
|
15
|
+
"@trackunit/react-test-setup": "1.4.8",
|
|
16
16
|
"es-toolkit": "^1.39.10"
|
|
17
17
|
},
|
|
18
18
|
"module": "./index.esm.js",
|
package/src/index.d.ts
CHANGED
package/src/translation.d.ts
CHANGED
|
@@ -12,7 +12,9 @@ export declare const namespace = "react.graphql-hooks";
|
|
|
12
12
|
*/
|
|
13
13
|
export declare const translations: TranslationResource<TranslationKeys>;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Local useTranslation for this specific library
|
|
16
|
+
*
|
|
17
|
+
* @returns {object} The translation function for this specific library
|
|
16
18
|
*/
|
|
17
19
|
export declare const useTranslation: () => [TransForLibs<never>, import("i18next").i18n, boolean] & {
|
|
18
20
|
t: TransForLibs<never>;
|
|
@@ -25,6 +27,9 @@ export declare const useTranslation: () => [TransForLibs<never>, import("i18next
|
|
|
25
27
|
export type TranslationFunction = TransForLibs<TranslationKeys>;
|
|
26
28
|
/**
|
|
27
29
|
* Trans for this specific library.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} props The props for the Trans component
|
|
32
|
+
* @returns {object} The Trans component
|
|
28
33
|
*/
|
|
29
34
|
export declare const Trans: (props: NamespaceTransProps<TranslationKeys>) => import("react/jsx-runtime").JSX.Element;
|
|
30
35
|
/**
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as ApolloClient from "@apollo/client";
|
|
2
|
+
type TypedDocumentNode<TData, TVariables> = ApolloClient.TypedDocumentNode<TData, TVariables>;
|
|
3
|
+
export interface LazyQueryProps<TData, TVariables extends ApolloClient.OperationVariables = ApolloClient.OperationVariables> extends ApolloClient.LazyQueryHookOptions<TData, TVariables> {
|
|
4
|
+
/**
|
|
5
|
+
* A boolean value that specifies whether to return stable data (data || previousData).
|
|
6
|
+
* When true, the hook will return the current data if available, otherwise the previous data.
|
|
7
|
+
* This prevents UI flickering during refetches by maintaining the last known data.
|
|
8
|
+
* Defaults to true.
|
|
9
|
+
*/
|
|
10
|
+
stableData?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A boolean value that specifies whether the loading state should be true during polling.
|
|
13
|
+
* When true, loading will be true during poll requests (requires notifyOnNetworkStatusChange: true).
|
|
14
|
+
* This allows you to show loading indicators during background polling.
|
|
15
|
+
* Defaults to true.
|
|
16
|
+
*/
|
|
17
|
+
showLoadingDuringPolling?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A wrapper around Apollo Client's useLazyQuery that provides stable data by default.
|
|
21
|
+
*
|
|
22
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
23
|
+
* when new data is loading, unless stableData is set to false.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
28
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument);
|
|
29
|
+
*
|
|
30
|
+
* // Execute with polling - loading will be true during polls by default
|
|
31
|
+
* executeQuery({ variables: { first: 10 }, pollInterval: 5000 });
|
|
32
|
+
*
|
|
33
|
+
* // Disable loading indicator during polling
|
|
34
|
+
* const [executeQuery, { data, loading, error }] = useLazyQuery(GetAssetsDocument, {
|
|
35
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
* @template TData The type of data returned by the query
|
|
39
|
+
* @template TVariables The type of variables accepted by the query
|
|
40
|
+
* @param document The GraphQL document (query)
|
|
41
|
+
* @param options The lazy query options including stableData and showLoadingDuringPolling flags
|
|
42
|
+
* @returns {Array} A tuple with the execute function and query result with stable data behavior and optional polling loading state
|
|
43
|
+
*/
|
|
44
|
+
export declare const useLazyQuery: <TData, TVariables extends ApolloClient.OperationVariables>(document: TypedDocumentNode<TData, TVariables>, options?: LazyQueryProps<TData, TVariables>) => [ApolloClient.LazyQueryExecFunction<TData, TVariables>, ApolloClient.LazyQueryResult<TData, TVariables>];
|
|
45
|
+
export {};
|
|
@@ -8,6 +8,7 @@ export type PaginationQuery<TData> = {
|
|
|
8
8
|
loading: boolean;
|
|
9
9
|
lastFetchedData?: TData;
|
|
10
10
|
pagination: RelayTableSupport;
|
|
11
|
+
previousData: TData | undefined;
|
|
11
12
|
reset: () => void;
|
|
12
13
|
};
|
|
13
14
|
type TypedDocumentNode<TData, TVariables> = ApolloClient.TypedDocumentNode<TData, TVariables>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as ApolloClient from "@apollo/client";
|
|
2
|
+
type TypedDocumentNode<TData, TVariables> = ApolloClient.TypedDocumentNode<TData, TVariables>;
|
|
3
|
+
export interface QueryProps<TData, TVariables extends ApolloClient.OperationVariables = ApolloClient.OperationVariables> extends ApolloClient.QueryHookOptions<TData, TVariables> {
|
|
4
|
+
/**
|
|
5
|
+
* A boolean value that specifies whether to return stable data (data || previousData).
|
|
6
|
+
* When true, the hook will return the current data if available, otherwise the previous data.
|
|
7
|
+
* This prevents UI flickering during refetches by maintaining the last known data.
|
|
8
|
+
* Defaults to true.
|
|
9
|
+
*/
|
|
10
|
+
stableData?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A boolean value that specifies whether the loading state should be true during polling.
|
|
13
|
+
* When true, loading will be true during poll requests (requires notifyOnNetworkStatusChange: true).
|
|
14
|
+
* This allows you to show loading indicators during background polling.
|
|
15
|
+
* Defaults to true.
|
|
16
|
+
*/
|
|
17
|
+
showLoadingDuringPolling?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A wrapper around Apollo Client's useQuery that provides stable data by default.
|
|
21
|
+
*
|
|
22
|
+
* This hook prevents UI flickering during refetches by returning the previous data
|
|
23
|
+
* when new data is loading, unless stableData is set to false.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* // Basic usage with stable data and polling loading (both are defaults)
|
|
28
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
29
|
+
* variables: { first: 10 },
|
|
30
|
+
* pollInterval: 5000,
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Disable loading indicator during polling
|
|
34
|
+
* const { data, loading, error } = useQuery(GetAssetsDocument, {
|
|
35
|
+
* variables: { first: 10 },
|
|
36
|
+
* pollInterval: 5000,
|
|
37
|
+
* showLoadingDuringPolling: false, // loading will NOT be true during polls
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
* @template TData The type of data returned by the query
|
|
41
|
+
* @template TVariables The type of variables accepted by the query
|
|
42
|
+
* @param document The GraphQL document (query)
|
|
43
|
+
* @param options The query options including stableData and showLoadingDuringPolling flags
|
|
44
|
+
* @returns {object} The query result with stable data behavior and optional polling loading state
|
|
45
|
+
*/
|
|
46
|
+
export declare const useQuery: <TData, TVariables extends ApolloClient.OperationVariables>(document: TypedDocumentNode<TData, TVariables>, options?: QueryProps<TData, TVariables>) => ApolloClient.QueryResult<TData, TVariables>;
|
|
47
|
+
export {};
|