@apollo/client 4.1.0-alpha.1 → 4.1.0-alpha.3
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/CHANGELOG.md +83 -0
- package/__cjs/cache/core/cache.cjs +148 -50
- package/__cjs/cache/core/cache.cjs.map +1 -1
- package/__cjs/cache/core/cache.d.cts +72 -23
- package/__cjs/cache/inmemory/inMemoryCache.cjs +12 -1
- package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
- package/__cjs/core/ApolloClient.cjs +24 -27
- package/__cjs/core/ApolloClient.cjs.map +1 -1
- package/__cjs/core/ApolloClient.d.cts +87 -2
- package/__cjs/incremental/handlers/defer20220824.cjs +6 -2
- package/__cjs/incremental/handlers/defer20220824.cjs.map +1 -1
- package/__cjs/incremental/handlers/defer20220824.d.cts +0 -2
- package/__cjs/incremental/handlers/graphql17Alpha9.cjs +9 -3
- package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -1
- package/__cjs/incremental/handlers/notImplemented.cjs +1 -1
- package/__cjs/incremental/handlers/notImplemented.cjs.map +1 -1
- package/__cjs/invariantErrorCodes.cjs +2 -2
- package/__cjs/react/hooks/useFragment.cjs +37 -84
- package/__cjs/react/hooks/useFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useFragment.d.cts +65 -6
- package/__cjs/react/hooks/useMutation.cjs +7 -1
- package/__cjs/react/hooks/useMutation.cjs.map +1 -1
- package/__cjs/react/hooks/useMutation.d.cts +13 -1
- package/__cjs/react/hooks/useQuery.d.cts +1 -1
- package/__cjs/react/hooks/useSuspenseFragment.cjs +13 -12
- package/__cjs/react/hooks/useSuspenseFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseFragment.d.cts +33 -7
- package/__cjs/react/internal/cache/FragmentReference.cjs +3 -22
- package/__cjs/react/internal/cache/FragmentReference.cjs.map +1 -1
- package/__cjs/react/internal/cache/FragmentReference.d.cts +2 -4
- package/__cjs/react/internal/cache/SuspenseCache.cjs.map +1 -1
- package/__cjs/react/internal/cache/SuspenseCache.d.cts +1 -1
- package/__cjs/react/internal/cache/types.d.cts +2 -2
- package/__cjs/utilities/DeepPartial.cjs.map +1 -1
- package/__cjs/utilities/DeepPartial.d.cts +1 -1
- package/__cjs/utilities/internal/combineLatestBatched.cjs +71 -0
- package/__cjs/utilities/internal/combineLatestBatched.cjs.map +1 -0
- package/__cjs/utilities/internal/combineLatestBatched.d.cts +14 -0
- package/__cjs/utilities/internal/index.cjs +4 -2
- package/__cjs/utilities/internal/index.cjs.map +1 -1
- package/__cjs/utilities/internal/index.d.cts +1 -0
- package/__cjs/version.cjs +1 -1
- package/cache/core/cache.d.ts +72 -23
- package/cache/core/cache.js +151 -53
- package/cache/core/cache.js.map +1 -1
- package/cache/inmemory/inMemoryCache.js +12 -1
- package/cache/inmemory/inMemoryCache.js.map +1 -1
- package/core/ApolloClient.d.ts +87 -2
- package/core/ApolloClient.js +24 -27
- package/core/ApolloClient.js.map +1 -1
- package/incremental/handlers/defer20220824.d.ts +0 -2
- package/incremental/handlers/defer20220824.js +6 -2
- package/incremental/handlers/defer20220824.js.map +1 -1
- package/incremental/handlers/graphql17Alpha9.js +9 -3
- package/incremental/handlers/graphql17Alpha9.js.map +1 -1
- package/incremental/handlers/notImplemented.js +1 -1
- package/incremental/handlers/notImplemented.js.map +1 -1
- package/invariantErrorCodes.js +2 -2
- package/package.json +1 -1
- package/react/hooks/useFragment.d.ts +65 -6
- package/react/hooks/useFragment.js +37 -84
- package/react/hooks/useFragment.js.map +1 -1
- package/react/hooks/useMutation.d.ts +13 -1
- package/react/hooks/useMutation.js +7 -1
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useSuspenseFragment.d.ts +33 -7
- package/react/hooks/useSuspenseFragment.js +14 -13
- package/react/hooks/useSuspenseFragment.js.map +1 -1
- package/react/hooks-compiled/useFragment.d.ts +65 -6
- package/react/hooks-compiled/useFragment.js +111 -85
- package/react/hooks-compiled/useFragment.js.map +1 -1
- package/react/hooks-compiled/useMutation.d.ts +13 -1
- package/react/hooks-compiled/useMutation.js +5 -1
- package/react/hooks-compiled/useMutation.js.map +1 -1
- package/react/hooks-compiled/useSuspenseFragment.d.ts +33 -7
- package/react/hooks-compiled/useSuspenseFragment.js +14 -13
- package/react/hooks-compiled/useSuspenseFragment.js.map +1 -1
- package/react/internal/cache/FragmentReference.d.ts +2 -4
- package/react/internal/cache/FragmentReference.js +3 -22
- package/react/internal/cache/FragmentReference.js.map +1 -1
- package/react/internal/cache/SuspenseCache.d.ts +1 -1
- package/react/internal/cache/SuspenseCache.js.map +1 -1
- package/react/internal/cache/types.d.ts +2 -2
- package/react/internal/cache/types.js.map +1 -1
- package/utilities/DeepPartial.d.ts +1 -1
- package/utilities/DeepPartial.js.map +1 -1
- package/utilities/internal/combineLatestBatched.d.ts +14 -0
- package/utilities/internal/combineLatestBatched.js +68 -0
- package/utilities/internal/combineLatestBatched.js.map +1 -0
- package/utilities/internal/index.d.ts +1 -0
- package/utilities/internal/index.js +1 -0
- package/utilities/internal/index.js.map +1 -1
- package/version.js +1 -1
|
@@ -198,7 +198,19 @@ export declare namespace useMutation {
|
|
|
198
198
|
variables: TVariables;
|
|
199
199
|
}
|
|
200
200
|
]) => Promise<ApolloClient.MutateResult<MaybeMasked<TData>>>;
|
|
201
|
-
type MutationFunctionOptions<TData = unknown, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache> = Options<TData, TVariables, TCache
|
|
201
|
+
type MutationFunctionOptions<TData = unknown, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache> = Options<TData, TVariables, TCache> & {
|
|
202
|
+
/**
|
|
203
|
+
* 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.
|
|
204
|
+
*
|
|
205
|
+
* @docGroup 2. Networking options
|
|
206
|
+
*
|
|
207
|
+
*
|
|
208
|
+
* @remarks
|
|
209
|
+
* When provided as a callback function, the function is called with the
|
|
210
|
+
* value of `context` provided to the `useMutation` hook.
|
|
211
|
+
*/
|
|
212
|
+
context?: DefaultContext | ((hookContext: DefaultContext | undefined) => DefaultContext);
|
|
213
|
+
};
|
|
202
214
|
namespace DocumentationTypes {
|
|
203
215
|
/**
|
|
204
216
|
* > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.
|
|
@@ -67,6 +67,9 @@ export function useMutation(mutation, options) {
|
|
|
67
67
|
const { options, mutation } = ref.current;
|
|
68
68
|
const baseOptions = { ...options, mutation };
|
|
69
69
|
const client = executeOptions.client || ref.current.client;
|
|
70
|
+
const context = typeof executeOptions.context === "function" ?
|
|
71
|
+
executeOptions.context(options?.context)
|
|
72
|
+
: executeOptions.context;
|
|
70
73
|
if (!ref.current.result.loading && ref.current.isMounted) {
|
|
71
74
|
setResult((ref.current.result = {
|
|
72
75
|
loading: true,
|
|
@@ -77,7 +80,10 @@ export function useMutation(mutation, options) {
|
|
|
77
80
|
}));
|
|
78
81
|
}
|
|
79
82
|
const mutationId = ++ref.current.mutationId;
|
|
80
|
-
const clientOptions = mergeOptions(baseOptions,
|
|
83
|
+
const clientOptions = mergeOptions(baseOptions, {
|
|
84
|
+
...executeOptions,
|
|
85
|
+
context,
|
|
86
|
+
});
|
|
81
87
|
return preventUnhandledRejection(client
|
|
82
88
|
.mutate(clientOptions)
|
|
83
89
|
.then((response) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAqB/B,OAAO,EACL,YAAY,EACZ,yBAAyB,GAC1B,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACpF,OAAO,EAAE,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,CAAC,SAAS,GAAG,IAAI,CAAC;QAEzB,OAAO,GAAG,EAAE;YACV,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAc,EAAE,EAAE,KAAK,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,mBAAmB,CAAC,MAAoB;IAC/C,OAAO;QACL,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,SAAS;QAChB,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport { equal } from \"@wry/equality\";\nimport * as React from \"react\";\n\nimport type {\n ApolloCache,\n ApolloClient,\n DefaultContext,\n DocumentNode,\n ErrorLike,\n ErrorPolicy,\n InternalRefetchQueriesInclude,\n MaybeMasked,\n MutationFetchPolicy,\n MutationQueryReducersMap,\n MutationUpdaterFunction,\n NormalizedExecutionResult,\n OnQueryUpdated,\n OperationVariables,\n Unmasked,\n} from \"@apollo/client\";\nimport type { IgnoreModifier } from \"@apollo/client/cache\";\nimport type { NoInfer, Prettify } from \"@apollo/client/utilities/internal\";\nimport {\n mergeOptions,\n preventUnhandledRejection,\n} from \"@apollo/client/utilities/internal\";\n\nimport { useIsomorphicLayoutEffect } from \"./internal/useIsomorphicLayoutEffect.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\ntype MakeRequiredVariablesOptional<\n TVariables extends OperationVariables,\n TConfiguredVariables extends Partial<TVariables>,\n> = Prettify<\n {\n [K in keyof TVariables as K extends keyof TConfiguredVariables ? K\n : never]?: TVariables[K];\n } & Omit<TVariables, keyof TConfiguredVariables>\n>;\n\nexport declare namespace useMutation {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n TConfiguredVariables extends Partial<TVariables> = Partial<TVariables>,\n > {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#optimisticResponse:member} */\n optimisticResponse?:\n | Unmasked<NoInfer<TData>>\n | ((\n vars: TVariables,\n { IGNORE }: { IGNORE: IgnoreModifier }\n ) => Unmasked<NoInfer<TData>> | IgnoreModifier);\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#updateQueries:member} */\n updateQueries?: MutationQueryReducersMap<TData>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#refetchQueries:member} */\n refetchQueries?:\n | ((\n result: NormalizedExecutionResult<Unmasked<TData>>\n ) => InternalRefetchQueriesInclude)\n | InternalRefetchQueriesInclude;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#awaitRefetchQueries:member} */\n awaitRefetchQueries?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#update:member} */\n update?: MutationUpdaterFunction<TData, TVariables, TCache>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onQueryUpdated:member} */\n onQueryUpdated?: OnQueryUpdated<any>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#errorPolicy:member} */\n errorPolicy?: ErrorPolicy;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables?: Partial<TVariables> & TConfiguredVariables;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#context:member} */\n context?: DefaultContext;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#fetchPolicy:member} */\n fetchPolicy?: MutationFetchPolicy;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#keepRootFields:member} */\n keepRootFields?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#client:member} */\n client?: ApolloClient;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#notifyOnNetworkStatusChange:member} */\n notifyOnNetworkStatusChange?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onCompleted:member} */\n onCompleted?: (\n data: MaybeMasked<TData>,\n clientOptions?: Options<TData, TVariables, TCache>\n ) => void;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onError:member} */\n onError?: (\n error: ErrorLike,\n clientOptions?: Options<TData, TVariables, TCache>\n ) => void;\n }\n\n export interface Result<TData = unknown> {\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#data:member} */\n data: MaybeMasked<TData> | null | undefined;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#error:member} */\n error: ErrorLike | undefined;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#loading:member} */\n loading: boolean;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#called:member} */\n called: boolean;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#client:member} */\n client: ApolloClient;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#reset:member} */\n reset: () => void;\n }\n\n export type ResultTuple<\n TData,\n TVariables extends OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = [\n mutate: MutationFunction<TData, TVariables, TCache>,\n result: Result<TData>,\n ];\n\n export type MutationFunction<\n TData,\n TVariables extends OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = (\n ...[options]: {} extends TVariables ?\n [\n options?: MutationFunctionOptions<TData, TVariables, TCache> & {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables?: TVariables;\n },\n ]\n : [\n options: MutationFunctionOptions<TData, TVariables, TCache> & {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables: TVariables;\n },\n ]\n ) => Promise<ApolloClient.MutateResult<MaybeMasked<TData>>>;\n\n export type MutationFunctionOptions<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = Options<TData, TVariables, TCache>;\n\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useMutation:function(1)} */\n export function useMutation<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: useMutation.Options<TData, TVariables>\n ): useMutation.ResultTuple<TData, TVariables>;\n }\n}\n\n/**\n * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.\n *\n * @example\n *\n * ```jsx\n * import { gql, useMutation } from \"@apollo/client\";\n *\n * const ADD_TODO = gql`\n * mutation AddTodo($type: String!) {\n * addTodo(type: $type) {\n * id\n * type\n * }\n * }\n * `;\n *\n * function AddTodo() {\n * let input;\n * const [addTodo, { data }] = useMutation(ADD_TODO);\n *\n * return (\n * <div>\n * <form\n * onSubmit={(e) => {\n * e.preventDefault();\n * addTodo({ variables: { type: input.value } });\n * input.value = \"\";\n * }}\n * >\n * <input\n * ref={(node) => {\n * input = node;\n * }}\n * />\n * <button type=\"submit\">Add Todo</button>\n * </form>\n * </div>\n * );\n * }\n * ```\n *\n * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.\n * @param options - Options to control how the mutation is executed.\n * @returns A tuple in the form of `[mutate, result]`\n */\nexport function useMutation<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n TConfiguredVariables extends Partial<TVariables> = {},\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: useMutation.Options<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TCache,\n {\n [K in keyof TConfiguredVariables]: K extends keyof TVariables ?\n TConfiguredVariables[K]\n : never;\n }\n >\n): useMutation.ResultTuple<\n TData,\n MakeRequiredVariablesOptional<TVariables, TConfiguredVariables>,\n TCache\n> {\n const client = useApolloClient(options?.client);\n const [result, setResult] = React.useState<\n Omit<useMutation.Result<TData>, \"reset\">\n >(() => createInitialResult(client));\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n useIsomorphicLayoutEffect(() => {\n Object.assign(ref.current, { client, options, mutation });\n });\n\n const execute = React.useCallback(\n (\n executeOptions: useMutation.MutationFunctionOptions<\n TData,\n TVariables,\n TCache\n > = {} as useMutation.MutationFunctionOptions<TData, TVariables, TCache>\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n\n if (!ref.current.result.loading && ref.current.isMounted) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: undefined,\n data: undefined,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, executeOptions as any);\n\n return preventUnhandledRejection(\n client\n .mutate(\n clientOptions as ApolloClient.MutateOptions<\n TData,\n OperationVariables\n >\n )\n .then(\n (response) => {\n const { data, error } = response;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(error, clientOptions);\n }\n\n if (mutationId === ref.current.mutationId) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (\n ref.current.isMounted &&\n !equal(ref.current.result, result)\n ) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(response.data!, clientOptions);\n }\n\n return response;\n },\n (error) => {\n if (\n mutationId === ref.current.mutationId &&\n ref.current.isMounted\n ) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(error, clientOptions);\n }\n\n throw error;\n }\n )\n );\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n const result = createInitialResult(ref.current.client);\n Object.assign(ref.current, { mutationId: 0, result });\n setResult(result);\n }\n }, []);\n\n React.useEffect(() => {\n const current = ref.current;\n current.isMounted = true;\n\n return () => {\n current.isMounted = false;\n };\n }, []);\n\n return [execute as any, { reset, ...result }];\n}\n\nfunction createInitialResult(client: ApolloClient) {\n return {\n data: undefined,\n error: undefined,\n called: false,\n loading: false,\n client,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAqB/B,OAAO,EACL,YAAY,EACZ,yBAAyB,GAC1B,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACpF,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AA8JvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OASC;IAMD,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAChD,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,KAAK,CAAC,QAAQ,CAExC,GAAG,EAAE,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;IAErC,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC;QACvB,MAAM;QACN,UAAU,EAAE,CAAC;QACb,SAAS,EAAE,IAAI;QACf,MAAM;QACN,QAAQ;QACR,OAAO;KACR,CAAC,CAAC;IAEH,yBAAyB,CAAC,GAAG,EAAE;QAC7B,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,KAAK,CAAC,WAAW,CAC/B,CACE,iBAII,EAAoE,EACxE,EAAE;QACF,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,GAAG,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AO,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;YAChC,CAAC;YAED,MAAM,KAAK,CAAC;QACd,CAAC,CACF,CACJ,CAAC;IACJ,CAAC,EACD,EAAE,CACH,CAAC;IAEF,MAAM,KAAK,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;QACnC,IAAI,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC1B,MAAM,MAAM,GAAG,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACvD,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;YACtD,SAAS,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;QAC5B,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QAEzB,OAAO,GAAG,EAAE;YACV,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAc,EAAE,EAAE,KAAK,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,mBAAmB,CAAC,MAAoB;IAC/C,OAAO;QACL,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,SAAS;QAChB,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport { equal } from \"@wry/equality\";\nimport * as React from \"react\";\n\nimport type {\n ApolloCache,\n ApolloClient,\n DefaultContext,\n DocumentNode,\n ErrorLike,\n ErrorPolicy,\n InternalRefetchQueriesInclude,\n MaybeMasked,\n MutationFetchPolicy,\n MutationQueryReducersMap,\n MutationUpdaterFunction,\n NormalizedExecutionResult,\n OnQueryUpdated,\n OperationVariables,\n Unmasked,\n} from \"@apollo/client\";\nimport type { IgnoreModifier } from \"@apollo/client/cache\";\nimport type { NoInfer, Prettify } from \"@apollo/client/utilities/internal\";\nimport {\n mergeOptions,\n preventUnhandledRejection,\n} from \"@apollo/client/utilities/internal\";\n\nimport { useIsomorphicLayoutEffect } from \"./internal/useIsomorphicLayoutEffect.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\ntype MakeRequiredVariablesOptional<\n TVariables extends OperationVariables,\n TConfiguredVariables extends Partial<TVariables>,\n> = Prettify<\n {\n [K in keyof TVariables as K extends keyof TConfiguredVariables ? K\n : never]?: TVariables[K];\n } & Omit<TVariables, keyof TConfiguredVariables>\n>;\n\nexport declare namespace useMutation {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n TConfiguredVariables extends Partial<TVariables> = Partial<TVariables>,\n > {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#optimisticResponse:member} */\n optimisticResponse?:\n | Unmasked<NoInfer<TData>>\n | ((\n vars: TVariables,\n { IGNORE }: { IGNORE: IgnoreModifier }\n ) => Unmasked<NoInfer<TData>> | IgnoreModifier);\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#updateQueries:member} */\n updateQueries?: MutationQueryReducersMap<TData>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#refetchQueries:member} */\n refetchQueries?:\n | ((\n result: NormalizedExecutionResult<Unmasked<TData>>\n ) => InternalRefetchQueriesInclude)\n | InternalRefetchQueriesInclude;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#awaitRefetchQueries:member} */\n awaitRefetchQueries?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#update:member} */\n update?: MutationUpdaterFunction<TData, TVariables, TCache>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onQueryUpdated:member} */\n onQueryUpdated?: OnQueryUpdated<any>;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#errorPolicy:member} */\n errorPolicy?: ErrorPolicy;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables?: Partial<TVariables> & TConfiguredVariables;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#context:member} */\n context?: DefaultContext;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#fetchPolicy:member} */\n fetchPolicy?: MutationFetchPolicy;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#keepRootFields:member} */\n keepRootFields?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#client:member} */\n client?: ApolloClient;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#notifyOnNetworkStatusChange:member} */\n notifyOnNetworkStatusChange?: boolean;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onCompleted:member} */\n onCompleted?: (\n data: MaybeMasked<TData>,\n clientOptions?: Options<TData, TVariables, TCache>\n ) => void;\n\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#onError:member} */\n onError?: (\n error: ErrorLike,\n clientOptions?: Options<TData, TVariables, TCache>\n ) => void;\n }\n\n export interface Result<TData = unknown> {\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#data:member} */\n data: MaybeMasked<TData> | null | undefined;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#error:member} */\n error: ErrorLike | undefined;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#loading:member} */\n loading: boolean;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#called:member} */\n called: boolean;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#client:member} */\n client: ApolloClient;\n\n /** {@inheritDoc @apollo/client!MutationResultDocumentation#reset:member} */\n reset: () => void;\n }\n\n export type ResultTuple<\n TData,\n TVariables extends OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = [\n mutate: MutationFunction<TData, TVariables, TCache>,\n result: Result<TData>,\n ];\n\n export type MutationFunction<\n TData,\n TVariables extends OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = (\n ...[options]: {} extends TVariables ?\n [\n options?: MutationFunctionOptions<TData, TVariables, TCache> & {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables?: TVariables;\n },\n ]\n : [\n options: MutationFunctionOptions<TData, TVariables, TCache> & {\n /** {@inheritDoc @apollo/client!MutationOptionsDocumentation#variables:member} */\n variables: TVariables;\n },\n ]\n ) => Promise<ApolloClient.MutateResult<MaybeMasked<TData>>>;\n\n export type MutationFunctionOptions<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n > = Options<TData, TVariables, TCache> & {\n /**\n * {@inheritDoc @apollo/client!MutationOptionsDocumentation#context:member}\n *\n * @remarks\n * When provided as a callback function, the function is called with the\n * value of `context` provided to the `useMutation` hook.\n */\n context?:\n | DefaultContext\n | ((hookContext: DefaultContext | undefined) => DefaultContext);\n };\n\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useMutation:function(1)} */\n export function useMutation<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: useMutation.Options<TData, TVariables>\n ): useMutation.ResultTuple<TData, TVariables>;\n }\n}\n\n/**\n * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.\n *\n * @example\n *\n * ```jsx\n * import { gql, useMutation } from \"@apollo/client\";\n *\n * const ADD_TODO = gql`\n * mutation AddTodo($type: String!) {\n * addTodo(type: $type) {\n * id\n * type\n * }\n * }\n * `;\n *\n * function AddTodo() {\n * let input;\n * const [addTodo, { data }] = useMutation(ADD_TODO);\n *\n * return (\n * <div>\n * <form\n * onSubmit={(e) => {\n * e.preventDefault();\n * addTodo({ variables: { type: input.value } });\n * input.value = \"\";\n * }}\n * >\n * <input\n * ref={(node) => {\n * input = node;\n * }}\n * />\n * <button type=\"submit\">Add Todo</button>\n * </form>\n * </div>\n * );\n * }\n * ```\n *\n * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.\n * @param options - Options to control how the mutation is executed.\n * @returns A tuple in the form of `[mutate, result]`\n */\nexport function useMutation<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n TCache extends ApolloCache = ApolloCache,\n TConfiguredVariables extends Partial<TVariables> = {},\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: useMutation.Options<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TCache,\n {\n [K in keyof TConfiguredVariables]: K extends keyof TVariables ?\n TConfiguredVariables[K]\n : never;\n }\n >\n): useMutation.ResultTuple<\n TData,\n MakeRequiredVariablesOptional<TVariables, TConfiguredVariables>,\n TCache\n> {\n const client = useApolloClient(options?.client);\n const [result, setResult] = React.useState<\n Omit<useMutation.Result<TData>, \"reset\">\n >(() => createInitialResult(client));\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n useIsomorphicLayoutEffect(() => {\n Object.assign(ref.current, { client, options, mutation });\n });\n\n const execute = React.useCallback(\n (\n executeOptions: useMutation.MutationFunctionOptions<\n TData,\n TVariables,\n TCache\n > = {} as useMutation.MutationFunctionOptions<TData, TVariables, TCache>\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n const context =\n typeof executeOptions.context === \"function\" ?\n executeOptions.context(options?.context)\n : executeOptions.context;\n\n if (!ref.current.result.loading && ref.current.isMounted) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: undefined,\n data: undefined,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, {\n ...executeOptions,\n context,\n } as any);\n\n return preventUnhandledRejection(\n client\n .mutate(\n clientOptions as ApolloClient.MutateOptions<\n TData,\n OperationVariables\n >\n )\n .then(\n (response) => {\n const { data, error } = response;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(error, clientOptions);\n }\n\n if (mutationId === ref.current.mutationId) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (\n ref.current.isMounted &&\n !equal(ref.current.result, result)\n ) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(response.data!, clientOptions);\n }\n\n return response;\n },\n (error) => {\n if (\n mutationId === ref.current.mutationId &&\n ref.current.isMounted\n ) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(error, clientOptions);\n }\n\n throw error;\n }\n )\n );\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n const result = createInitialResult(ref.current.client);\n Object.assign(ref.current, { mutationId: 0, result });\n setResult(result);\n }\n }, []);\n\n React.useEffect(() => {\n const current = ref.current;\n current.isMounted = true;\n\n return () => {\n current.isMounted = false;\n };\n }, []);\n\n return [execute as any, { reset, ...result }];\n}\n\nfunction createInitialResult(client: ApolloClient) {\n return {\n data: undefined,\n error: undefined,\n called: false,\n loading: false,\n client,\n };\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ApolloClient, DataValue, DocumentNode, OperationVariables,
|
|
2
|
-
import type {
|
|
1
|
+
import type { ApolloClient, DataValue, DocumentNode, OperationVariables, TypedDocumentNode } from "@apollo/client";
|
|
2
|
+
import type { ApolloCache } from "@apollo/client/cache";
|
|
3
|
+
import type { MaybeMasked } from "@apollo/client/masking";
|
|
3
4
|
import type { DocumentationTypes as UtilityDocumentationTypes, NoInfer, VariablesOption } from "@apollo/client/utilities/internal";
|
|
4
|
-
type From<TData> = StoreObject | Reference | FragmentType<NoInfer<TData>> | string | null;
|
|
5
5
|
export declare namespace useSuspenseFragment {
|
|
6
6
|
import _self = useSuspenseFragment;
|
|
7
7
|
namespace Base {
|
|
@@ -19,7 +19,12 @@ export declare namespace useSuspenseFragment {
|
|
|
19
19
|
* `fragment` document then that fragment will be used.
|
|
20
20
|
*/
|
|
21
21
|
fragmentName?: string;
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* An object or array containing a `__typename` and primary key fields
|
|
24
|
+
* (such as `id`) identifying the entity object from which the fragment will
|
|
25
|
+
* be retrieved, or a `{ __ref: "..." }` reference, or a `string` ID (uncommon).
|
|
26
|
+
*/
|
|
27
|
+
from: useSuspenseFragment.FromValue<TData> | Array<useSuspenseFragment.FromValue<TData>>;
|
|
23
28
|
optimistic?: boolean;
|
|
24
29
|
/**
|
|
25
30
|
* The instance of `ApolloClient` to use to look up the fragment.
|
|
@@ -39,6 +44,10 @@ export declare namespace useSuspenseFragment {
|
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Acceptable values provided to the `from` option.
|
|
49
|
+
*/
|
|
50
|
+
type FromValue<TData> = ApolloCache.WatchFragmentFromValue<TData>;
|
|
42
51
|
interface Result<TData> {
|
|
43
52
|
data: DataValue.Complete<MaybeMasked<TData>>;
|
|
44
53
|
}
|
|
@@ -56,8 +65,26 @@ export declare namespace useSuspenseFragment {
|
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
67
|
/** #TODO documentation */
|
|
68
|
+
export declare function useSuspenseFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables> & {
|
|
69
|
+
from: Array<NonNullable<useSuspenseFragment.FromValue<TData>>>;
|
|
70
|
+
}): useSuspenseFragment.Result<Array<TData>>;
|
|
71
|
+
/**
|
|
72
|
+
* #TODO documentation
|
|
73
|
+
*/
|
|
74
|
+
export declare function useSuspenseFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables> & {
|
|
75
|
+
from: Array<null>;
|
|
76
|
+
}): useSuspenseFragment.Result<Array<null>>;
|
|
77
|
+
/**
|
|
78
|
+
* #TODO documentation
|
|
79
|
+
*/
|
|
80
|
+
export declare function useSuspenseFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables> & {
|
|
81
|
+
from: Array<useSuspenseFragment.FromValue<TData>>;
|
|
82
|
+
}): useSuspenseFragment.Result<Array<TData | null>>;
|
|
83
|
+
/**
|
|
84
|
+
* #TODO documentation
|
|
85
|
+
*/
|
|
59
86
|
export declare function useSuspenseFragment<TData, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables> & {
|
|
60
|
-
from: NonNullable<
|
|
87
|
+
from: NonNullable<useSuspenseFragment.FromValue<TData>>;
|
|
61
88
|
}): useSuspenseFragment.Result<TData>;
|
|
62
89
|
/**
|
|
63
90
|
* #TODO documentation
|
|
@@ -69,11 +96,10 @@ export declare function useSuspenseFragment<TData, TVariables extends OperationV
|
|
|
69
96
|
* #TODO documentation
|
|
70
97
|
*/
|
|
71
98
|
export declare function useSuspenseFragment<TData, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables> & {
|
|
72
|
-
from:
|
|
99
|
+
from: useSuspenseFragment.FromValue<TData>;
|
|
73
100
|
}): useSuspenseFragment.Result<TData | null>;
|
|
74
101
|
/**
|
|
75
102
|
* #TODO documentation
|
|
76
103
|
*/
|
|
77
104
|
export declare function useSuspenseFragment<TData, TVariables extends OperationVariables = OperationVariables>(options: useSuspenseFragment.Options<TData, TVariables>): useSuspenseFragment.Result<TData>;
|
|
78
|
-
export {};
|
|
79
105
|
//# sourceMappingURL=useSuspenseFragment.d.ts.map
|
|
@@ -2,9 +2,8 @@ import * as React from "react";
|
|
|
2
2
|
import { canonicalStringify } from "@apollo/client/cache";
|
|
3
3
|
import { getSuspenseCache } from "@apollo/client/react/internal";
|
|
4
4
|
import { __use } from "./internal/__use.js";
|
|
5
|
-
import { wrapHook } from "./internal/index.js";
|
|
5
|
+
import { useDeepMemo, wrapHook } from "./internal/index.js";
|
|
6
6
|
import { useApolloClient } from "./useApolloClient.js";
|
|
7
|
-
const NULL_PLACEHOLDER = [];
|
|
8
7
|
export function useSuspenseFragment(options) {
|
|
9
8
|
"use no memo";
|
|
10
9
|
return wrapHook("useSuspenseFragment",
|
|
@@ -15,15 +14,15 @@ function useSuspenseFragment_(options) {
|
|
|
15
14
|
const client = useApolloClient(options.client);
|
|
16
15
|
const { from, variables } = options;
|
|
17
16
|
const { cache } = client;
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
const ids = useDeepMemo(() => {
|
|
18
|
+
return Array.isArray(from) ?
|
|
19
|
+
from.map((id) => toStringId(cache, id))
|
|
20
|
+
: toStringId(cache, from);
|
|
21
|
+
}, [cache, from]);
|
|
22
|
+
const idString = React.useMemo(() => (Array.isArray(ids) ? ids.join(",") : ids), [ids]);
|
|
23
|
+
const fragmentRef = getSuspenseCache(client).getFragmentRef([options.fragment, canonicalStringify(variables), idString], client, { ...options, variables: variables, from: ids });
|
|
24
|
+
let [current, setPromise] = React.useState([fragmentRef.key, fragmentRef.promise]);
|
|
23
25
|
React.useEffect(() => {
|
|
24
|
-
if (fragmentRef === null) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
26
|
const dispose = fragmentRef.retain();
|
|
28
27
|
const removeListener = fragmentRef.listen((promise) => {
|
|
29
28
|
setPromise([fragmentRef.key, promise]);
|
|
@@ -33,9 +32,6 @@ function useSuspenseFragment_(options) {
|
|
|
33
32
|
removeListener();
|
|
34
33
|
};
|
|
35
34
|
}, [fragmentRef]);
|
|
36
|
-
if (fragmentRef === null) {
|
|
37
|
-
return { data: null };
|
|
38
|
-
}
|
|
39
35
|
if (current[0] !== fragmentRef.key) {
|
|
40
36
|
// eslint-disable-next-line react-compiler/react-compiler
|
|
41
37
|
current[0] = fragmentRef.key;
|
|
@@ -44,4 +40,9 @@ function useSuspenseFragment_(options) {
|
|
|
44
40
|
const data = __use(current[1]);
|
|
45
41
|
return { data };
|
|
46
42
|
}
|
|
43
|
+
function toStringId(cache, from) {
|
|
44
|
+
return (typeof from === "string" ? from
|
|
45
|
+
: from === null ? null
|
|
46
|
+
: cache.identify(from));
|
|
47
|
+
}
|
|
47
48
|
//# sourceMappingURL=useSuspenseFragment.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSuspenseFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useSuspenseFragment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAW/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAOjE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AA4EvD,MAAM,gBAAgB,GAAG,EAGxB,CAAC;AAwCF,MAAM,UAAU,mBAAmB,CAIjC,OAAuD;IAEvD,aAAa,CAAC;IACd,OAAO,QAAQ,CACb,qBAAqB;IACrB,yDAAyD;IACzD,oBAAoB,EACpB,eAAe,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAC1E,CAAC,OAAO,CAAC,CAAC;AACb,CAAC;AAED,SAAS,oBAAoB,CAI3B,OAAuD;IAEvD,MAAM,MAAM,GAAG,eAA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AG,EAAE,OAAO,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,OAAO,EAAE,CAAC;YACV,cAAc,EAAE,CAAC;QACnB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;IAElB,IAAI,WAAW,KAAK,IAAI,EAAE,CAAC;QACzB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;IAED,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,WAAW,CAAC,GAAG,EAAE,CAAC;QACnC,yDAAyD;QACzD,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;QAC7B,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC;IACnC,CAAC;IAED,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/B,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import * as React from \"react\";\n\nimport type {\n ApolloClient,\n DataValue,\n DocumentNode,\n OperationVariables,\n Reference,\n StoreObject,\n TypedDocumentNode,\n} from \"@apollo/client\";\nimport { canonicalStringify } from \"@apollo/client/cache\";\nimport type { FragmentType, MaybeMasked } from \"@apollo/client/masking\";\nimport type { FragmentKey } from \"@apollo/client/react/internal\";\nimport { getSuspenseCache } from \"@apollo/client/react/internal\";\nimport type {\n DocumentationTypes as UtilityDocumentationTypes,\n NoInfer,\n VariablesOption,\n} from \"@apollo/client/utilities/internal\";\n\nimport { __use } from \"./internal/__use.js\";\nimport { wrapHook } from \"./internal/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\ntype From<TData> =\n | StoreObject\n | Reference\n | FragmentType<NoInfer<TData>>\n | string\n | null;\n\nexport declare namespace useSuspenseFragment {\n import _self = useSuspenseFragment;\n export namespace Base {\n export type Options<TData, TVariables extends OperationVariables> = {\n /**\n * A GraphQL document created using the `gql` template string tag from\n * `graphql-tag` with one or more fragments which will be used to determine\n * the shape of data to read. If you provide more than one fragment in this\n * document then you must also specify `fragmentName` to select a single.\n */\n fragment: DocumentNode | TypedDocumentNode<TData, TVariables>;\n\n /**\n * The name of the fragment in your GraphQL document to be used. If you do\n * not provide a `fragmentName` and there is only one fragment in your\n * `fragment` document then that fragment will be used.\n */\n fragmentName?: string;\n from: From<TData>;\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n /**\n * The instance of `ApolloClient` to use to look up the fragment.\n *\n * By default, the instance that's passed down via context is used, but you\n * can provide a different instance here.\n *\n * @docGroup 1. Operation options\n */\n client?: ApolloClient;\n };\n }\n export type Options<\n TData,\n TVariables extends OperationVariables,\n > = Base.Options<TData, TVariables> & VariablesOption<NoInfer<TVariables>>;\n\n export namespace DocumentationTypes {\n export namespace useSuspenseFragment {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n > extends Base.Options<TData, TVariables>,\n UtilityDocumentationTypes.VariableOptions<TVariables> {}\n }\n }\n\n export interface Result<TData> {\n data: DataValue.Complete<MaybeMasked<TData>>;\n }\n export namespace DocumentationTypes {\n export namespace useSuspenseFragment {\n export interface Result<TData = unknown> extends _self.Result<TData> {}\n }\n }\n\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\n export function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n >(\n options: useSuspenseFragment.Options<TData, TVariables>\n ): useSuspenseFragment.Result<TData>;\n }\n}\n\nconst NULL_PLACEHOLDER = [] as unknown as [\n FragmentKey,\n Promise<MaybeMasked<any> | null>,\n];\n\n/** #TODO documentation */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: NonNullable<From<TData>>;\n }\n): useSuspenseFragment.Result<TData>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: null;\n }\n): useSuspenseFragment.Result<null>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: From<TData>;\n }\n): useSuspenseFragment.Result<TData | null>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData>;\n\nexport function useSuspenseFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData | null> {\n \"use no memo\";\n return wrapHook(\n \"useSuspenseFragment\",\n // eslint-disable-next-line react-compiler/react-compiler\n useSuspenseFragment_,\n useApolloClient(typeof options === \"object\" ? options.client : undefined)\n )(options);\n}\n\nfunction useSuspenseFragment_<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData | null> {\n const client = useApolloClient(options.client);\n const { from, variables } = options;\n const { cache } = client;\n\n const id = React.useMemo(\n () =>\n typeof from === \"string\" ? from\n : from === null ? null\n : cache.identify(from),\n [cache, from]\n ) as string | null;\n\n const fragmentRef =\n id === null ? null : (\n getSuspenseCache(client).getFragmentRef(\n [id, options.fragment, canonicalStringify(variables)],\n client,\n { ...options, variables: variables as TVariables, from: id }\n )\n );\n\n let [current, setPromise] = React.useState<\n [FragmentKey, Promise<MaybeMasked<TData> | null>]\n >(\n fragmentRef === null ? NULL_PLACEHOLDER : (\n [fragmentRef.key, fragmentRef.promise]\n )\n );\n\n React.useEffect(() => {\n if (fragmentRef === null) {\n return;\n }\n\n const dispose = fragmentRef.retain();\n const removeListener = fragmentRef.listen((promise) => {\n setPromise([fragmentRef.key, promise]);\n });\n\n return () => {\n dispose();\n removeListener();\n };\n }, [fragmentRef]);\n\n if (fragmentRef === null) {\n return { data: null };\n }\n\n if (current[0] !== fragmentRef.key) {\n // eslint-disable-next-line react-compiler/react-compiler\n current[0] = fragmentRef.key;\n current[1] = fragmentRef.promise;\n }\n\n const data = __use(current[1]);\n\n return { data };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useSuspenseFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useSuspenseFragment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAU/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAOjE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAuJvD,MAAM,UAAU,mBAAmB,CAIjC,OAAuD;IAEvD,aAAa,CAAC;IACd,OAAO,QAAQ,CACb,qBAAqB;IACrB,yDAAyD;IACzD,oBAAoB,EACpB,eAAe,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAC1E,CAAC,OAAO,CAAC,CAAC;AACb,CAAC;AAED,SAAS,oBAAoB,CAI3B,OAAuD;IAEvD,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IACpC,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IAEzB,MAAM,GAAG,G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O,EAAE,CAAC;YACV,cAAc,EAAE,CAAC;QACnB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;IAElB,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,WAAW,CAAC,GAAG,EAAE,CAAC;QACnC,yDAAyD;QACzD,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;QAC7B,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC;IACnC,CAAC;IAED,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/B,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC;AAED,SAAS,UAAU,CACjB,KAAkB,EAClB,IAAwC;IAExC,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI;QAC/B,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI;YACtB,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAkB,CAAC;AAC7C,CAAC","sourcesContent":["import * as React from \"react\";\n\nimport type {\n ApolloClient,\n DataValue,\n DocumentNode,\n OperationVariables,\n TypedDocumentNode,\n} from \"@apollo/client\";\nimport type { ApolloCache } from \"@apollo/client/cache\";\nimport { canonicalStringify } from \"@apollo/client/cache\";\nimport type { MaybeMasked } from \"@apollo/client/masking\";\nimport type { FragmentKey } from \"@apollo/client/react/internal\";\nimport { getSuspenseCache } from \"@apollo/client/react/internal\";\nimport type {\n DocumentationTypes as UtilityDocumentationTypes,\n NoInfer,\n VariablesOption,\n} from \"@apollo/client/utilities/internal\";\n\nimport { __use } from \"./internal/__use.js\";\nimport { useDeepMemo, wrapHook } from \"./internal/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\nexport declare namespace useSuspenseFragment {\n import _self = useSuspenseFragment;\n export namespace Base {\n export type Options<TData, TVariables extends OperationVariables> = {\n /**\n * A GraphQL document created using the `gql` template string tag from\n * `graphql-tag` with one or more fragments which will be used to determine\n * the shape of data to read. If you provide more than one fragment in this\n * document then you must also specify `fragmentName` to select a single.\n */\n fragment: DocumentNode | TypedDocumentNode<TData, TVariables>;\n\n /**\n * The name of the fragment in your GraphQL document to be used. If you do\n * not provide a `fragmentName` and there is only one fragment in your\n * `fragment` document then that fragment will be used.\n */\n fragmentName?: string;\n\n /**\n * An object or array containing a `__typename` and primary key fields\n * (such as `id`) identifying the entity object from which the fragment will\n * be retrieved, or a `{ __ref: \"...\" }` reference, or a `string` ID (uncommon).\n */\n from:\n | useSuspenseFragment.FromValue<TData>\n | Array<useSuspenseFragment.FromValue<TData>>;\n\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n /**\n * The instance of `ApolloClient` to use to look up the fragment.\n *\n * By default, the instance that's passed down via context is used, but you\n * can provide a different instance here.\n *\n * @docGroup 1. Operation options\n */\n client?: ApolloClient;\n };\n }\n export type Options<\n TData,\n TVariables extends OperationVariables,\n > = Base.Options<TData, TVariables> & VariablesOption<NoInfer<TVariables>>;\n\n export namespace DocumentationTypes {\n export namespace useSuspenseFragment {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n > extends Base.Options<TData, TVariables>,\n UtilityDocumentationTypes.VariableOptions<TVariables> {}\n }\n }\n\n /**\n * Acceptable values provided to the `from` option.\n */\n export type FromValue<TData> = ApolloCache.WatchFragmentFromValue<TData>;\n\n export interface Result<TData> {\n data: DataValue.Complete<MaybeMasked<TData>>;\n }\n export namespace DocumentationTypes {\n export namespace useSuspenseFragment {\n export interface Result<TData = unknown> extends _self.Result<TData> {}\n }\n }\n\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\n export function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n >(\n options: useSuspenseFragment.Options<TData, TVariables>\n ): useSuspenseFragment.Result<TData>;\n }\n}\n\n/** #TODO documentation */\nexport function useSuspenseFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: Array<NonNullable<useSuspenseFragment.FromValue<TData>>>;\n }\n): useSuspenseFragment.Result<Array<TData>>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: Array<null>;\n }\n): useSuspenseFragment.Result<Array<null>>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: Array<useSuspenseFragment.FromValue<TData>>;\n }\n): useSuspenseFragment.Result<Array<TData | null>>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: NonNullable<useSuspenseFragment.FromValue<TData>>;\n }\n): useSuspenseFragment.Result<TData>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: null;\n }\n): useSuspenseFragment.Result<null>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables> & {\n from: useSuspenseFragment.FromValue<TData>;\n }\n): useSuspenseFragment.Result<TData | null>;\n\n/** {@inheritDoc @apollo/client/react!useSuspenseFragment:function(1)} */\nexport function useSuspenseFragment<\n TData,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData>;\n\nexport function useSuspenseFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData | null> {\n \"use no memo\";\n return wrapHook(\n \"useSuspenseFragment\",\n // eslint-disable-next-line react-compiler/react-compiler\n useSuspenseFragment_,\n useApolloClient(typeof options === \"object\" ? options.client : undefined)\n )(options);\n}\n\nfunction useSuspenseFragment_<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n options: useSuspenseFragment.Options<TData, TVariables>\n): useSuspenseFragment.Result<TData | null> {\n const client = useApolloClient(options.client);\n const { from, variables } = options;\n const { cache } = client;\n\n const ids = useDeepMemo(() => {\n return Array.isArray(from) ?\n from.map((id) => toStringId(cache, id))\n : toStringId(cache, from);\n }, [cache, from]);\n const idString = React.useMemo(\n () => (Array.isArray(ids) ? ids.join(\",\") : ids),\n [ids]\n );\n\n const fragmentRef = getSuspenseCache(client).getFragmentRef(\n [options.fragment, canonicalStringify(variables), idString],\n client,\n { ...options, variables: variables as TVariables, from: ids }\n );\n\n let [current, setPromise] = React.useState<\n [FragmentKey, Promise<MaybeMasked<TData> | null>]\n >([fragmentRef.key, fragmentRef.promise]);\n\n React.useEffect(() => {\n const dispose = fragmentRef.retain();\n const removeListener = fragmentRef.listen((promise) => {\n setPromise([fragmentRef.key, promise]);\n });\n\n return () => {\n dispose();\n removeListener();\n };\n }, [fragmentRef]);\n\n if (current[0] !== fragmentRef.key) {\n // eslint-disable-next-line react-compiler/react-compiler\n current[0] = fragmentRef.key;\n current[1] = fragmentRef.promise;\n }\n\n const data = __use(current[1]);\n\n return { data };\n}\n\nfunction toStringId(\n cache: ApolloCache,\n from: useSuspenseFragment.FromValue<any>\n) {\n return (\n typeof from === \"string\" ? from\n : from === null ? null\n : cache.identify(from)) as string | null;\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ApolloClient, DataValue, DocumentNode, GetDataState, OperationVariables, TypedDocumentNode } from "@apollo/client";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { ApolloCache, MissingTree } from "@apollo/client/cache";
|
|
3
|
+
import type { MaybeMasked } from "@apollo/client/masking";
|
|
4
4
|
import type { NoInfer } from "@apollo/client/utilities/internal";
|
|
5
5
|
export declare namespace useFragment {
|
|
6
6
|
import _self = useFragment;
|
|
@@ -23,9 +23,11 @@ export declare namespace useFragment {
|
|
|
23
23
|
*/
|
|
24
24
|
variables?: NoInfer<TVariables>;
|
|
25
25
|
/**
|
|
26
|
-
* An object containing a `__typename` and primary key fields
|
|
26
|
+
* An object or array containing a `__typename` and primary key fields
|
|
27
|
+
* (such as `id`) identifying the entity object from which the fragment will
|
|
28
|
+
* be retrieved, or a `{ __ref: "..." }` reference, or a `string` ID (uncommon).
|
|
27
29
|
*/
|
|
28
|
-
from:
|
|
30
|
+
from: useFragment.FromValue<TData> | Array<useFragment.FromValue<TData>>;
|
|
29
31
|
/**
|
|
30
32
|
* Whether to read from optimistic or non-optimistic cache data. If
|
|
31
33
|
* this named option is provided, the optimistic parameter of the
|
|
@@ -50,6 +52,10 @@ export declare namespace useFragment {
|
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Acceptable values provided to the `from` option.
|
|
57
|
+
*/
|
|
58
|
+
type FromValue<TData> = ApolloCache.WatchFragmentFromValue<TData>;
|
|
53
59
|
type Result<TData> = ({
|
|
54
60
|
/**
|
|
55
61
|
*
|
|
@@ -59,7 +65,7 @@ export declare namespace useFragment {
|
|
|
59
65
|
* A tree of all `MissingFieldError` messages reported during fragment reading, where the branches of the tree indicate the paths of the errors within the query result.
|
|
60
66
|
*/
|
|
61
67
|
missing?: never;
|
|
62
|
-
} & GetDataState<MaybeMasked<TData>, "complete">) |
|
|
68
|
+
} & GetDataState<MaybeMasked<TData>, "complete">) | {
|
|
63
69
|
/**
|
|
64
70
|
*
|
|
65
71
|
*/
|
|
@@ -68,7 +74,30 @@ export declare namespace useFragment {
|
|
|
68
74
|
* A tree of all `MissingFieldError` messages reported during fragment reading, where the branches of the tree indicate the paths of the errors within the query result.
|
|
69
75
|
*/
|
|
70
76
|
missing?: MissingTree;
|
|
71
|
-
|
|
77
|
+
/**
|
|
78
|
+
* An object containing the result of your GraphQL query after it completes.
|
|
79
|
+
*
|
|
80
|
+
* This value might be `undefined` if a query results in one or more errors (depending on the query's `errorPolicy`).
|
|
81
|
+
*
|
|
82
|
+
* @docGroup 1. Operation data
|
|
83
|
+
*/
|
|
84
|
+
data: TData extends Array<infer TItem> ? Array<DataValue.Partial<TItem> | null> : DataValue.Partial<TData>;
|
|
85
|
+
/**
|
|
86
|
+
* Describes the completeness of `data`.
|
|
87
|
+
*
|
|
88
|
+
* - `empty`: No data could be fulfilled from the cache or the result is
|
|
89
|
+
* incomplete. `data` is `undefined`.
|
|
90
|
+
* - `partial`: Some data could be fulfilled from the cache but `data` is
|
|
91
|
+
* incomplete. This is only possible when `returnPartialData` is `true`.
|
|
92
|
+
* - `streaming`: `data` is incomplete as a result of a deferred query and
|
|
93
|
+
* the result is still streaming in.
|
|
94
|
+
* - `complete`: `data` is a fully satisfied query result fulfilled
|
|
95
|
+
* either from the cache or network.
|
|
96
|
+
*
|
|
97
|
+
* @docGroup 1. Operation data
|
|
98
|
+
*/
|
|
99
|
+
dataState: "partial";
|
|
100
|
+
};
|
|
72
101
|
namespace DocumentationTypes {
|
|
73
102
|
namespace useFragment {
|
|
74
103
|
interface Result<TData> {
|
|
@@ -99,5 +128,35 @@ export declare namespace useFragment {
|
|
|
99
128
|
*
|
|
100
129
|
* To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.
|
|
101
130
|
*/
|
|
131
|
+
export declare function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useFragment.Options<TData, TVariables> & {
|
|
132
|
+
from: Array<NonNullable<useFragment.FromValue<TData>>>;
|
|
133
|
+
}): useFragment.Result<Array<TData>>;
|
|
134
|
+
/**
|
|
135
|
+
* `useFragment` represents a lightweight live binding into the Apollo Client Cache and enables Apollo Client to broadcast very specific fragment results to individual components. This hook returns an always-up-to-date view of whatever data the cache currently contains for a given fragment. `useFragment` never triggers network requests of its own.
|
|
136
|
+
*
|
|
137
|
+
* Note that the `useQuery` hook remains the primary hook responsible for querying and populating data in the cache ([see the API reference](./hooks#usequery)). As a result, the component reading the fragment data via `useFragment` is still subscribed to all changes in the query data, but receives updates only when that fragment's specific data change.
|
|
138
|
+
*
|
|
139
|
+
* To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.
|
|
140
|
+
*/
|
|
141
|
+
export declare function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useFragment.Options<TData, TVariables> & {
|
|
142
|
+
from: Array<null>;
|
|
143
|
+
}): useFragment.Result<Array<null>>;
|
|
144
|
+
/**
|
|
145
|
+
* `useFragment` represents a lightweight live binding into the Apollo Client Cache and enables Apollo Client to broadcast very specific fragment results to individual components. This hook returns an always-up-to-date view of whatever data the cache currently contains for a given fragment. `useFragment` never triggers network requests of its own.
|
|
146
|
+
*
|
|
147
|
+
* Note that the `useQuery` hook remains the primary hook responsible for querying and populating data in the cache ([see the API reference](./hooks#usequery)). As a result, the component reading the fragment data via `useFragment` is still subscribed to all changes in the query data, but receives updates only when that fragment's specific data change.
|
|
148
|
+
*
|
|
149
|
+
* To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.
|
|
150
|
+
*/
|
|
151
|
+
export declare function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useFragment.Options<TData, TVariables> & {
|
|
152
|
+
from: Array<useFragment.FromValue<TData>>;
|
|
153
|
+
}): useFragment.Result<Array<TData | null>>;
|
|
154
|
+
/**
|
|
155
|
+
* `useFragment` represents a lightweight live binding into the Apollo Client Cache and enables Apollo Client to broadcast very specific fragment results to individual components. This hook returns an always-up-to-date view of whatever data the cache currently contains for a given fragment. `useFragment` never triggers network requests of its own.
|
|
156
|
+
*
|
|
157
|
+
* Note that the `useQuery` hook remains the primary hook responsible for querying and populating data in the cache ([see the API reference](./hooks#usequery)). As a result, the component reading the fragment data via `useFragment` is still subscribed to all changes in the query data, but receives updates only when that fragment's specific data change.
|
|
158
|
+
*
|
|
159
|
+
* To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.
|
|
160
|
+
*/
|
|
102
161
|
export declare function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: useFragment.Options<TData, TVariables>): useFragment.Result<TData>;
|
|
103
162
|
//# sourceMappingURL=useFragment.d.ts.map
|