@trackunit/react-graphql-hooks 0.0.163 → 0.0.165

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 CHANGED
@@ -7,8 +7,11 @@ var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
7
7
  var sharedUtils = require('@trackunit/shared-utils');
8
8
  var ApolloClient = require('@apollo/client');
9
9
  var reactTablePagination = require('@trackunit/react-table-pagination');
10
+ var isEqual = require('lodash/isEqual');
10
11
  var react = require('react');
11
12
 
13
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
+
12
15
  function _interopNamespace(e) {
13
16
  if (e && e.__esModule) return e;
14
17
  var n = Object.create(null);
@@ -28,6 +31,7 @@ function _interopNamespace(e) {
28
31
  }
29
32
 
30
33
  var ApolloClient__namespace = /*#__PURE__*/_interopNamespace(ApolloClient);
34
+ var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
31
35
 
32
36
  var defaultTranslations = {
33
37
 
@@ -94,6 +98,38 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
94
98
  .filter(sharedUtils.truthy);
95
99
  };
96
100
 
101
+ /******************************************************************************
102
+ Copyright (c) Microsoft Corporation.
103
+
104
+ Permission to use, copy, modify, and/or distribute this software for any
105
+ purpose with or without fee is hereby granted.
106
+
107
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
108
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
109
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
110
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
111
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
112
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
113
+ PERFORMANCE OF THIS SOFTWARE.
114
+ ***************************************************************************** */
115
+
116
+ function __rest(s, e) {
117
+ var t = {};
118
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
119
+ t[p] = s[p];
120
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
121
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
122
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
123
+ t[p[i]] = s[p[i]];
124
+ }
125
+ return t;
126
+ }
127
+
128
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
129
+ var e = new Error(message);
130
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
131
+ };
132
+
97
133
  /**
98
134
  * This hook is used to fetch data from a GraphQL query and support pagination, it will help maintain the data and the pagination.
99
135
  *
@@ -135,10 +171,22 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
135
171
  * @param {PaginationQueryProps<TData, TVariables>} props - The properties for configuring the query. This includes the `updateQuery` function for merging new and existing data, and options for pagination such as `pageSize`. Also includes other lazy query hook options from Apollo Client.
136
172
  * @returns {*} {PaginationQuery}
137
173
  */
