@dhis2/app-service-data 3.11.3 → 3.12.0-alpha.2

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.
Files changed (113) hide show
  1. package/build/cjs/__tests__/integration.test.js +6 -15
  2. package/build/cjs/__tests__/mutations.test.js +4 -11
  3. package/build/cjs/engine/DataEngine.js +4 -14
  4. package/build/cjs/engine/DataEngine.test.js +0 -2
  5. package/build/cjs/engine/helpers/getMutationFetchType.js +0 -2
  6. package/build/cjs/engine/helpers/getMutationFetchType.test.js +0 -1
  7. package/build/cjs/engine/helpers/resolveDynamicQuery.js +0 -2
  8. package/build/cjs/engine/helpers/resolveDynamicQuery.test.js +3 -4
  9. package/build/cjs/engine/helpers/validate.js +0 -21
  10. package/build/cjs/engine/helpers/validate.test.js +0 -1
  11. package/build/cjs/engine/index.js +0 -18
  12. package/build/cjs/engine/types/FetchError.js +3 -8
  13. package/build/cjs/engine/types/FetchError.test.js +0 -1
  14. package/build/cjs/engine/types/InvalidQueryError.js +3 -8
  15. package/build/cjs/index.js +0 -2
  16. package/build/cjs/links/CustomDataLink.js +3 -17
  17. package/build/cjs/links/CustomDataLink.test.js +0 -1
  18. package/build/cjs/links/ErrorLink.js +3 -7
  19. package/build/cjs/links/RestAPILink/fetchData.js +12 -22
  20. package/build/cjs/links/RestAPILink/fetchData.test.js +0 -2
  21. package/build/cjs/links/RestAPILink/metadataResources.js +9 -7
  22. package/build/cjs/links/RestAPILink/path.js +0 -3
  23. package/build/cjs/links/RestAPILink/path.test.js +0 -1
  24. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -14
  25. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -1
  26. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.js +4 -31
  27. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -1
  28. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -39
  29. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -1
  30. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -2
  31. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -1
  32. package/build/cjs/links/RestAPILink/queryToRequestOptions.js +0 -9
  33. package/build/cjs/links/RestAPILink/queryToRequestOptions.test.js +0 -1
  34. package/build/cjs/links/RestAPILink/queryToResourcePath.js +3 -24
  35. package/build/cjs/links/RestAPILink/queryToResourcePath.test.js +2 -4
  36. package/build/cjs/links/RestAPILink/validateQuery.js +4 -18
  37. package/build/cjs/links/RestAPILink/validateQuery.test.js +0 -1
  38. package/build/cjs/links/RestAPILink.js +3 -14
  39. package/build/cjs/links/RestAPILink.test.js +0 -2
  40. package/build/cjs/links/index.js +0 -6
  41. package/build/cjs/react/components/CustomDataProvider.js +3 -12
  42. package/build/cjs/react/components/DataMutation.js +1 -4
  43. package/build/cjs/react/components/DataProvider.js +11 -16
  44. package/build/cjs/react/components/DataProvider.test.js +2 -9
  45. package/build/cjs/react/components/DataQuery.js +1 -4
  46. package/build/cjs/react/context/DataContext.js +2 -8
  47. package/build/cjs/react/context/defaultContext.js +2 -6
  48. package/build/cjs/react/context/defaultContext.test.js +0 -1
  49. package/build/cjs/react/hooks/mergeAndCompareVariables.js +4 -6
  50. package/build/cjs/react/hooks/mergeAndCompareVariables.test.js +2 -3
  51. package/build/cjs/react/hooks/stableVariablesHash.js +11 -16
  52. package/build/cjs/react/hooks/stableVariablesHash.test.js +0 -1
  53. package/build/cjs/react/hooks/useDataEngine.js +0 -4
  54. package/build/cjs/react/hooks/useDataMutation.js +0 -7
  55. package/build/cjs/react/hooks/useDataMutation.test.js +42 -71
  56. package/build/cjs/react/hooks/useDataQuery.js +26 -44
  57. package/build/cjs/react/hooks/useDataQuery.test.js +318 -374
  58. package/build/cjs/react/hooks/useQueryExecutor.js +6 -14
  59. package/build/cjs/react/hooks/useQueryExecutor.test.js +42 -45
  60. package/build/cjs/react/hooks/useStaticInput.js +0 -3
  61. package/build/cjs/react/hooks/useStaticInput.test.js +8 -10
  62. package/build/cjs/react/index.js +0 -11
  63. package/build/cjs/setupRTL.js +1 -2
  64. package/build/es/__tests__/integration.test.js +4 -8
  65. package/build/es/__tests__/mutations.test.js +2 -4
  66. package/build/es/engine/DataEngine.js +3 -8
  67. package/build/es/engine/DataEngine.test.js +0 -1
  68. package/build/es/engine/helpers/resolveDynamicQuery.test.js +3 -3
  69. package/build/es/engine/helpers/validate.js +0 -13
  70. package/build/es/engine/types/FetchError.js +3 -6
  71. package/build/es/engine/types/InvalidQueryError.js +3 -6
  72. package/build/es/links/CustomDataLink.js +3 -15
  73. package/build/es/links/ErrorLink.js +3 -5
  74. package/build/es/links/RestAPILink/fetchData.js +12 -16
  75. package/build/es/links/RestAPILink/metadataResources.js +8 -3
  76. package/build/es/links/RestAPILink/path.js +0 -1
  77. package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -4
  78. package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.js +2 -19
  79. package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -19
  80. package/build/es/links/RestAPILink/queryToRequestOptions.js +0 -7
  81. package/build/es/links/RestAPILink/queryToResourcePath.js +3 -21
  82. package/build/es/links/RestAPILink/queryToResourcePath.test.js +2 -3
  83. package/build/es/links/RestAPILink/validateQuery.js +4 -16
  84. package/build/es/links/RestAPILink.js +3 -8
  85. package/build/es/react/components/CustomDataProvider.js +2 -2
  86. package/build/es/react/components/DataMutation.js +1 -1
  87. package/build/es/react/components/DataProvider.js +9 -3
  88. package/build/es/react/components/DataProvider.test.js +1 -1
  89. package/build/es/react/components/DataQuery.js +1 -1
  90. package/build/es/react/hooks/mergeAndCompareVariables.js +4 -3
  91. package/build/es/react/hooks/mergeAndCompareVariables.test.js +2 -1
  92. package/build/es/react/hooks/stableVariablesHash.js +11 -14
  93. package/build/es/react/hooks/useDataMutation.test.js +17 -39
  94. package/build/es/react/hooks/useDataQuery.js +26 -37
  95. package/build/es/react/hooks/useDataQuery.test.js +268 -318
  96. package/build/es/react/hooks/useQueryExecutor.js +6 -9
  97. package/build/es/react/hooks/useQueryExecutor.test.js +33 -34
  98. package/build/es/react/hooks/useStaticInput.test.js +6 -6
  99. package/build/es/setupRTL.js +1 -1
  100. package/build/types/engine/types/ExecuteOptions.d.ts +1 -1
  101. package/build/types/engine/types/FetchError.d.ts +2 -2
  102. package/build/types/engine/types/JsonValue.d.ts +1 -1
  103. package/build/types/engine/types/Mutation.d.ts +2 -2
  104. package/build/types/engine/types/PossiblyDynamic.d.ts +1 -1
  105. package/build/types/engine/types/Query.d.ts +3 -3
  106. package/build/types/engine/types/QueryParameters.d.ts +4 -4
  107. package/build/types/links/CustomDataLink.d.ts +2 -2
  108. package/build/types/links/RestAPILink/queryToRequestOptions/requestContentType.d.ts +1 -1
  109. package/build/types/links/RestAPILink/queryToRequestOptions.d.ts +1 -1
  110. package/build/types/react/components/DataProvider.d.ts +2 -11
  111. package/build/types/react/hooks/mergeAndCompareVariables.d.ts +1 -1
  112. package/build/types/types.d.ts +6 -6
  113. package/package.json +5 -5
