@octanejs/apollo-client 0.1.0

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 (85) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +97 -0
  3. package/package.json +77 -0
  4. package/src/index.d.ts +1 -0
  5. package/src/index.js +3 -0
  6. package/src/react/context/ApolloContext.d.ts +8 -0
  7. package/src/react/context/ApolloContext.js +21 -0
  8. package/src/react/context/ApolloProvider.d.ts +9 -0
  9. package/src/react/context/ApolloProvider.js +15 -0
  10. package/src/react/hooks/constants.d.ts +2 -0
  11. package/src/react/hooks/constants.js +1 -0
  12. package/src/react/hooks/internal/__use.d.ts +3 -0
  13. package/src/react/hooks/internal/__use.js +9 -0
  14. package/src/react/hooks/internal/index.d.ts +5 -0
  15. package/src/react/hooks/internal/index.js +6 -0
  16. package/src/react/hooks/internal/site.d.ts +2 -0
  17. package/src/react/hooks/internal/site.js +20 -0
  18. package/src/react/hooks/internal/useDeepMemo.d.ts +5 -0
  19. package/src/react/hooks/internal/useDeepMemo.js +11 -0
  20. package/src/react/hooks/internal/useIsomorphicLayoutEffect.d.ts +2 -0
  21. package/src/react/hooks/internal/useIsomorphicLayoutEffect.js +8 -0
  22. package/src/react/hooks/internal/useRenderGuard.d.ts +1 -0
  23. package/src/react/hooks/internal/useRenderGuard.js +14 -0
  24. package/src/react/hooks/internal/useSuspenseHookCacheKey.d.ts +13 -0
  25. package/src/react/hooks/internal/useSuspenseHookCacheKey.js +19 -0
  26. package/src/react/hooks/internal/validateSuspenseHookOptions.d.ts +4 -0
  27. package/src/react/hooks/internal/validateSuspenseHookOptions.js +16 -0
  28. package/src/react/hooks/internal/wrapHook.d.ts +77 -0
  29. package/src/react/hooks/internal/wrapHook.js +64 -0
  30. package/src/react/hooks/useApolloClient.d.ts +17 -0
  31. package/src/react/hooks/useApolloClient.js +27 -0
  32. package/src/react/hooks/useBackgroundQuery.d.ts +3061 -0
  33. package/src/react/hooks/useBackgroundQuery.js +88 -0
  34. package/src/react/hooks/useFragment.d.ts +205 -0
  35. package/src/react/hooks/useFragment.js +75 -0
  36. package/src/react/hooks/useLazyQuery.d.ts +947 -0
  37. package/src/react/hooks/useLazyQuery.js +156 -0
  38. package/src/react/hooks/useLoadableQuery.d.ts +955 -0
  39. package/src/react/hooks/useLoadableQuery.js +98 -0
  40. package/src/react/hooks/useMutation.d.ts +585 -0
  41. package/src/react/hooks/useMutation.js +121 -0
  42. package/src/react/hooks/useQuery.d.ts +1401 -0
  43. package/src/react/hooks/useQuery.js +234 -0
  44. package/src/react/hooks/useQueryRefHandlers.d.ts +85 -0
  45. package/src/react/hooks/useQueryRefHandlers.js +79 -0
  46. package/src/react/hooks/useReactiveVar.d.ts +21 -0
  47. package/src/react/hooks/useReactiveVar.js +41 -0
  48. package/src/react/hooks/useReadQuery.d.ts +104 -0
  49. package/src/react/hooks/useReadQuery.js +83 -0
  50. package/src/react/hooks/useSubscription.d.ts +289 -0
  51. package/src/react/hooks/useSubscription.js +258 -0
  52. package/src/react/hooks/useSuspenseFragment.d.ts +154 -0
  53. package/src/react/hooks/useSuspenseFragment.js +50 -0
  54. package/src/react/hooks/useSuspenseQuery.d.ts +1629 -0
  55. package/src/react/hooks/useSuspenseQuery.js +130 -0
  56. package/src/react/hooks/useSyncExternalStore.d.ts +2 -0
  57. package/src/react/hooks/useSyncExternalStore.js +125 -0
  58. package/src/react/index.d.ts +68 -0
  59. package/src/react/index.js +18 -0
  60. package/src/react/internal/cache/FragmentReference.d.ts +41 -0
  61. package/src/react/internal/cache/FragmentReference.js +117 -0
  62. package/src/react/internal/cache/QueryReference.d.ts +180 -0
  63. package/src/react/internal/cache/QueryReference.js +310 -0
  64. package/src/react/internal/cache/SuspenseCache.d.ts +38 -0
  65. package/src/react/internal/cache/SuspenseCache.js +39 -0
  66. package/src/react/internal/cache/getSuspenseCache.d.ts +26 -0
  67. package/src/react/internal/cache/getSuspenseCache.js +8 -0
  68. package/src/react/internal/cache/types.d.ts +13 -0
  69. package/src/react/internal/cache/types.js +1 -0
  70. package/src/react/internal/index.d.ts +16 -0
  71. package/src/react/internal/index.js +10 -0
  72. package/src/react/internal/types.d.ts +15 -0
  73. package/src/react/internal/types.js +1 -0
  74. package/src/react/query-preloader/createQueryPreloader.d.ts +630 -0
  75. package/src/react/query-preloader/createQueryPreloader.js +89 -0
  76. package/src/react/types/deprecated.d.ts +140 -0
  77. package/src/react/types/deprecated.js +1 -0
  78. package/src/react/types/types.documentation.d.ts +551 -0
  79. package/src/react/types/types.documentation.js +1 -0
  80. package/src/testing/index.d.ts +1 -0
  81. package/src/testing/index.js +1 -0
  82. package/src/testing/react/MockedProvider.d.ts +21 -0
  83. package/src/testing/react/MockedProvider.js +53 -0
  84. package/src/testing/react/index.d.ts +2 -0
  85. package/src/testing/react/index.js +1 -0