138
- const usePaginationQuery = (document, props) => {
174
+ const usePaginationQuery = (document, _a) => {
175
+ var props = __rest(_a, []);
139
176
  const [lastFetchedData, setLastFetchedData] = react.useState();
140
177
  const [resetTrigger, setResetTrigger] = react.useState(0);
141
- const [, { fetchMore, networkStatus }] = ApolloClient__namespace.useLazyQuery(document, Object.assign(Object.assign({}, props), { notifyOnNetworkStatusChange: true, onCompleted: () => {
178
+ // Makes **sure** query variables are stable.
179
+ // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
180
+ // Maybe better solution exists but this is the best I could come up with without changing to much.
181
+ // If ever a better solution is found, please remove this ugliness.
182
+ const stableVariables = react.useRef(props.variables);
183
+ const internalProps = react.useMemo(() => {
184
+ if (!isEqual__default["default"](props.variables, stableVariables.current)) {
185
+ stableVariables.current = props.variables;
186
+ }
187
+ return Object.assign(Object.assign({}, props), { variables: stableVariables.current });
188
+ }, [props]);
189
+ const [, { fetchMore, networkStatus }] = ApolloClient__namespace.useLazyQuery(document, Object.assign(Object.assign({}, internalProps), { notifyOnNetworkStatusChange: true, onCompleted: () => {
142
190
  if (networkStatus === ApolloClient__namespace.NetworkStatus.refetch) {
143
191
  // trigger reset for refetchQueries for the provided document.
144
192
  setResetTrigger(prev => prev + 1);
@@ -153,7 +201,7 @@ const usePaginationQuery = (document, props) => {
153
201
  setResetTrigger(prev => prev + 1);
154
202
  }, []);
155
203
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = reactTablePagination.useRelayPagination({
156
- pageSize: props.pageSize || 50,
204
+ pageSize: internalProps.pageSize || 50,
157
205
  onReset,
158
206
  });
159
207
  const doFetchMore = react.useCallback((variables, prev) => {
@@ -162,9 +210,12 @@ const usePaginationQuery = (document, props) => {
162
210
  /**
163
211
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
164
212
  */
165
- let fetchMoreVariables = Object.assign(Object.assign({}, props.variables), (props.variables && "page" in props.variables && "pageSize" in props.variables
213
+ let fetchMoreVariables = Object.assign(Object.assign({}, internalProps.variables), (internalProps.variables && "page" in internalProps.variables && "pageSize" in internalProps.variables
166
214
  ? // eslint-disable-next-line local-rules/no-typescript-assertion
167
- { pageSize: variables.first, page: Number(variables.after) || 0 }
215
+ {
216
+ pageSize: variables.first,
217
+ page: Number(variables.after) || 0,
218
+ }
168
219
  : Object.assign({}, variables)));
169
220
  // To support "pageable" queries we do it here! REMOVE once the serviceplans are updated to use the new pagination.
170
221
  fetchMoreVariables = Object.assign({}, ("pageable" in fetchMoreVariables
@@ -190,7 +241,7 @@ const usePaginationQuery = (document, props) => {
190
241
  return undefined;
191
242
  }
192
243
  setLastFetchedData(fetchMoreResult);
193
- const result = props.updateQuery(prev, fetchMoreResult);
244
+ const result = internalProps.updateQuery(prev, fetchMoreResult);
194
245
  setPageInfo(result.pageInfo || null);
195
246
  setData(result.data);
196
247
  setIsLoading(false);
@@ -198,18 +249,18 @@ const usePaginationQuery = (document, props) => {
198
249
  },
199
250
  });
200
251
  return abortController;
201
- }, [setIsLoading, props, fetchMore, setPageInfo]);
252
+ }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
202
253
  react.useEffect(() => {
203
254
  setData(undefined);
204
255
  reset();
205
- }, [props.variables, reset]);
256
+ }, [internalProps.variables, reset]);
206
257
  react.useEffect(() => {
207
258
  const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
208
259
  return () => {
209
260
  abortController.abort();
210
261
  };
211
262
  // eslint-disable-next-line react-hooks/exhaustive-deps
212
- }, [props.variables, resetTrigger]);
263
+ }, [internalProps.variables, resetTrigger]);
213
264
  react.useEffect(() => {
214
265
  if (after) {
215
266
  doFetchMore({ first, after }, data);
package/index.esm.js CHANGED
@@ -3,7 +3,8 @@ 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
5
  import { useRelayPagination } from '@trackunit/react-table-pagination';
6
- import { useState, useCallback, useEffect, useMemo } from 'react';
6
+ import isEqual from 'lodash/isEqual';
7
+ import { useState, useRef, useMemo, useCallback, useEffect } from 'react';
7
8
 
8
9
  var defaultTranslations = {
9
10
 
@@ -70,6 +71,38 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
70
71
  .filter(truthy);
71
72
  };
72
73
 
74
+ /******************************************************************************
75
+ Copyright (c) Microsoft Corporation.
76
+
77
+ Permission to use, copy, modify, and/or distribute this software for any
78
+ purpose with or without fee is hereby granted.
79
+
80
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
81
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
82
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
83
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
84
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
85
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
86
+ PERFORMANCE OF THIS SOFTWARE.
87
+ ***************************************************************************** */
88
+
89
+ function __rest(s, e) {
90
+ var t = {};
91
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
92
+ t[p] = s[p];
93
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
94
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
95
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
96
+ t[p[i]] = s[p[i]];
97
+ }
98
+ return t;
99
+ }
100
+
101
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
102
+ var e = new Error(message);
103
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
104
+ };
105
+
73
106
  /**
74
107
  * This hook is used to fetch data from a GraphQL query and support pagination, it will help maintain the data and the pagination.
75
108
  *
@@ -111,10 +144,22 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
111
144
  * @param {PaginationQueryProps<TData, TVariables>} props - The properties for configuring the query. This includes the `updateQuery` function for merging new and existing data, and options for pagination such as `pageSize`. Also includes other lazy query hook options from Apollo Client.
112
145
  * @returns {*} {PaginationQuery}
113
146
  */
114
- const usePaginationQuery = (document, props) => {
147
+ const usePaginationQuery = (document, _a) => {
148
+ var props = __rest(_a, []);
115
149
  const [lastFetchedData, setLastFetchedData] = useState();
116
150
  const [resetTrigger, setResetTrigger] = useState(0);
117
- const [, { fetchMore, networkStatus }] = ApolloClient.useLazyQuery(document, Object.assign(Object.assign({}, props), { notifyOnNetworkStatusChange: true, onCompleted: () => {
151
+ // Makes **sure** query variables are stable.
152
+ // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
153
+ // Maybe better solution exists but this is the best I could come up with without changing to much.
154
+ // If ever a better solution is found, please remove this ugliness.
155
+ const stableVariables = useRef(props.variables);
156
+ const internalProps = useMemo(() => {
157
+ if (!isEqual(props.variables, stableVariables.current)) {
158
+ stableVariables.current = props.variables;
159
+ }
160
+ return Object.assign(Object.assign({}, props), { variables: stableVariables.current });
161
+ }, [props]);
162
+ const [, { fetchMore, networkStatus }] = ApolloClient.useLazyQuery(document, Object.assign(Object.assign({}, internalProps), { notifyOnNetworkStatusChange: true, onCompleted: () => {
118
163
  if (networkStatus === ApolloClient.NetworkStatus.refetch) {
119
164
  // trigger reset for refetchQueries for the provided document.
120
165
  setResetTrigger(prev => prev + 1);
@@ -129,7 +174,7 @@ const usePaginationQuery = (document, props) => {
129
174
  setResetTrigger(prev => prev + 1);
130
175
  }, []);
131
176
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = useRelayPagination({
132
- pageSize: props.pageSize || 50,
177
+ pageSize: internalProps.pageSize || 50,
133
178
  onReset,
134
179
  });
135
180
  const doFetchMore = useCallback((variables, prev) => {
@@ -138,9 +183,12 @@ const usePaginationQuery = (document, props) => {
138
183
  /**
139
184
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
140
185
  */
141
- let fetchMoreVariables = Object.assign(Object.assign({}, props.variables), (props.variables && "page" in props.variables && "pageSize" in props.variables
186
+ let fetchMoreVariables = Object.assign(Object.assign({}, internalProps.variables), (internalProps.variables && "page" in internalProps.variables && "pageSize" in internalProps.variables
142
187
  ? // eslint-disable-next-line local-rules/no-typescript-assertion
143
- { pageSize: variables.first, page: Number(variables.after) || 0 }
188
+ {
189
+ pageSize: variables.first,
190
+ page: Number(variables.after) || 0,
191
+ }
144
192
  : Object.assign({}, variables)));
145
193
  // To support "pageable" queries we do it here! REMOVE once the serviceplans are updated to use the new pagination.
146
194
  fetchMoreVariables = Object.assign({}, ("pageable" in fetchMoreVariables
@@ -166,7 +214,7 @@ const usePaginationQuery = (document, props) => {
166
214
  return undefined;
167
215
  }
168
216
  setLastFetchedData(fetchMoreResult);
169
- const result = props.updateQuery(prev, fetchMoreResult);
217
+ const result = internalProps.updateQuery(prev, fetchMoreResult);
170
218
  setPageInfo(result.pageInfo || null);
171
219
  setData(result.data);
172
220
  setIsLoading(false);
@@ -174,18 +222,18 @@ const usePaginationQuery = (document, props) => {
174
222
  },
175
223
  });
176
224
  return abortController;
177
- }, [setIsLoading, props, fetchMore, setPageInfo]);
225
+ }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
178
226
  useEffect(() => {
179
227
  setData(undefined);
180
228
  reset();
181
- }, [props.variables, reset]);
229
+ }, [internalProps.variables, reset]);
182
230
  useEffect(() => {
183
231
  const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
184
232
  return () => {
185
233
  abortController.abort();
186
234
  };
187
235
  // eslint-disable-next-line react-hooks/exhaustive-deps
188
- }, [props.variables, resetTrigger]);
236
+ }, [internalProps.variables, resetTrigger]);
189
237
  useEffect(() => {
190
238
  if (after) {
191
239
  doFetchMore({ first, after }, data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-graphql-hooks",
3
- "version": "0.0.163",
3
+ "version": "0.0.165",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -11,7 +11,8 @@
11
11
  "react": "^18.2.0",
12
12
  "@trackunit/i18n-library-translation": "*",
13
13
  "@trackunit/shared-utils": "*",
14
- "@trackunit/react-table-pagination": "*"
14
+ "@trackunit/react-table-pagination": "*",
15
+ "lodash": "4.17.21"
15
16
  },
16
17
  "module": "./index.esm.js",
17
18
  "main": "./index.cjs.js"
@@ -69,4 +69,4 @@ export interface PaginationQueryProps<TData, TVariables extends ApolloClient.Ope
69
69
  * @param {PaginationQueryProps<TData, TVariables>} props - The properties for configuring the query. This includes the `updateQuery` function for merging new and existing data, and options for pagination such as `pageSize`. Also includes other lazy query hook options from Apollo Client.
70
70
  * @returns {*} {PaginationQuery}
71
71
  */
72
- export declare const usePaginationQuery: <TData, TVariables extends ApolloClient.OperationVariables>(document: ApolloClient.TypedDocumentNode<TData, TVariables>, props: PaginationQueryProps<TData, TVariables>) => PaginationQuery<TData>;
72
+ export declare const usePaginationQuery: <TData, TVariables extends ApolloClient.OperationVariables>(document: ApolloClient.TypedDocumentNode<TData, TVariables>, { ...props }: PaginationQueryProps<TData, TVariables>) => PaginationQuery<TData>;