@trackunit/react-graphql-hooks 0.0.271 → 0.0.273

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
@@ -134,10 +134,9 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
134
134
  * @returns {*} {PaginationQuery}
135
135
  */
136
136
  const usePaginationQuery = (document, { ...props }) => {
137
- var _a, _b;
137
+ var _a;
138
138
  const [lastFetchedData, setLastFetchedData] = react.useState();
139
139
  const [resetTrigger, setResetTrigger] = react.useState(0);
140
- const [abortController, setAbortController] = react.useState(new AbortController());
141
140
  // Makes **sure** query variables are stable.
142
141
  // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
143
142
  // Maybe better solution exists but this is the best I could come up with without changing to much.
@@ -154,13 +153,6 @@ const usePaginationQuery = (document, { ...props }) => {
154
153
  }, [props]);
155
154
  const [, { fetchMore, networkStatus }] = ApolloClient__namespace.useLazyQuery(document, {
156
155
  ...internalProps,
157
- context: {
158
- ...internalProps.context,
159
- fetchOptions: {
160
- ...(_a = internalProps.context) === null || _a === void 0 ? void 0 : _a.fetchOptions,
161
- signal: abortController.signal,
162
- },
163
- },
164
156
  notifyOnNetworkStatusChange: true, // Needed to update networkStatus
165
157
  onCompleted: () => {
166
158
  if (networkStatus === ApolloClient__namespace.NetworkStatus.refetch) {
@@ -179,13 +171,14 @@ const usePaginationQuery = (document, { ...props }) => {
179
171
  setResetTrigger(prev => prev + 1);
180
172
  }, []);
181
173
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = reactTablePagination.useRelayPagination({
182
- pageSize: internalProps.pageSize || ((_b = internalProps.variables) === null || _b === void 0 ? void 0 : _b.first) || reactTablePagination.defaultPageSize,
174
+ pageSize: internalProps.pageSize || ((_a = internalProps.variables) === null || _a === void 0 ? void 0 : _a.first) || reactTablePagination.defaultPageSize,
183
175
  onReset,
184
176
  });
185
177
  const doFetchMore = react.useCallback((variables, prev) => {
186
178
  if (internalProps.skip) {
187
179
  return;
188
180
  }
181
+ const abortController = new AbortController();
189
182
  setIsLoading(true);
190
183
  /**
191
184
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
@@ -236,27 +229,18 @@ const usePaginationQuery = (document, { ...props }) => {
236
229
  setIsLoading(false);
237
230
  return result.data;
238
231
  },
239
- // It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
240
- // However, if you need to pass in your own onError function, you can do so in the props of the hook.
241
- // But we ignore the error if the request was aborted.
242
- }).catch(error => {
243
- if (abortController.signal.aborted) {
244
- return;
245
- }
246
- if (internalProps.onError) {
247
- return internalProps.onError(error);
248
- }
249
- else {
250
- throw error;
251
- }
252
232
  });
253
- }, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
233
+ return abortController;
234
+ }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
254
235
  react.useEffect(() => {
255
236
  setData(undefined);
256
237
  reset();
257
238
  }, [internalProps.variables, reset]);
258
239
  react.useEffect(() => {
259
- doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
240
+ const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
241
+ return () => {
242
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
243
+ };
260
244
  // eslint-disable-next-line react-hooks/exhaustive-deps
261
245
  }, [internalProps.variables, resetTrigger]);
262
246
  react.useEffect(() => {
@@ -265,11 +249,6 @@ const usePaginationQuery = (document, { ...props }) => {
265
249
  }
266
250
  // eslint-disable-next-line react-hooks/exhaustive-deps
267
251
  }, [after]);
268
- react.useEffect(() => () => {
269
- abortController.abort();
270
- // We need ensure we reset the abort controller on unmounting, as it's not possible to re-use an aborted controller.
271
- setAbortController(new AbortController());
272
- }, [abortController]);
273
252
  return react.useMemo(() => {
274
253
  return {
275
254
  data,
package/index.esm.js CHANGED
@@ -113,10 +113,9 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
113
113
  * @returns {*} {PaginationQuery}
114
114
  */
115
115
  const usePaginationQuery = (document, { ...props }) => {
116
- var _a, _b;
116
+ var _a;
117
117
  const [lastFetchedData, setLastFetchedData] = useState();
118
118
  const [resetTrigger, setResetTrigger] = useState(0);
119
- const [abortController, setAbortController] = useState(new AbortController());
120
119
  // Makes **sure** query variables are stable.
121
120
  // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
122
121
  // Maybe better solution exists but this is the best I could come up with without changing to much.
@@ -133,13 +132,6 @@ const usePaginationQuery = (document, { ...props }) => {
133
132
  }, [props]);
134
133
  const [, { fetchMore, networkStatus }] = ApolloClient.useLazyQuery(document, {
135
134
  ...internalProps,
136
- context: {
137
- ...internalProps.context,
138
- fetchOptions: {
139
- ...(_a = internalProps.context) === null || _a === void 0 ? void 0 : _a.fetchOptions,
140
- signal: abortController.signal,
141
- },
142
- },
143
135
  notifyOnNetworkStatusChange: true, // Needed to update networkStatus
144
136
  onCompleted: () => {
145
137
  if (networkStatus === ApolloClient.NetworkStatus.refetch) {
@@ -158,13 +150,14 @@ const usePaginationQuery = (document, { ...props }) => {
158
150
  setResetTrigger(prev => prev + 1);
159
151
  }, []);
160
152
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = useRelayPagination({
161
- pageSize: internalProps.pageSize || ((_b = internalProps.variables) === null || _b === void 0 ? void 0 : _b.first) || defaultPageSize,
153
+ pageSize: internalProps.pageSize || ((_a = internalProps.variables) === null || _a === void 0 ? void 0 : _a.first) || defaultPageSize,
162
154
  onReset,
163
155
  });
164
156
  const doFetchMore = useCallback((variables, prev) => {
165
157
  if (internalProps.skip) {
166
158
  return;
167
159
  }
160
+ const abortController = new AbortController();
168
161
  setIsLoading(true);
169
162
  /**
170
163
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
@@ -215,27 +208,18 @@ const usePaginationQuery = (document, { ...props }) => {
215
208
  setIsLoading(false);
216
209
  return result.data;
217
210
  },
218
- // It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
219
- // However, if you need to pass in your own onError function, you can do so in the props of the hook.
220
- // But we ignore the error if the request was aborted.
221
- }).catch(error => {
222
- if (abortController.signal.aborted) {
223
- return;
224
- }
225
- if (internalProps.onError) {
226
- return internalProps.onError(error);
227
- }
228
- else {
229
- throw error;
230
- }
231
211
  });
232
- }, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
212
+ return abortController;
213
+ }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
233
214
  useEffect(() => {
234
215
  setData(undefined);
235
216
  reset();
236
217
  }, [internalProps.variables, reset]);
237
218
  useEffect(() => {
238
- doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
219
+ const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
220
+ return () => {
221
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
222
+ };
239
223
  // eslint-disable-next-line react-hooks/exhaustive-deps
240
224
  }, [internalProps.variables, resetTrigger]);
241
225
  useEffect(() => {
@@ -244,11 +228,6 @@ const usePaginationQuery = (document, { ...props }) => {
244
228
  }
245
229
  // eslint-disable-next-line react-hooks/exhaustive-deps
246
230
  }, [after]);
247
- useEffect(() => () => {
248
- abortController.abort();
249
- // We need ensure we reset the abort controller on unmounting, as it's not possible to re-use an aborted controller.
250
- setAbortController(new AbortController());
251
- }, [abortController]);
252
231
  return useMemo(() => {
253
232
  return {
254
233
  data,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-graphql-hooks",
3
- "version": "0.0.271",
3
+ "version": "0.0.273",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {