@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,98 @@
1
+ import * as React from 'octane';
2
+ import { canonicalStringify } from '@apollo/client/cache';
3
+ import {
4
+ assertWrappedQueryRef,
5
+ getSuspenseCache,
6
+ unwrapQueryRef,
7
+ updateWrappedQueryRef,
8
+ wrapQueryRef,
9
+ } from '@octanejs/apollo-client/react/internal';
10
+ import { __DEV__ } from '@apollo/client/utilities/environment';
11
+ import { invariant } from '@apollo/client/utilities/invariant';
12
+ import { useDeepMemo, useRenderGuard } from './internal/index.js';
13
+ import { isCompilerSite } from './internal/site.js';
14
+ import { validateSuspenseHookOptions } from './internal/validateSuspenseHookOptions.js';
15
+ import { useApolloClient } from './useApolloClient.js';
16
+ export const useLoadableQuery = function useLoadableQuery(query, options = {}) {
17
+ if (isCompilerSite(options)) options = {};
18
+ const client = useApolloClient(options.client);
19
+ const suspenseCache = getSuspenseCache(client);
20
+ const watchQueryOptions = useWatchQueryOptions({ client, query, options });
21
+ const { queryKey = [] } = options;
22
+ const [queryRef, setQueryRef] = React.useState(null);
23
+ assertWrappedQueryRef(queryRef);
24
+ const internalQueryRef = queryRef && unwrapQueryRef(queryRef);
25
+ if (queryRef && internalQueryRef?.didChangeOptions(watchQueryOptions)) {
26
+ const promise = internalQueryRef.applyOptions(watchQueryOptions);
27
+ updateWrappedQueryRef(queryRef, promise);
28
+ }
29
+ const calledDuringRender = useRenderGuard();
30
+ const fetchMore = React.useCallback(
31
+ (options) => {
32
+ if (!internalQueryRef) {
33
+ throw new Error('The query has not been loaded. Please load the query.');
34
+ }
35
+ const promise = internalQueryRef.fetchMore(options);
36
+ setQueryRef(wrapQueryRef(internalQueryRef));
37
+ return promise;
38
+ },
39
+ [internalQueryRef],
40
+ );
41
+ const refetch = React.useCallback(
42
+ (options) => {
43
+ if (!internalQueryRef) {
44
+ throw new Error('The query has not been loaded. Please load the query.');
45
+ }
46
+ const promise = internalQueryRef.refetch(options);
47
+ setQueryRef(wrapQueryRef(internalQueryRef));
48
+ return promise;
49
+ },
50
+ [internalQueryRef],
51
+ );
52
+ const loadQuery = React.useCallback(
53
+ (...args) => {
54
+ invariant(!calledDuringRender(), 31);
55
+ const [variables] = args;
56
+ const cacheKey = [query, canonicalStringify(variables), ...[].concat(queryKey)];
57
+ const queryRef = suspenseCache.getQueryRef(cacheKey, () =>
58
+ client.watchQuery({
59
+ ...watchQueryOptions,
60
+ variables,
61
+ }),
62
+ );
63
+ setQueryRef(wrapQueryRef(queryRef));
64
+ },
65
+ [query, queryKey, suspenseCache, watchQueryOptions, calledDuringRender, client],
66
+ );
67
+ const subscribeToMore = React.useCallback(
68
+ (options) => {
69
+ invariant(internalQueryRef, 32);
70
+ return internalQueryRef.observable.subscribeToMore(
71
+ // TODO: The internalQueryRef doesn't have TVariables' type information so we have to cast it here
72
+ options,
73
+ );
74
+ },
75
+ [internalQueryRef],
76
+ );
77
+ const reset = React.useCallback(() => {
78
+ setQueryRef(null);
79
+ }, []);
80
+ return [loadQuery, queryRef, { fetchMore, refetch, reset, subscribeToMore }];
81
+ };
82
+ function useWatchQueryOptions({ client, query, options }) {
83
+ return useDeepMemo(() => {
84
+ const fetchPolicy =
85
+ options.fetchPolicy || client.defaultOptions.watchQuery?.fetchPolicy || 'cache-first';
86
+ const watchQueryOptions = {
87
+ ...options,
88
+ fetchPolicy,
89
+ query,
90
+ notifyOnNetworkStatusChange: false,
91
+ nextFetchPolicy: void 0,
92
+ };
93
+ if (__DEV__) {
94
+ validateSuspenseHookOptions(watchQueryOptions);
95
+ }
96
+ return watchQueryOptions;
97
+ }, [client, options, query]);
98
+ }
@@ -0,0 +1,585 @@
1
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import type {
3
+ ApolloCache,
4
+ ApolloClient,
5
+ DefaultContext,
6
+ DocumentNode,
7
+ ErrorLike,
8
+ ErrorPolicy,
9
+ InternalRefetchQueriesInclude,
10
+ MaybeMasked,
11
+ MutationFetchPolicy,
12
+ MutationQueryReducersMap,
13
+ MutationUpdaterFunction,
14
+ NormalizedExecutionResult,
15
+ OnQueryUpdated,
16
+ OperationVariables,
17
+ Unmasked,
18
+ } from '@apollo/client';
19
+ import type { IgnoreModifier } from '@apollo/client/cache';
20
+ import type {
21
+ LazyType,
22
+ NoInfer,
23
+ Prettify,
24
+ SignatureStyle,
25
+ } from '@apollo/client/utilities/internal';
26
+ type MakeRequiredVariablesOptional<
27
+ TVariables extends OperationVariables,
28
+ TConfiguredVariables extends Partial<TVariables>,
29
+ > = Prettify<
30
+ {
31
+ [K in keyof TVariables as K extends keyof TConfiguredVariables ? K : never]?: TVariables[K];
32
+ } & Omit<TVariables, keyof TConfiguredVariables>
33
+ >;
34
+ type ExtractConfiguredVariables<
35
+ TOptions,
36
+ TVariables extends OperationVariables,
37
+ > = 'variables' extends keyof TOptions
38
+ ? Exclude<TOptions['variables'], undefined> extends infer V
39
+ ? [V] extends [never]
40
+ ? {}
41
+ : V extends Partial<TVariables>
42
+ ? V
43
+ : {}
44
+ : {}
45
+ : {};
46
+ export declare namespace useMutation {
47
+ interface Options<
48
+ TData = unknown,
49
+ TVariables extends OperationVariables = OperationVariables,
50
+ TCache extends ApolloCache = ApolloCache,
51
+ TConfiguredVariables extends Partial<TVariables> = Partial<TVariables>,
52
+ > {
53
+ /**
54
+ * By providing either an object or a callback function that, when invoked after
55
+ * a mutation, allows you to return optimistic data and optionally skip updates
56
+ * via the `IGNORE` sentinel object, Apollo Client caches this temporary
57
+ * (and potentially incorrect) response until the mutation completes, enabling
58
+ * more responsive UI updates.
59
+ *
60
+ * For more information, see [Optimistic mutation results](https://www.apollographql.com/docs/react/performance/optimistic-ui/).
61
+ *
62
+ * @docGroup 3. Caching options
63
+ */
64
+ optimisticResponse?:
65
+ | Unmasked<NoInfer<TData>>
66
+ | ((
67
+ vars: TVariables,
68
+ {
69
+ IGNORE,
70
+ }: {
71
+ IGNORE: IgnoreModifier;
72
+ },
73
+ ) => Unmasked<NoInfer<TData>> | IgnoreModifier);
74
+ /**
75
+ * A `MutationQueryReducersMap`, which is map from query names to
76
+ * mutation query reducers. Briefly, this map defines how to incorporate the
77
+ * results of the mutation into the results of queries that are currently
78
+ * being watched by your application.
79
+ */
80
+ updateQueries?: MutationQueryReducersMap<TData>;
81
+ /**
82
+ * An array (or a function that _returns_ an array) that specifies which queries you want to refetch after the mutation occurs.
83
+ *
84
+ * Each array value can be either:
85
+ *
86
+ * - An object containing the `query` to execute, along with any `variables`
87
+ *
88
+ * - A string indicating the operation name of the query to refetch
89
+ *
90
+ * @docGroup 1. Operation options
91
+ */
92
+ refetchQueries?:
93
+ | ((result: NormalizedExecutionResult<Unmasked<TData>>) => InternalRefetchQueriesInclude)
94
+ | InternalRefetchQueriesInclude;
95
+ /**
96
+ * If `true`, makes sure all queries included in `refetchQueries` are completed before the mutation is considered complete.
97
+ *
98
+ * The default value is `false` (queries are refetched asynchronously).
99
+ *
100
+ * @docGroup 1. Operation options
101
+ */
102
+ awaitRefetchQueries?: boolean;
103
+ /**
104
+ * A function used to update the Apollo Client cache after the mutation completes.
105
+ *
106
+ * For more information, see [Updating the cache after a mutation](https://www.apollographql.com/docs/react/data/mutations#updating-the-cache-after-a-mutation).
107
+ *
108
+ * @docGroup 3. Caching options
109
+ */
110
+ update?: MutationUpdaterFunction<TData, TVariables, TCache>;
111
+ /**
112
+ * Optional callback for intercepting queries whose cache data has been updated by the mutation, as well as any queries specified in the `refetchQueries: [...]` list passed to `client.mutate`.
113
+ *
114
+ * Returning a `Promise` from `onQueryUpdated` will cause the final mutation `Promise` to await the returned `Promise`. Returning `false` causes the query to be ignored.
115
+ *
116
+ * @docGroup 1. Operation options
117
+ */
118
+ onQueryUpdated?: OnQueryUpdated<any>;
119
+ /**
120
+ * Specifies how the mutation handles a response that returns both GraphQL errors and partial results.
121
+ *
122
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
123
+ *
124
+ * The default value is `none`, meaning that the mutation result includes error details but _not_ partial results.
125
+ *
126
+ * @docGroup 1. Operation options
127
+ */
128
+ errorPolicy?: ErrorPolicy;
129
+ /**
130
+ * An object containing all of the GraphQL variables your mutation requires to execute.
131
+ *
132
+ * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
133
+ *
134
+ * @docGroup 1. Operation options
135
+ */
136
+ variables?: Partial<TVariables> & TConfiguredVariables;
137
+ /**
138
+ * If you're using [Apollo Link](https://www.apollographql.com/docs/react/api/link/introduction/), this object is the initial value of the `context` object that's passed along your link chain.
139
+ *
140
+ * @docGroup 2. Networking options
141
+ */
142
+ context?: DefaultContext;
143
+ /**
144
+ * Provide `no-cache` if the mutation's result should _not_ be written to the Apollo Client cache.
145
+ *
146
+ * The default value is `network-only` (which means the result _is_ written to the cache).
147
+ *
148
+ * Unlike queries, mutations _do not_ support [fetch policies](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy) besides `network-only` and `no-cache`.
149
+ *
150
+ * @docGroup 3. Caching options
151
+ */
152
+ fetchPolicy?: MutationFetchPolicy;
153
+ /**
154
+ * To avoid retaining sensitive information from mutation root field
155
+ * arguments, Apollo Client v3.4+ automatically clears any `ROOT_MUTATION`
156
+ * fields from the cache after each mutation finishes. If you need this
157
+ * information to remain in the cache, you can prevent the removal by passing
158
+ * `keepRootFields: true` to the mutation. `ROOT_MUTATION` result data are
159
+ * also passed to the mutation `update` function, so we recommend obtaining
160
+ * the results that way, rather than using this option, if possible.
161
+ */
162
+ keepRootFields?: boolean;
163
+ /**
164
+ * The instance of `ApolloClient` to use to execute the mutation.
165
+ *
166
+ * By default, the instance that's passed down via context is used, but you can provide a different instance here.
167
+ *
168
+ * @docGroup 2. Networking options
169
+ */
170
+ client?: ApolloClient;
171
+ /**
172
+ * If `true`, the in-progress mutation's associated component re-renders whenever the network status changes or a network error occurs.
173
+ *
174
+ * The default value is `true`.
175
+ *
176
+ * @docGroup 2. Networking options
177
+ */
178
+ notifyOnNetworkStatusChange?: boolean;
179
+ /**
180
+ * A callback function that's called when your mutation successfully completes with zero errors (or if `errorPolicy` is `ignore` and partial data is returned).
181
+ *
182
+ * This function is passed the mutation's result `data` and any options passed to the mutation.
183
+ *
184
+ * @docGroup 1. Operation options
185
+ */
186
+ onCompleted?: (
187
+ data: MaybeMasked<TData>,
188
+ clientOptions?: Options<TData, TVariables, TCache>,
189
+ ) => void;
190
+ /**
191
+ * A callback function that's called when the mutation encounters one or more errors (unless `errorPolicy` is `ignore`).
192
+ *
193
+ * This function is passed an [`ApolloError`](https://github.com/apollographql/apollo-client/blob/d96f4578f89b933c281bb775a39503f6cdb59ee8/src/errors/index.ts#L36-L39) object that contains either a `networkError` object or a `graphQLErrors` array, depending on the error(s) that occurred, as well as any options passed the mutation.
194
+ *
195
+ * @docGroup 1. Operation options
196
+ */
197
+ onError?: (error: ErrorLike, clientOptions?: Options<TData, TVariables, TCache>) => void;
198
+ }
199
+ namespace Base {
200
+ interface Result {
201
+ /**
202
+ * If `true`, the mutation is currently in flight.
203
+ */
204
+ loading: boolean;
205
+ /**
206
+ * If `true`, the mutation's mutate function has been called.
207
+ */
208
+ called: boolean;
209
+ /**
210
+ * The instance of Apollo Client that executed the mutation.
211
+ *
212
+ * Can be useful for manually executing followup operations or writing data to the cache.
213
+ */
214
+ client: ApolloClient;
215
+ /**
216
+ * A function that you can call to reset the mutation's result to its initial, uncalled state.
217
+ */
218
+ reset: () => void;
219
+ }
220
+ }
221
+ /**
222
+ * Maps `errorPolicy` to the shape of `data` and `error` as observable from
223
+ * the hook result state.
224
+ *
225
+ * The hook has additional states (before call, during loading) where `data`
226
+ * and `error` are `undefined`, so `data` remains nullable even on error
227
+ * policies that would otherwise guarantee it. Only `error` is narrowed away
228
+ * for `"ignore"`, since the underlying `client.mutate` promise never rejects
229
+ * and never resolves with an error for that policy.
230
+ */
231
+ type ResultStateMap<TData = unknown> = {
232
+ none: {
233
+ /**
234
+ * The data returned from your mutation. Can be `undefined` if the `errorPolicy`
235
+ * is `all` or `ignore` and the server returns a GraphQL response with `errors`
236
+ * but not `data` or a network error is returned.
237
+ */
238
+ data: MaybeMasked<TData> | null | undefined;
239
+ /**
240
+ * If the mutation produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
241
+ *
242
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
243
+ */
244
+ error: ErrorLike | undefined;
245
+ };
246
+ all: {
247
+ /**
248
+ * The data returned from your mutation. Can be `undefined` if the `errorPolicy`
249
+ * is `all` or `ignore` and the server returns a GraphQL response with `errors`
250
+ * but not `data` or a network error is returned.
251
+ */
252
+ data: MaybeMasked<TData> | null | undefined;
253
+ /**
254
+ * If the mutation produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
255
+ *
256
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
257
+ */
258
+ error: ErrorLike | undefined;
259
+ };
260
+ ignore: {
261
+ /**
262
+ * The data returned from your mutation. Can be `undefined` if the `errorPolicy`
263
+ * is `all` or `ignore` and the server returns a GraphQL response with `errors`
264
+ * but not `data` or a network error is returned.
265
+ */
266
+ data: MaybeMasked<TData> | null | undefined;
267
+ /**
268
+ * If the mutation produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
269
+ *
270
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
271
+ */
272
+ error: undefined;
273
+ };
274
+ undefined: {
275
+ /**
276
+ * The data returned from your mutation. Can be `undefined` if the `errorPolicy`
277
+ * is `all` or `ignore` and the server returns a GraphQL response with `errors`
278
+ * but not `data` or a network error is returned.
279
+ */
280
+ data: MaybeMasked<TData> | null | undefined;
281
+ /**
282
+ * If the mutation produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
283
+ *
284
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
285
+ */
286
+ error: ErrorLike | undefined;
287
+ };
288
+ };
289
+ type Result<
290
+ TData = unknown,
291
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
292
+ > = Base.Result & ResultStateMap<TData>[`${TErrorPolicy}`];
293
+ namespace DocumentationTypes {
294
+ namespace useMutation {
295
+ interface Result<TData = unknown> extends Base.Result {
296
+ /**
297
+ * The data returned from your mutation. Can be `undefined` if the `errorPolicy`
298
+ * is `all` or `ignore` and the server returns a GraphQL response with `errors`
299
+ * but not `data` or a network error is returned.
300
+ */
301
+ data: MaybeMasked<TData> | null | undefined;
302
+ /**
303
+ * If the mutation produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
304
+ *
305
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
306
+ */
307
+ error: ErrorLike | undefined;
308
+ }
309
+ }
310
+ }
311
+ type ResultTuple<
312
+ TData,
313
+ TVariables extends OperationVariables,
314
+ TCache extends ApolloCache = ApolloCache,
315
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
316
+ > = [
317
+ mutate: MutationFunction<TData, TVariables, TCache, TErrorPolicy>,
318
+ result: Result<TData, TErrorPolicy>,
319
+ ];
320
+ type MutationFunction<
321
+ TData,
322
+ TVariables extends OperationVariables,
323
+ TCache extends ApolloCache = ApolloCache,
324
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
325
+ > = (
326
+ ...[options]: {} extends TVariables
327
+ ? [
328
+ options?: MutationFunctionOptions<TData, TVariables, TCache> & {
329
+ /**
330
+ * An object containing all of the GraphQL variables your mutation requires to execute.
331
+ *
332
+ * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
333
+ *
334
+ * @docGroup 1. Operation options
335
+ */
336
+ variables?: TVariables;
337
+ },
338
+ ]
339
+ : [
340
+ options: MutationFunctionOptions<TData, TVariables, TCache> & {
341
+ /**
342
+ * An object containing all of the GraphQL variables your mutation requires to execute.
343
+ *
344
+ * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
345
+ *
346
+ * @docGroup 1. Operation options
347
+ */
348
+ variables: TVariables;
349
+ },
350
+ ]
351
+ ) => Promise<ApolloClient.MutateResult<MaybeMasked<TData>, TErrorPolicy>>;
352
+ type MutationFunctionOptions<
353
+ TData = unknown,
354
+ TVariables extends OperationVariables = OperationVariables,
355
+ TCache extends ApolloCache = ApolloCache,
356
+ > = Options<TData, TVariables, TCache> & {
357
+ /**
358
+ * If you're using [Apollo Link](https://www.apollographql.com/docs/react/api/link/introduction/), this object is the initial value of the `context` object that's passed along your link chain.
359
+ *
360
+ * @docGroup 2. Networking options
361
+ *
362
+ *
363
+ * @remarks
364
+ * When provided as a callback function, the function is called with the
365
+ * value of `context` provided to the `useMutation` hook.
366
+ */
367
+ context?: DefaultContext | ((hookContext: DefaultContext | undefined) => DefaultContext);
368
+ };
369
+ interface DefaultOptions extends ApolloClient.DefaultOptions.Mutate.Calculated {}
370
+ type ResultForOptions<
371
+ TData,
372
+ TVariables extends OperationVariables,
373
+ TCache extends ApolloCache,
374
+ TOptions extends Record<string, never> | Options<TData, TVariables, TCache>,
375
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
376
+ > = LazyType<
377
+ ResultTuple<
378
+ TData,
379
+ MakeRequiredVariablesOptional<TVariables, ExtractConfiguredVariables<TOptions, TVariables>>,
380
+ TCache,
381
+ [TErrorPolicy] extends [undefined]
382
+ ? DefaultOptions extends {
383
+ errorPolicy: infer D;
384
+ }
385
+ ? D
386
+ : undefined
387
+ : TErrorPolicy
388
+ >
389
+ >;
390
+ namespace DocumentationTypes {
391
+ /**
392
+ * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.
393
+ *
394
+ * @example
395
+ *
396
+ * ```jsx
397
+ * import { gql, useMutation } from "@apollo/client";
398
+ *
399
+ * const ADD_TODO = gql`
400
+ * mutation AddTodo($type: String!) {
401
+ * addTodo(type: $type) {
402
+ * id
403
+ * type
404
+ * }
405
+ * }
406
+ * `;
407
+ *
408
+ * function AddTodo() {
409
+ * let input;
410
+ * const [addTodo, { data }] = useMutation(ADD_TODO);
411
+ *
412
+ * return (
413
+ * <div>
414
+ * <form
415
+ * onSubmit={(e) => {
416
+ * e.preventDefault();
417
+ * addTodo({ variables: { type: input.value } });
418
+ * input.value = "";
419
+ * }}
420
+ * >
421
+ * <input
422
+ * ref={(node) => {
423
+ * input = node;
424
+ * }}
425
+ * />
426
+ * <button type="submit">Add Todo</button>
427
+ * </form>
428
+ * </div>
429
+ * );
430
+ * }
431
+ * ```
432
+ *
433
+ * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.
434
+ * @param options - Options to control how the mutation is executed.
435
+ * @returns A tuple in the form of `[mutate, result]`
436
+ */
437
+ interface useMutation {
438
+ <TData = unknown, TVariables extends OperationVariables = OperationVariables>(
439
+ mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
440
+ options?: Options<TData, TVariables>,
441
+ ): ResultTuple<TData, TVariables>;
442
+ }
443
+ }
444
+ namespace Signatures {
445
+ /**
446
+ *
447
+ */
448
+ interface Classic {
449
+ /**
450
+ *
451
+ */
452
+ <
453
+ TData,
454
+ TVariables extends OperationVariables,
455
+ _INFERENCE_ONLY_DO_NOT_SPECIFY extends 'inferred',
456
+ TConfiguredVariables extends Partial<TVariables> = {},
457
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
458
+ >(
459
+ mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
460
+ options?: useMutation.Options<
461
+ NoInfer<TData>,
462
+ NoInfer<TVariables>,
463
+ ApolloCache,
464
+ {
465
+ [K in keyof TConfiguredVariables]: K extends keyof TVariables
466
+ ? TConfiguredVariables[K]
467
+ : never;
468
+ }
469
+ > & {
470
+ /**
471
+ * Specifies how the mutation handles a response that returns both GraphQL errors and partial results.
472
+ *
473
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
474
+ *
475
+ * The default value is `none`, meaning that the mutation result includes error details but _not_ partial results.
476
+ *
477
+ * @docGroup 1. Operation options
478
+ */
479
+ errorPolicy?: TErrorPolicy;
480
+ },
481
+ ): useMutation.ResultTuple<
482
+ TData,
483
+ MakeRequiredVariablesOptional<TVariables, TConfiguredVariables>,
484
+ ApolloCache,
485
+ TErrorPolicy
486
+ >;
487
+ /**
488
+ * @deprecated Avoid manually specifying generics on `useMutation`.
489
+ * Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your mutation results.
490
+ */
491
+ <
492
+ TData,
493
+ TVariables extends OperationVariables = OperationVariables,
494
+ TCache extends ApolloCache = ApolloCache,
495
+ TConfiguredVariables extends Partial<TVariables> = {},
496
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
497
+ >(
498
+ mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
499
+ options?: useMutation.Options<
500
+ NoInfer<TData>,
501
+ NoInfer<TVariables>,
502
+ TCache,
503
+ {
504
+ [K in keyof TConfiguredVariables]: K extends keyof TVariables
505
+ ? TConfiguredVariables[K]
506
+ : never;
507
+ }
508
+ > &
509
+ (TErrorPolicy extends undefined
510
+ ? {}
511
+ : {
512
+ /**
513
+ * Specifies how the mutation handles a response that returns both GraphQL errors and partial results.
514
+ *
515
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
516
+ *
517
+ * The default value is `none`, meaning that the mutation result includes error details but _not_ partial results.
518
+ *
519
+ * @docGroup 1. Operation options
520
+ */
521
+ errorPolicy: TErrorPolicy;
522
+ }),
523
+ ): useMutation.ResultTuple<
524
+ TData,
525
+ MakeRequiredVariablesOptional<TVariables, TConfiguredVariables>,
526
+ TCache,
527
+ TErrorPolicy
528
+ >;
529
+ }
530
+ /**
531
+ *
532
+ */
533
+ interface Modern {
534
+ /**
535
+ *
536
+ */
537
+ <
538
+ TData,
539
+ TVariables extends OperationVariables,
540
+ TCache extends ApolloCache,
541
+ TOptions extends never,
542
+ >(
543
+ mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
544
+ ): useMutation.ResultForOptions<TData, TVariables, TCache, Record<string, never>>;
545
+ /**
546
+ *
547
+ */
548
+ <
549
+ TData,
550
+ TVariables extends OperationVariables,
551
+ TCache extends ApolloCache,
552
+ TOptions extends useMutation.Options<NoInfer<TData>, NoInfer<TVariables>, TCache> & {
553
+ variables?: {
554
+ [K in Exclude<keyof TOptions['variables'], keyof TVariables>]?: never;
555
+ };
556
+ },
557
+ TErrorPolicy extends ErrorPolicy | undefined = undefined,
558
+ >(
559
+ mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
560
+ options?: TOptions & {
561
+ /**
562
+ * Specifies how the mutation handles a response that returns both GraphQL errors and partial results.
563
+ *
564
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
565
+ *
566
+ * The default value is `none`, meaning that the mutation result includes error details but _not_ partial results.
567
+ *
568
+ * @docGroup 1. Operation options
569
+ */
570
+ errorPolicy?: TErrorPolicy;
571
+ },
572
+ ): useMutation.ResultForOptions<TData, TVariables, TCache, TOptions, TErrorPolicy>;
573
+ }
574
+ type Evaluated = SignatureStyle extends 'classic' ? Classic : Modern;
575
+ }
576
+ /**
577
+ *
578
+ */
579
+ interface Signature extends Signatures.Evaluated {}
580
+ }
581
+ /**
582
+ *
583
+ */
584
+ export declare const useMutation: useMutation.Signature;
585
+ export {};