@@ -1,7 +1,8 @@
1
1
  /* eslint-disable react/no-unused-prop-types */
2
+
2
3
  import { useConfig } from '@dhis2/app-service-config';
4
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
3
5
  import React from 'react';
4
- import { QueryClient, QueryClientProvider } from 'react-query';
5
6
  import { DataEngine } from '../../engine';
6
7
  import { RestAPILink } from '../../links';
7
8
  import { DataContext } from '../context/DataContext';
@@ -17,13 +18,18 @@ export const queryClientOptions = {
17
18
  // Don't refetch when the window regains focus
18
19
  refetchOnWindowFocus: false,
19
20
  // Don't refetch after connection issues
20
- refetchOnReconnect: false
21
+ refetchOnReconnect: false,
22
+ // RQv4 uses 'online' as the default, which pauses queries without network connection.
23
+ // 'always' reestablishes behavior from v3, and lets requests fire when offline
24
+ // https://tanstack.com/query/latest/docs/framework/react/guides/network-mode
25
+ networkMode: 'always'
21
26
  }
22
27
  }
23
28
  };
24
29
  const queryClient = new QueryClient(queryClientOptions);
25
30
  export const DataProvider = props => {
26
- const config = { ...useConfig(),
31
+ const config = {
32
+ ...useConfig(),
27
33
  ...props
28
34
  };
29
35
  const link = new RestAPILink(config);
@@ -7,7 +7,7 @@ import { DataProvider } from './DataProvider';
7
7
  describe('DataProvider', () => {
8
8
  it('Should pass a new engine and RestAPILink to consumers', () => {
9
9
  const renderFunction = jest.fn();
10
- render( /*#__PURE__*/React.createElement(DataProvider, {
10
+ render(/*#__PURE__*/React.createElement(DataProvider, {
11
11
  baseUrl: "test",
12
12
  apiVersion: 42
13
13
  }, /*#__PURE__*/React.createElement(DataContext.Consumer, null, renderFunction)));
@@ -1,5 +1,5 @@
1
1
  import { useDataQuery } from '../hooks/useDataQuery';
2
- export const DataQuery = (_ref) => {
2
+ export const DataQuery = _ref => {
3
3
  let {
4
4
  query,
5
5
  onComplete,
@@ -6,11 +6,12 @@ export const mergeAndCompareVariables = (previousVariables, newVariables, previo
6
6
  mergedVariablesHash: previousHash,
7
7
  mergedVariables: previousVariables
8
8
  };
9
- } // Use cached hash if it exists
10
-
9
+ }
11
10
 
11
+ // Use cached hash if it exists
12
12
  const currentHash = previousHash || stableVariablesHash(previousVariables);
13
- const mergedVariables = { ...previousVariables,
13
+ const mergedVariables = {
14
+ ...previousVariables,
14
15
  ...newVariables
15
16
  };
16
17
  const mergedVariablesHash = stableVariablesHash(mergedVariables);
@@ -41,7 +41,8 @@ describe('mergeAndCompareVariables', () => {
41
41
  const newVariables = {
42
42
  answer: 43
43
43
  };
44
- const expectedMergedVariables = { ...testVariables,
44
+ const expectedMergedVariables = {
45
+ ...testVariables,
45
46
  ...newVariables
46
47
  };
47
48
  expect(mergeAndCompareVariables(testVariables, newVariables, testHash)).toMatchObject({
@@ -1,42 +1,40 @@
1
1
  function hasObjectPrototype(o) {
2
2
  return Object.prototype.toString.call(o) === '[object Object]';
3
- } // eslint-disable-next-line @typescript-eslint/ban-types
4
-
3
+ }
5
4
 
5
+ // eslint-disable-next-line @typescript-eslint/ban-types
6
6
  export function isPlainObject(o) {
7
7
  if (!hasObjectPrototype(o)) {
8
8
  return false;
9
- } // If has modified constructor
10
-
9
+ }
11
10
 
11
+ // If has modified constructor
12
12
  const ctor = o.constructor;
13
-
14
13
  if (typeof ctor === 'undefined') {
15
14
  return true;
16
- } // If has modified prototype
17
-
15
+ }
18
16
 
17
+ // If has modified prototype
19
18
  const prot = ctor.prototype;
20
-
21
19
  if (!hasObjectPrototype(prot)) {
22
20
  return false;
23
- } // If constructor does not have an Object-specific method
24
-
21
+ }
25
22
 
23
+ // If constructor does not have an Object-specific method
26
24
  if (!Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf')) {
27
25
  return false;
28
- } // Most likely a plain Object
29
-
26
+ }
30
27
 
28
+ // Most likely a plain Object
31
29
  return true;
32
30
  }
31
+
33
32
  /**
34
33
  * Hashes the value into a stable hash.
35
34
  */
36
35
 
37
36
  export function stableVariablesHash(value) {
38
37
  let hash;
39
-
40
38
  try {
41
39
  hash = JSON.stringify(value, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
42
40
  result[key] = val[key];
@@ -45,6 +43,5 @@ export function stableVariablesHash(value) {
45
43
  } catch (e) {
46
44
  throw new Error('Could not serialize variables. Make sure that the variables do not contain circular references and can be processed by JSON.stringify.');
47
45
  }
48
-
49
46
  return hash;
50
47
  }
@@ -1,4 +1,4 @@
1
- import { renderHook, act } from '@testing-library/react-hooks';
1
+ import { renderHook, act, waitFor } from '@testing-library/react';
2
2
  import * as React from 'react';
3
3
  import { CustomDataProvider } from '../components/CustomDataProvider';
4
4
  import { useDataEngine } from './useDataEngine';
@@ -15,8 +15,7 @@ describe('useDataMutation', () => {
15
15
  const data = {
16
16
  answer: 42
17
17
  };
18
-
19
- const wrapper = (_ref) => {
18
+ const wrapper = _ref => {
20
19
  let {
21
20
  children
22
21
  } = _ref;
@@ -24,10 +23,8 @@ describe('useDataMutation', () => {
24
23
  data: data
25
24
  }, children);
26
25
  };
27
-
28
26
  const {
29
- result,
30
- waitFor
27
+ result
31
28
  } = renderHook(() => useDataMutation(mutation), {
32
29
  wrapper
33
30
  });
@@ -66,8 +63,7 @@ describe('useDataMutation', () => {
66
63
  const data = {
67
64
  answer: 42
68
65
  };
69
-
70
- const wrapper = (_ref2) => {
66
+ const wrapper = _ref2 => {
71
67
  let {
72
68
  children
73
69
  } = _ref2;
@@ -75,10 +71,8 @@ describe('useDataMutation', () => {
75
71
  data: data
76
72
  }, children);
77
73
  };
78
-
79
74
  const {
80
- result,
81
- waitFor
75
+ result
82
76
  } = renderHook(() => useDataMutation(mutation, {
83
77
  lazy: false
84
78
  }), {
@@ -110,8 +104,7 @@ describe('useDataMutation', () => {
110
104
  const data = {
111
105
  answer: 42
112
106
  };
113
-
114
- const wrapper = (_ref3) => {
107
+ const wrapper = _ref3 => {
115
108
  let {
116
109
  children
117
110
  } = _ref3;
@@ -119,10 +112,8 @@ describe('useDataMutation', () => {
119
112
  data: data
120
113
  }, children);
121
114
  };
122
-
123
115
  const {
124
- result,
125
- waitFor
116
+ result
126
117
  } = renderHook(() => useDataMutation(mutation, {
127
118
  onComplete
128
119
  }), {
@@ -159,8 +150,7 @@ describe('useDataMutation', () => {
159
150
  throw error;
160
151
  }
161
152
  };
162
-
163
- const wrapper = (_ref4) => {
153
+ const wrapper = _ref4 => {
164
154
  let {
165
155
  children
166
156
  } = _ref4;
@@ -168,10 +158,8 @@ describe('useDataMutation', () => {
168
158
  data: data
169
159
  }, children);
170
160
  };
171
-
172
161
  const {
173
- result,
174
- waitFor
162
+ result
175
163
  } = renderHook(() => useDataMutation(mutation, {
176
164
  onError
177
165
  }), {
@@ -197,7 +185,7 @@ describe('useDataMutation', () => {
197
185
  const mutation = {
198
186
  type: 'update',
199
187
  resource: 'answer',
200
- id: (_ref5) => {
188
+ id: _ref5 => {
201
189
  let {
202
190
  id
203
191
  } = _ref5;
@@ -211,8 +199,7 @@ describe('useDataMutation', () => {
211
199
  const data = {
212
200
  answer: answerSpy
213
201
  };
214
-
215
- const wrapper = (_ref6) => {
202
+ const wrapper = _ref6 => {
216
203
  let {
217
204
  children
218
205
  } = _ref6;
@@ -220,10 +207,8 @@ describe('useDataMutation', () => {
220
207
  data: data
221
208
  }, children);
222
209
  };
223
-
224
210
  const {
225
- result,
226
- waitFor
211
+ result
227
212
  } = renderHook(() => useDataMutation(mutation, {
228
213
  lazy: false,
229
214
  variables: {
@@ -257,8 +242,7 @@ describe('useDataMutation', () => {
257
242
  answer: '?'
258
243
  }
259
244
  };
260
-
261
- const wrapper = (_ref7) => {
245
+ const wrapper = _ref7 => {
262
246
  let {
263
247
  children
264
248
  } = _ref7;
@@ -266,18 +250,17 @@ describe('useDataMutation', () => {
266
250
  data: {}
267
251
  }, children);
268
252
  };
269
-
270
253
  const engineHook = renderHook(() => useDataEngine(), {
271
254
  wrapper
272
255
  });
273
256
  const mutationHook = renderHook(() => useDataMutation(mutation), {
274
257
  wrapper
275
258
  });
259
+
276
260
  /**
277
261
  * Ideally we'd check referential equality here with .toBe, but since
278
262
  * both hooks run in a different context that doesn't work.
279
263
  */
280
-
281
264
  expect(mutationHook.result.current[1].engine).toStrictEqual(engineHook.result.current);
282
265
  });
283
266
  it('should return a stable mutate function', async () => {
@@ -291,8 +274,7 @@ describe('useDataMutation', () => {
291
274
  const data = {
292
275
  answer: 42
293
276
  };
294
-
295
- const wrapper = (_ref8) => {
277
+ const wrapper = _ref8 => {
296
278
  let {
297
279
  children
298
280
  } = _ref8;
@@ -300,7 +282,6 @@ describe('useDataMutation', () => {
300
282
  data: data
301
283
  }, children);
302
284
  };
303
-
304
285
  const {
305
286
  result
306
287
  } = renderHook(() => useDataMutation(mutation), {
@@ -330,8 +311,7 @@ describe('useDataMutation', () => {
330
311
  const data = {
331
312
  answer: 42
332
313
  };
333
-
334
- const wrapper = (_ref9) => {
314
+ const wrapper = _ref9 => {
335
315
  let {
336
316
  children
337
317
  } = _ref9;
@@ -339,10 +319,8 @@ describe('useDataMutation', () => {
339
319
  data: data
340
320
  }, children);
341
321
  };
342
-
343
322
  const {
344
- result,
345
- waitFor
323
+ result
346
324
  } = renderHook(() => useDataMutation(mutation), {
347
325
  wrapper
348
326
  });
@@ -1,22 +1,8 @@
1
+ import { useQuery } from '@tanstack/react-query';
1
2
  import { useState, useRef, useCallback, useDebugValue } from 'react';
2
- import { useQuery, setLogger } from 'react-query';
3
3
  import { mergeAndCompareVariables } from './mergeAndCompareVariables';
4
4
  import { useDataEngine } from './useDataEngine';
5
5
  import { useStaticInput } from './useStaticInput';
6
-
7
- const noop = () => {
8
- /**
9
- * Used to silence the default react-query logger. Eventually we
10
- * could expose the setLogger functionality and remove the call
11
- * to setLogger here.
12
- */
13
- };
14
-
15
- setLogger({
16
- log: noop,
17
- warn: noop,
18
- error: noop
19
- });
20
6
  export const useDataQuery = function (query) {
21
7
  let {
22
8
  onComplete: userOnSuccess,
@@ -35,6 +21,7 @@ export const useDataQuery = function (query) {
35
21
  enabled: !initialLazy,
36
22
  refetchCallback: undefined
37
23
  });
24
+
38
25
  /**
39
26
  * Display current query state and refetch count in React DevTools
40
27
  */
@@ -44,53 +31,50 @@ export const useDataQuery = function (query) {
44
31
  enabled: queryState.current.enabled,
45
32
  variables: queryState.current.variables
46
33
  }, debugValue => JSON.stringify(debugValue));
34
+
47
35
  /**
48
36
  * User callbacks and refetch handling
49
37
  */
50
38
 
51
39
  const onSuccess = data => {
52
40
  var _queryState$current$r, _queryState$current;
53
-
54
41
  (_queryState$current$r = (_queryState$current = queryState.current).refetchCallback) === null || _queryState$current$r === void 0 ? void 0 : _queryState$current$r.call(_queryState$current, data);
55
42
  queryState.current.refetchCallback = undefined;
56
-
57
43
  if (userOnSuccess) {
58
44
  userOnSuccess(data);
59
45
  }
60
46
  };
61
-
62
47
  const onError = error => {
63
48
  // If we'd want to reject on errors we'd call the cb with the error here
64
49
  queryState.current.refetchCallback = undefined;
65
-
66
50
  if (userOnError) {
67
51
  userOnError(error);
68
52
  }
69
53
  };
54
+
70
55
  /**
71
56
  * Setting up react-query
72
57
  */
73
58
 
74
-
75
59
  const engine = useDataEngine();
76
60
  const queryKey = [staticQuery, queryState.current.variables];
77
-
78
61
  const queryFn = () => engine.query(staticQuery, {
79
62
  variables: queryState.current.variables
80
63
  });
81
-
82
64
  const {
83
- isIdle,
84
- isFetching,
85
- isLoading,
65
+ status,
66
+ fetchStatus,
86
67
  error,
87
68
  data,
88
69
  refetch: queryRefetch
89
- } = useQuery(queryKey, queryFn, {
70
+ } = useQuery({
71
+ queryKey,
72
+ queryFn,
90
73
  enabled: queryState.current.enabled,
91
74
  onSuccess,
92
75
  onError
93
76
  });
77
+
94
78
  /**
95
79
  * Refetch allows a user to update the variables or just
96
80
  * trigger a refetch of the query with the current variables.
@@ -106,48 +90,53 @@ export const useDataQuery = function (query) {
106
90
  mergedVariables,
107
91
  mergedVariablesHash
108
92
  } = mergeAndCompareVariables(queryState.current.variables, newVariables, queryState.current.variablesHash);
93
+
109
94
  /**
110
95
  * If there are no updates that will trigger an automatic refetch
111
96
  * we'll need to call react-query's refetch directly
112
97
  */
113
-
114
98
  if (queryState.current.enabled && identical) {
115
99
  return queryRefetch({
116
- cancelRefetch: true,
117
100
  throwOnError: false
118
- }).then((_ref) => {
101
+ }).then(_ref => {
119
102
  let {
120
103
  data
121
104
  } = _ref;
122
105
  return data;
123
106
  });
124
107
  }
125
-
126
108
  queryState.current.variables = mergedVariables;
127
109
  queryState.current.variablesHash = mergedVariablesHash;
128
- queryState.current.enabled = true; // This promise does not currently reject on errors
110
+ queryState.current.enabled = true;
129
111
 
112
+ // This promise does not currently reject on errors
130
113
  const refetchPromise = new Promise(resolve => {
131
114
  queryState.current.refetchCallback = data => {
132
115
  resolve(data);
133
116
  };
134
- }); // Trigger a react-query refetch by incrementing variablesUpdateCount state
117
+ });
135
118
 
119
+ // Trigger a react-query refetch by incrementing variablesUpdateCount state
136
120
  setVariablesUpdateCount(prevCount => prevCount + 1);
137
121
  return refetchPromise;
138
122
  }, [queryRefetch]);
123
+
139
124
  /**
140
125
  * react-query returns null or an error, but we return undefined
141
126
  * or an error, so this ensures consistency with the other types.
142
127
  */
143
-
144
128
  const ourError = error || undefined;
145
129
  return {
146
130
  engine,
147
- // A query is idle if it is lazy and no initial data is available.
148
- called: !isIdle,
149
- loading: isLoading,
150
- fetching: isFetching,
131
+ // A query has not been called if it is lazy (fetchStatus = 'idle') and no initial data is available (status = 'loading').
132
+ // https://tanstack.com/query/v4/docs/framework/react/guides/queries
133
+ called: !(status === 'loading' && fetchStatus === 'idle'),
134
+ // 'loading' should only be true when actively fetching (fetchStatus = 'fetching') while there is no data yet (status = 'loading').
135
+ // If there is already data for the query, then 'loading' will not become 'true' when refetching, so the previous data can still be
136
+ // displayed while new data is fetched in the background
137
+ loading: fetchStatus === 'fetching' && status === 'loading',
138
+ // 'fetching' reflects the fetching behavior behind the scenes
139
+ fetching: fetchStatus === 'fetching',
151
140
  error: ourError,
152
141
  data,
153
142
  refetch