@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.
- package/LICENSE +49 -0
- package/README.md +97 -0
- package/package.json +77 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -0
- package/src/react/context/ApolloContext.d.ts +8 -0
- package/src/react/context/ApolloContext.js +21 -0
- package/src/react/context/ApolloProvider.d.ts +9 -0
- package/src/react/context/ApolloProvider.js +15 -0
- package/src/react/hooks/constants.d.ts +2 -0
- package/src/react/hooks/constants.js +1 -0
- package/src/react/hooks/internal/__use.d.ts +3 -0
- package/src/react/hooks/internal/__use.js +9 -0
- package/src/react/hooks/internal/index.d.ts +5 -0
- package/src/react/hooks/internal/index.js +6 -0
- package/src/react/hooks/internal/site.d.ts +2 -0
- package/src/react/hooks/internal/site.js +20 -0
- package/src/react/hooks/internal/useDeepMemo.d.ts +5 -0
- package/src/react/hooks/internal/useDeepMemo.js +11 -0
- package/src/react/hooks/internal/useIsomorphicLayoutEffect.d.ts +2 -0
- package/src/react/hooks/internal/useIsomorphicLayoutEffect.js +8 -0
- package/src/react/hooks/internal/useRenderGuard.d.ts +1 -0
- package/src/react/hooks/internal/useRenderGuard.js +14 -0
- package/src/react/hooks/internal/useSuspenseHookCacheKey.d.ts +13 -0
- package/src/react/hooks/internal/useSuspenseHookCacheKey.js +19 -0
- package/src/react/hooks/internal/validateSuspenseHookOptions.d.ts +4 -0
- package/src/react/hooks/internal/validateSuspenseHookOptions.js +16 -0
- package/src/react/hooks/internal/wrapHook.d.ts +77 -0
- package/src/react/hooks/internal/wrapHook.js +64 -0
- package/src/react/hooks/useApolloClient.d.ts +17 -0
- package/src/react/hooks/useApolloClient.js +27 -0
- package/src/react/hooks/useBackgroundQuery.d.ts +3061 -0
- package/src/react/hooks/useBackgroundQuery.js +88 -0
- package/src/react/hooks/useFragment.d.ts +205 -0
- package/src/react/hooks/useFragment.js +75 -0
- package/src/react/hooks/useLazyQuery.d.ts +947 -0
- package/src/react/hooks/useLazyQuery.js +156 -0
- package/src/react/hooks/useLoadableQuery.d.ts +955 -0
- package/src/react/hooks/useLoadableQuery.js +98 -0
- package/src/react/hooks/useMutation.d.ts +585 -0
- package/src/react/hooks/useMutation.js +121 -0
- package/src/react/hooks/useQuery.d.ts +1401 -0
- package/src/react/hooks/useQuery.js +234 -0
- package/src/react/hooks/useQueryRefHandlers.d.ts +85 -0
- package/src/react/hooks/useQueryRefHandlers.js +79 -0
- package/src/react/hooks/useReactiveVar.d.ts +21 -0
- package/src/react/hooks/useReactiveVar.js +41 -0
- package/src/react/hooks/useReadQuery.d.ts +104 -0
- package/src/react/hooks/useReadQuery.js +83 -0
- package/src/react/hooks/useSubscription.d.ts +289 -0
- package/src/react/hooks/useSubscription.js +258 -0
- package/src/react/hooks/useSuspenseFragment.d.ts +154 -0
- package/src/react/hooks/useSuspenseFragment.js +50 -0
- package/src/react/hooks/useSuspenseQuery.d.ts +1629 -0
- package/src/react/hooks/useSuspenseQuery.js +130 -0
- package/src/react/hooks/useSyncExternalStore.d.ts +2 -0
- package/src/react/hooks/useSyncExternalStore.js +125 -0
- package/src/react/index.d.ts +68 -0
- package/src/react/index.js +18 -0
- package/src/react/internal/cache/FragmentReference.d.ts +41 -0
- package/src/react/internal/cache/FragmentReference.js +117 -0
- package/src/react/internal/cache/QueryReference.d.ts +180 -0
- package/src/react/internal/cache/QueryReference.js +310 -0
- package/src/react/internal/cache/SuspenseCache.d.ts +38 -0
- package/src/react/internal/cache/SuspenseCache.js +39 -0
- package/src/react/internal/cache/getSuspenseCache.d.ts +26 -0
- package/src/react/internal/cache/getSuspenseCache.js +8 -0
- package/src/react/internal/cache/types.d.ts +13 -0
- package/src/react/internal/cache/types.js +1 -0
- package/src/react/internal/index.d.ts +16 -0
- package/src/react/internal/index.js +10 -0
- package/src/react/internal/types.d.ts +15 -0
- package/src/react/internal/types.js +1 -0
- package/src/react/query-preloader/createQueryPreloader.d.ts +630 -0
- package/src/react/query-preloader/createQueryPreloader.js +89 -0
- package/src/react/types/deprecated.d.ts +140 -0
- package/src/react/types/deprecated.js +1 -0
- package/src/react/types/types.documentation.d.ts +551 -0
- package/src/react/types/types.documentation.js +1 -0
- package/src/testing/index.d.ts +1 -0
- package/src/testing/index.js +1 -0
- package/src/testing/react/MockedProvider.d.ts +21 -0
- package/src/testing/react/MockedProvider.js +53 -0
- package/src/testing/react/index.d.ts +2 -0
- 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
|
+
}
|