@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,121 @@
1
+ import { equal } from '@wry/equality';
2
+ import * as React from 'octane';
3
+ import { mergeOptions, preventUnhandledRejection } from '@apollo/client/utilities/internal';
4
+ import { useIsomorphicLayoutEffect } from './internal/useIsomorphicLayoutEffect.js';
5
+ import { isCompilerSite } from './internal/site.js';
6
+ import { useApolloClient } from './useApolloClient.js';
7
+ /**
8
+ *
9
+ */
10
+ export const useMutation = function useMutation(mutation, options) {
11
+ if (isCompilerSite(options)) options = undefined;
12
+ const client = useApolloClient(options?.client);
13
+ const [result, setResult] = React.useState(() => createInitialResult(client));
14
+ const ref = React.useRef({
15
+ result,
16
+ mutationId: 0,
17
+ isMounted: true,
18
+ client,
19
+ mutation,
20
+ options,
21
+ });
22
+ useIsomorphicLayoutEffect(() => {
23
+ Object.assign(ref.current, { client, options, mutation });
24
+ }, null);
25
+ const execute = React.useCallback((executeOptions = {}) => {
26
+ const { options, mutation } = ref.current;
27
+ const baseOptions = { ...options, mutation };
28
+ const client = executeOptions.client || ref.current.client;
29
+ const context =
30
+ typeof executeOptions.context === 'function'
31
+ ? executeOptions.context(options?.context)
32
+ : executeOptions.context;
33
+ if (!ref.current.result.loading && ref.current.isMounted) {
34
+ setResult(
35
+ (ref.current.result = {
36
+ loading: true,
37
+ error: undefined,
38
+ data: undefined,
39
+ called: true,
40
+ client,
41
+ }),
42
+ );
43
+ }
44
+ const mutationId = ++ref.current.mutationId;
45
+ const clientOptions = mergeOptions(baseOptions, {
46
+ ...executeOptions,
47
+ context,
48
+ });
49
+ return preventUnhandledRejection(
50
+ client.mutate(clientOptions).then(
51
+ (response) => {
52
+ const { data, error } = response;
53
+ const onError = executeOptions.onError || ref.current.options?.onError;
54
+ if (error && onError) {
55
+ onError(error, clientOptions);
56
+ }
57
+ if (mutationId === ref.current.mutationId) {
58
+ const result = {
59
+ called: true,
60
+ loading: false,
61
+ data,
62
+ error,
63
+ client,
64
+ };
65
+ if (ref.current.isMounted && !equal(ref.current.result, result)) {
66
+ setResult((ref.current.result = result));
67
+ }
68
+ }
69
+ const onCompleted = executeOptions.onCompleted || ref.current.options?.onCompleted;
70
+ if (!error) {
71
+ onCompleted?.(response.data, clientOptions);
72
+ }
73
+ return response;
74
+ },
75
+ (error) => {
76
+ if (mutationId === ref.current.mutationId && ref.current.isMounted) {
77
+ const result = {
78
+ loading: false,
79
+ error,
80
+ data: void 0,
81
+ called: true,
82
+ client,
83
+ };
84
+ if (!equal(ref.current.result, result)) {
85
+ setResult((ref.current.result = result));
86
+ }
87
+ }
88
+ const onError = executeOptions.onError || ref.current.options?.onError;
89
+ if (onError) {
90
+ onError(error, clientOptions);
91
+ }
92
+ throw error;
93
+ },
94
+ ),
95
+ );
96
+ }, []);
97
+ const reset = React.useCallback(() => {
98
+ if (ref.current.isMounted) {
99
+ const result = createInitialResult(ref.current.client);
100
+ Object.assign(ref.current, { mutationId: 0, result });
101
+ setResult(result);
102
+ }
103
+ }, []);
104
+ React.useEffect(() => {
105
+ const current = ref.current;
106
+ current.isMounted = true;
107
+ return () => {
108
+ current.isMounted = false;
109
+ };
110
+ }, []);
111
+ return [execute, { reset, ...result }];
112
+ };
113
+ function createInitialResult(client) {
114
+ return {
115
+ data: undefined,
116
+ error: undefined,
117
+ called: false,
118
+ loading: false,
119
+ client,
120
+ };
121
+ }