@@ -0,0 +1,89 @@
1
+ import {
2
+ assertWrappedQueryRef,
3
+ getWrappedPromise,
4
+ InternalQueryReference,
5
+ wrapQueryRef,
6
+ } from '@octanejs/apollo-client/react/internal';
7
+ import { FinalizationRegistry } from '@apollo/client/utilities/internal/ponyfills';
8
+ import { wrapHook } from '../hooks/internal/index.js';
9
+ /**
10
+ * A higher order function that returns a `preloadQuery` function which
11
+ * can be used to begin loading a query with the given `client`. This is useful
12
+ * when you want to start loading a query as early as possible outside of a
13
+ * React component.
14
+ *
15
+ * > Refer to the [Suspense - Initiating queries outside React](https://www.apollographql.com/docs/react/data/suspense#initiating-queries-outside-react) section for a more in-depth overview.
16
+ *
17
+ * @param client - The `ApolloClient` instance that will be used to load queries
18
+ * from the returned `preloadQuery` function.
19
+ * @returns The `preloadQuery` function.
20
+ *
21
+ * @example
22
+ *
23
+ * ```js
24
+ * const preloadQuery = createQueryPreloader(client);
25
+ * ```
26
+ */
27
+ export function createQueryPreloader(client) {
28
+ return wrapHook('createQueryPreloader', _createQueryPreloader, client)(client);
29
+ }
30
+ const _createQueryPreloader = (client) => {
31
+ function preloadQuery(query, options = {}) {
32
+ const queryRef = new InternalQueryReference(
33
+ client.watchQuery({
34
+ ...options,
35
+ query,
36
+ notifyOnNetworkStatusChange: false,
37
+ }),
38
+ {
39
+ autoDisposeTimeoutMs: client.defaultOptions.react?.suspense?.autoDisposeTimeoutMs,
40
+ },
41
+ );
42
+ const wrapped = wrapQueryRef(queryRef);
43
+ softRetainWhileReferenced(wrapped, queryRef);
44
+ return wrapped;
45
+ }
46
+ return Object.assign(preloadQuery, {
47
+ toPromise(queryRef) {
48
+ assertWrappedQueryRef(queryRef);
49
+ return getWrappedPromise(queryRef).then(() => queryRef);
50
+ },
51
+ });
52
+ };
53
+ /**
54
+ * Soft-retains the underlying `InternalQueryReference` while the `PreloadedQueryRef`
55
+ * is still reachable.
56
+ * When the `PreloadedQueryRef` is garbage collected, the soft retain is
57
+ * disposed of, but only after the initial query has finished loading.
58
+ * Once the `InternalQueryReference` is properly retained, the check for garbage
59
+ * collection is unregistered and the soft retain is disposed of immediately.
60
+ */
61
+ // this is an individual function to avoid closing over any values more than necessary
62
+ function softRetainWhileReferenced(wrapped, queryRef) {
63
+ const { softDispose, delayedSoftDispose } = getCleanup(queryRef);
64
+ registry.register(wrapped, delayedSoftDispose, queryRef);
65
+ // This will unregister the cleanup from the finalization registry when
66
+ // the queryRef is properly retained.
67
+ // This is mostly done to keep the FinalizationRegistry from holding too many
68
+ // cleanup functions, as our React Native polyfill has to iterate all of them regularly.
69
+ queryRef.retain = unregisterOnRetain(queryRef.retain, softDispose);
70
+ }
71
+ // this is an individual function to avoid closing over any values more than necessary
72
+ function unregisterOnRetain(originalRetain, softDispose) {
73
+ return function (...args) {
74
+ registry.unregister(this);
75
+ const dispose = originalRetain.apply(this, args);
76
+ softDispose();
77
+ return dispose;
78
+ };
79
+ }
80
+ // this is an individual function to avoid closing over any values more than necessary
81
+ function getCleanup(queryRef) {
82
+ const softDispose = queryRef.softRetain();
83
+ const initialPromise = queryRef.promise;
84
+ return {
85
+ softDispose,
86
+ delayedSoftDispose: () => initialPromise.finally(softDispose).catch(() => {}),
87
+ };
88
+ }
89
+ const registry = new FinalizationRegistry((cleanup) => cleanup());
@@ -0,0 +1,140 @@
1
+ import type { ApolloCache, DefaultContext, OperationVariables } from '@apollo/client';
2
+ import type { useBackgroundQuery } from '../hooks/useBackgroundQuery.js';
3
+ import type { useFragment } from '../hooks/useFragment.js';
4
+ import type { useLazyQuery } from '../hooks/useLazyQuery.js';
5
+ import type { useLoadableQuery } from '../hooks/useLoadableQuery.js';
6
+ import type { useMutation } from '../hooks/useMutation.js';
7
+ import type { useQuery } from '../hooks/useQuery.js';
8
+ import type { useQueryRefHandlers } from '../hooks/useQueryRefHandlers.js';
9
+ import type { useReadQuery } from '../hooks/useReadQuery.js';
10
+ import type { useSubscription } from '../hooks/useSubscription.js';
11
+ import type { useSuspenseFragment } from '../hooks/useSuspenseFragment.js';
12
+ import type { useSuspenseQuery } from '../hooks/useSuspenseQuery.js';
13
+ /** @deprecated Use `useQuery.Options` instead */
14
+ export type QueryHookOptions<
15
+ TData = unknown,
16
+ TVariables extends OperationVariables = OperationVariables,
17
+ > = useQuery.Options<TData, TVariables>;
18
+ /** @deprecated Use `useQuery.Result` instead */
19
+ export type QueryResult<
20
+ TData = unknown,
21
+ TVariables extends OperationVariables = OperationVariables,
22
+ > = useQuery.Result<TData, TVariables>;
23
+ /** @deprecated Use `useLazyQuery.Options` instead */
24
+ export type LazyQueryHookOptions<
25
+ TData = unknown,
26
+ TVariables extends OperationVariables = OperationVariables,
27
+ > = useLazyQuery.Options<TData, TVariables>;
28
+ /** @deprecated Use `useLazyQuery.Result` instead */
29
+ export type LazyQueryResult<
30
+ TData = unknown,
31
+ TVariables extends OperationVariables = OperationVariables,
32
+ > = useLazyQuery.Result<TData, TVariables>;
33
+ /** @deprecated Use `useLazyQuery.ResultTuple` instead */
34
+ export type LazyQueryResultTuple<
35
+ TData = unknown,
36
+ TVariables extends OperationVariables = OperationVariables,
37
+ > = useLazyQuery.ResultTuple<TData, TVariables>;
38
+ /** @deprecated Use `useLazyQuery.ExecOptions` instead */
39
+ export type LazyQueryHookExecOptions<TVariables extends OperationVariables = OperationVariables> =
40
+ useLazyQuery.ExecOptions<TVariables>;
41
+ /** @deprecated Use `useLazyQuery.ExecFunction` instead */
42
+ export type LazyQueryExecFunction<
43
+ TData = unknown,
44
+ TVariables extends OperationVariables = OperationVariables,
45
+ > = useLazyQuery.ExecFunction<TData, TVariables>;
46
+ /** @deprecated Use `useMutation.Options` instead */
47
+ export type MutationHookOptions<
48
+ TData = unknown,
49
+ TVariables extends OperationVariables = OperationVariables,
50
+ _TContext = DefaultContext,
51
+ TCache extends ApolloCache = ApolloCache,
52
+ > = useMutation.Options<TData, TVariables, TCache>;
53
+ /** @deprecated Use `useMutation.Result` instead */
54
+ export type MutationResult<TData = unknown> = useMutation.Result<TData>;
55
+ /** @deprecated Use `useMutation.MutationFunctionOptions` instead */
56
+ export type MutationFunctionOptions<
57
+ TData = unknown,
58
+ TVariables extends OperationVariables = OperationVariables,
59
+ _TContext = DefaultContext,
60
+ TCache extends ApolloCache = ApolloCache,
61
+ > = useMutation.MutationFunctionOptions<TData, TVariables, TCache>;
62
+ /** @deprecated Use `useMutation.ResultTuple` instead */
63
+ export type MutationTuple<
64
+ TData,
65
+ TVariables extends OperationVariables,
66
+ _TContext = DefaultContext,
67
+ TCache extends ApolloCache = ApolloCache,
68
+ > = useMutation.ResultTuple<TData, TVariables, TCache>;
69
+ /** @deprecated Use `useSubscription.Result` instead */
70
+ export type SubscriptionResult<
71
+ TData = unknown,
72
+ TVariables extends OperationVariables = OperationVariables,
73
+ > = useSubscription.Result<TData>;
74
+ /** @deprecated Use `useSubscription.Options` instead */
75
+ export type SubscriptionHookOptions<
76
+ TData = unknown,
77
+ TVariables extends OperationVariables = OperationVariables,
78
+ > = useSubscription.Options<TData, TVariables>;
79
+ /** @deprecated Use `useSubscription.OnDataOptions` instead */
80
+ export type OnDataOptions<TData = unknown> = useSubscription.OnDataOptions<TData>;
81
+ /** @deprecated Use `useSubscription.OnSubscriptionDataOptions` instead */
82
+ export type OnSubscriptionDataOptions<TData = unknown> =
83
+ useSubscription.OnSubscriptionDataOptions<TData>;
84
+ /** @deprecated Use `useFragment.Options` instead */
85
+ export type UseFragmentOptions<TData, TVariables extends OperationVariables> = useFragment.Options<
86
+ TData,
87
+ TVariables
88
+ >;
89
+ /** @deprecated Use `useFragment.Result` instead */
90
+ export type UseFragmentResult<TData> = useFragment.Result<TData>;
91
+ /** @deprecated Use `useSuspenseQuery.Options` instead */
92
+ export type SuspenseQueryHookOptions<
93
+ TData = unknown,
94
+ TVariables extends OperationVariables = OperationVariables,
95
+ > = useSuspenseQuery.Options<TVariables>;
96
+ /** @deprecated Use `useSuspenseQuery.Result` instead */
97
+ export type UseSuspenseQueryResult<
98
+ TData = unknown,
99
+ TVariables extends OperationVariables = OperationVariables,
100
+ > = useSuspenseQuery.Result<TData, TVariables>;
101
+ /** @deprecated Use `useSuspenseQuery.FetchPolicy` instead */
102
+ export type SuspenseQueryHookFetchPolicy = useSuspenseQuery.FetchPolicy;
103
+ /** @deprecated Use `useBackgroundQuery.Options` instead */
104
+ export type BackgroundQueryHookOptions<
105
+ TData = unknown,
106
+ TVariables extends OperationVariables = OperationVariables,
107
+ > = useBackgroundQuery.Options<TVariables>;
108
+ /** @deprecated Use `useBackgroundQuery.Result` instead */
109
+ export type UseBackgroundQueryResult<
110
+ TData = unknown,
111
+ TVariables extends OperationVariables = OperationVariables,
112
+ > = useBackgroundQuery.Result<TData, TVariables>;
113
+ /** @deprecated Use `useBackgroundQuery.FetchPolicy` instead */
114
+ export type BackgroundQueryHookFetchPolicy = useBackgroundQuery.FetchPolicy;
115
+ /** @deprecated Use `useSuspenseFragment.Options` instead */
116
+ export type UseSuspenseFragmentOptions<
117
+ TData,
118
+ TVariables extends OperationVariables,
119
+ > = useSuspenseFragment.Options<TData, TVariables>;
120
+ /** @deprecated Use `useSuspenseFragment.Result` instead */
121
+ export type UseSuspenseFragmentResult<TData> = useSuspenseFragment.Result<TData>;
122
+ /** @deprecated Use `useLoadableQuery.LoadQueryFunction` instead */
123
+ export type LoadQueryFunction<TVariables extends OperationVariables> =
124
+ useLoadableQuery.LoadQueryFunction<TVariables>;
125
+ /** @deprecated Use `useLoadableQuery.FetchPolicy` instead */
126
+ export type LoadableQueryFetchPolicy = useLoadableQuery.FetchPolicy;
127
+ /** @deprecated Use `useLoadableQuery.Options` instead */
128
+ export type LoadableQueryHookOptions = useLoadableQuery.Options;
129
+ /** @deprecated Use `useLoadableQuery.Result` instead */
130
+ export type UseLoadableQueryResult<
131
+ TData = unknown,
132
+ TVariables extends OperationVariables = OperationVariables,
133
+ > = useLoadableQuery.Result<TData, TVariables>;
134
+ /** @deprecated Use `useQueryRefHandlers.Result` instead */
135
+ export type UseQueryRefHandlersResult<
136
+ TData = unknown,
137
+ TVariables extends OperationVariables = OperationVariables,
138
+ > = useQueryRefHandlers.Result<TData, TVariables>;
139
+ /** @deprecated Use `useReadQuery.Result` instead */
140
+ export type UseReadQueryResult<TData = unknown> = useReadQuery.Result<TData>;
@@ -0,0 +1 @@
1
+ export {};