@apollo/client 4.0.2 → 4.0.4

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 (75) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/__cjs/core/ApolloClient.cjs +8 -0
  3. package/__cjs/core/ApolloClient.cjs.map +1 -1
  4. package/__cjs/core/ApolloClient.d.cts +9 -1
  5. package/__cjs/core/index.cjs.map +1 -1
  6. package/__cjs/core/index.d.cts +5 -0
  7. package/__cjs/masking/maskFragment.cjs +2 -3
  8. package/__cjs/masking/maskFragment.cjs.map +1 -1
  9. package/__cjs/react/hooks/internal/wrapHook.cjs +2 -3
  10. package/__cjs/react/hooks/internal/wrapHook.cjs.map +1 -1
  11. package/__cjs/react/hooks/internal/wrapHook.d.cts +0 -1
  12. package/__cjs/react/hooks/useFragment.cjs +2 -2
  13. package/__cjs/react/hooks/useFragment.cjs.map +1 -1
  14. package/__cjs/react/hooks/useMutation.cjs +4 -3
  15. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  16. package/__cjs/react/hooks/useMutation.d.cts +1 -1
  17. package/__cjs/react/hooks/useQuery.cjs +34 -13
  18. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  19. package/__cjs/react/hooks/useQuery.d.cts +160 -4
  20. package/__cjs/react/index.cjs.map +1 -1
  21. package/__cjs/react/index.d.cts +9 -0
  22. package/__cjs/react/internal/index.cjs +1 -3
  23. package/__cjs/react/internal/index.cjs.map +1 -1
  24. package/__cjs/react/internal/index.d.cts +3 -2
  25. package/__cjs/utilities/caching/sizes.cjs.map +1 -1
  26. package/__cjs/utilities/caching/sizes.d.cts +0 -11
  27. package/__cjs/utilities/internal/equalByQuery.cjs +3 -4
  28. package/__cjs/utilities/internal/equalByQuery.cjs.map +1 -1
  29. package/__cjs/utilities/invariant/index.cjs +3 -0
  30. package/__cjs/utilities/invariant/index.cjs.map +1 -1
  31. package/__cjs/version.cjs +1 -1
  32. package/core/ApolloClient.d.ts +9 -1
  33. package/core/ApolloClient.js +8 -0
  34. package/core/ApolloClient.js.map +1 -1
  35. package/core/index.d.ts +6 -1
  36. package/core/index.js.map +1 -1
  37. package/masking/maskFragment.js +1 -1
  38. package/masking/maskFragment.js.map +1 -1
  39. package/package.json +1 -1
  40. package/react/hooks/internal/wrapHook.d.ts +0 -1
  41. package/react/hooks/internal/wrapHook.js +1 -1
  42. package/react/hooks/internal/wrapHook.js.map +1 -1
  43. package/react/hooks/useFragment.js +1 -1
  44. package/react/hooks/useFragment.js.map +1 -1
  45. package/react/hooks/useMutation.d.ts +1 -1
  46. package/react/hooks/useMutation.js +5 -4
  47. package/react/hooks/useMutation.js.map +1 -1
  48. package/react/hooks/useQuery.d.ts +160 -4
  49. package/react/hooks/useQuery.js +35 -14
  50. package/react/hooks/useQuery.js.map +1 -1
  51. package/react/hooks-compiled/internal/wrapHook.d.ts +0 -1
  52. package/react/hooks-compiled/internal/wrapHook.js +1 -1
  53. package/react/hooks-compiled/internal/wrapHook.js.map +1 -1
  54. package/react/hooks-compiled/useFragment.js +1 -1
  55. package/react/hooks-compiled/useFragment.js.map +1 -1
  56. package/react/hooks-compiled/useMutation.d.ts +1 -1
  57. package/react/hooks-compiled/useMutation.js +3 -3
  58. package/react/hooks-compiled/useMutation.js.map +1 -1
  59. package/react/hooks-compiled/useQuery.d.ts +160 -4
  60. package/react/hooks-compiled/useQuery.js +154 -133
  61. package/react/hooks-compiled/useQuery.js.map +1 -1
  62. package/react/index.compiled.d.ts +9 -0
  63. package/react/index.compiled.js.map +1 -1
  64. package/react/index.d.ts +10 -1
  65. package/react/index.js.map +1 -1
  66. package/react/internal/index.d.ts +3 -2
  67. package/react/internal/index.js +1 -2
  68. package/react/internal/index.js.map +1 -1
  69. package/utilities/caching/sizes.d.ts +0 -11
  70. package/utilities/caching/sizes.js.map +1 -1
  71. package/utilities/internal/equalByQuery.js +1 -1
  72. package/utilities/internal/equalByQuery.js.map +1 -1
  73. package/utilities/invariant/index.js +3 -0
  74. package/utilities/invariant/index.js.map +1 -1
  75. package/version.js +1 -1
@@ -1,7 +1,7 @@
1
1
  import * as React from "react";
2
+ import { wrapperSymbol } from "@apollo/client/react/internal";
2
3
  // direct import to avoid circular dependency
3
4
  import { getApolloContext } from "../../context/ApolloContext.js";
4
- export const wrapperSymbol = Symbol.for("apollo.hook.wrappers");
5
5
  /**
6
6
  * @internal
7
7
  *
@@ -1 +1 @@
1
- {"version":3,"file":"wrapHook.js","sources":["../../../../src/react/hooks/internal/wrapHook.ts"],"sourcesContent":["import * as React from \"react\";\n\nimport type { ApolloClient, InternalTypes } from \"@apollo/client\";\nimport type { ObservableQuery } from \"@apollo/client\";\nimport type { createQueryPreloader } from \"@apollo/client/react\";\nimport type {\n useBackgroundQuery,\n useFragment,\n useQuery,\n useQueryRefHandlers,\n useReadQuery,\n useSuspenseFragment,\n useSuspenseQuery,\n} from \"@apollo/client/react\";\n\n// direct import to avoid circular dependency\nimport { getApolloContext } from \"../../context/ApolloContext.js\";\n\nexport const wrapperSymbol = Symbol.for(\"apollo.hook.wrappers\");\n\ntype FunctionSignature<T> =\n T extends (...args: infer A) => infer R ? (...args: A) => R : never;\n\ninterface WrappableHooks {\n createQueryPreloader: FunctionSignature<typeof createQueryPreloader>;\n useQuery: FunctionSignature<typeof useQuery>;\n useSuspenseQuery: FunctionSignature<typeof useSuspenseQuery>;\n useSuspenseFragment: FunctionSignature<typeof useSuspenseFragment>;\n useBackgroundQuery: FunctionSignature<typeof useBackgroundQuery>;\n useReadQuery: FunctionSignature<typeof useReadQuery>;\n useFragment: FunctionSignature<typeof useFragment>;\n useQueryRefHandlers: FunctionSignature<typeof useQueryRefHandlers>;\n}\n\n/**\n * @internal\n * Can be used to correctly type the [Symbol.for(\"apollo.hook.wrappers\")] property of\n * `QueryManager`, to override/wrap hook functionality.\n */\nexport type HookWrappers = {\n [K in keyof WrappableHooks]?: (\n originalHook: WrappableHooks[K]\n ) => WrappableHooks[K];\n};\n\ninterface QueryManagerWithWrappers extends InternalTypes.QueryManager {\n [wrapperSymbol]?: HookWrappers;\n}\n\n/**\n * @internal\n *\n * Makes an Apollo Client hook \"wrappable\".\n * That means that the Apollo Client instance can expose a \"wrapper\" that will be\n * used to wrap the original hook implementation with additional logic.\n * @example\n *\n * ```tsx\n * // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`)\n * // following this pattern\n * function useQuery() {\n * return wrapHook('useQuery', _useQuery, options.client)(query, options);\n * }\n * function _useQuery(query, options) {\n * // original implementation\n * }\n *\n * // this is what a library like `@apollo/client-react-streaming` would do\n * class ApolloClientWithStreaming extends ApolloClient {\n * constructor(options) {\n * super(options);\n * this.queryManager[Symbol.for(\"apollo.hook.wrappers\")] = {\n * useQuery: (original) => (query, options) => {\n * console.log(\"useQuery was called with options\", options);\n * return original(query, options);\n * }\n * }\n * }\n * }\n *\n * // this will now log the options and then call the original `useQuery`\n * const client = new ApolloClientWithStreaming({ ... });\n * useQuery(query, { client });\n * ```\n */\nexport function wrapHook<Hook extends (...args: any[]) => any>(\n hookName: keyof WrappableHooks,\n useHook: Hook,\n clientOrObsQuery: ObservableQuery<any> | ApolloClient\n): Hook {\n // Priority-wise, the later entries in this array wrap\n // previous entries and could prevent them (and in the end,\n // even the original hook) from running\n const wrapperSources = [\n (\n clientOrObsQuery as unknown as {\n // both `ApolloClient` and `ObservableQuery` have a `queryManager` property\n // but they're both `private`, so we have to cast around for a bit here.\n queryManager: QueryManagerWithWrappers;\n }\n )[\"queryManager\"],\n // if we are a hook (not `preloadQuery`), we are guaranteed to be inside of\n // a React render and can use context\n hookName.startsWith(\"use\") ?\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useContext(getApolloContext())\n : undefined,\n ];\n\n let wrapped = useHook;\n for (const source of wrapperSources) {\n const wrapper = source?.[wrapperSymbol]?.[hookName];\n if (wrapper) {\n wrapped = wrapper(wrapped) as Hook;\n }\n }\n\n return wrapped;\n}\n"],"names":[],"mappings":"AAAA,CAAA,CAAA,CA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sources":["../../../../src/react/hooks/internal/wrapHook.ts"],"sourcesContent":["import * as React from \"react\";\n\nimport type { ApolloClient, InternalTypes } from \"@apollo/client\";\nimport type { ObservableQuery } from \"@apollo/client\";\nimport type { createQueryPreloader } from \"@apollo/client/react\";\nimport type {\n useBackgroundQuery,\n useFragment,\n useQuery,\n useQueryRefHandlers,\n useReadQuery,\n useSuspenseFragment,\n useSuspenseQuery,\n} from \"@apollo/client/react\";\nimport { wrapperSymbol } from \"@apollo/client/react/internal\";\n\n// direct import to avoid circular dependency\nimport { getApolloContext } from \"../../context/ApolloContext.js\";\n\ntype FunctionSignature<T> =\n T extends (...args: infer A) => infer R ? (...args: A) => R : never;\n\ninterface WrappableHooks {\n createQueryPreloader: FunctionSignature<typeof createQueryPreloader>;\n useQuery: FunctionSignature<typeof useQuery>;\n useSuspenseQuery: FunctionSignature<typeof useSuspenseQuery>;\n useSuspenseFragment: FunctionSignature<typeof useSuspenseFragment>;\n useBackgroundQuery: FunctionSignature<typeof useBackgroundQuery>;\n useReadQuery: FunctionSignature<typeof useReadQuery>;\n useFragment: FunctionSignature<typeof useFragment>;\n useQueryRefHandlers: FunctionSignature<typeof useQueryRefHandlers>;\n}\n\n/**\n * @internal\n * Can be used to correctly type the [Symbol.for(\"apollo.hook.wrappers\")] property of\n * `QueryManager`, to override/wrap hook functionality.\n */\nexport type HookWrappers = {\n [K in keyof WrappableHooks]?: (\n originalHook: WrappableHooks[K]\n ) => WrappableHooks[K];\n};\n\ninterface QueryManagerWithWrappers extends InternalTypes.QueryManager {\n [wrapperSymbol]?: HookWrappers;\n}\n\n/**\n * @internal\n *\n * Makes an Apollo Client hook \"wrappable\".\n * That means that the Apollo Client instance can expose a \"wrapper\" that will be\n * used to wrap the original hook implementation with additional logic.\n * @example\n *\n * ```tsx\n * // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`)\n * // following this pattern\n * function useQuery() {\n * return wrapHook('useQuery', _useQuery, options.client)(query, options);\n * }\n * function _useQuery(query, options) {\n * // original implementation\n * }\n *\n * // this is what a library like `@apollo/client-react-streaming` would do\n * class ApolloClientWithStreaming extends ApolloClient {\n * constructor(options) {\n * super(options);\n * this.queryManager[Symbol.for(\"apollo.hook.wrappers\")] = {\n * useQuery: (original) => (query, options) => {\n * console.log(\"useQuery was called with options\", options);\n * return original(query, options);\n * }\n * }\n * }\n * }\n *\n * // this will now log the options and then call the original `useQuery`\n * const client = new ApolloClientWithStreaming({ ... });\n * useQuery(query, { client });\n * ```\n */\nexport function wrapHook<Hook extends (...args: any[]) => any>(\n hookName: keyof WrappableHooks,\n useHook: Hook,\n clientOrObsQuery: ObservableQuery<any> | ApolloClient\n): Hook {\n // Priority-wise, the later entries in this array wrap\n // previous entries and could prevent them (and in the end,\n // even the original hook) from running\n const wrapperSources = [\n (\n clientOrObsQuery as unknown as {\n // both `ApolloClient` and `ObservableQuery` have a `queryManager` property\n // but they're both `private`, so we have to cast around for a bit here.\n queryManager: QueryManagerWithWrappers;\n }\n )[\"queryManager\"],\n // if we are a hook (not `preloadQuery`), we are guaranteed to be inside of\n // a React render and can use context\n hookName.startsWith(\"use\") ?\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useContext(getApolloContext())\n : undefined,\n ];\n\n let wrapped = useHook;\n for (const source of wrapperSources) {\n const wrapper = source?.[wrapperSymbol]?.[hookName];\n if (wrapper) {\n wrapped = wrapper(wrapped) as Hook;\n }\n }\n\n return wrapped;\n}\n"],"names":[],"mappings":"AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAO,EAAP,CAAA,EAAY,CAAZ,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAuB,CAAvB,CAAA,CAAA,CAAA,CAAA,CAAA,CAA8B;AAc9B,CAAA,CAAA,CAAA,CAAA,CAAA,EAAO,EAAE,CAAT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,EAA8B,CA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AAA,CAAA,CAAA,CAAA,CAAA,CAAuB;IACrB,CAAF,CAAA,EAAA,CAAO,CAAP,CAAA,CAAA,CAAA,EAAa,CAAb,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAuB,CAAvB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAqC,EAAE;QACnC,CAAJ,CAAA,CAAA,CAAA,EAAU,CAAV,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAoB,CAApB,CAAA,CAAA,CAAA,CAAA,CAA0B,CAA1B,CAA4B,CAAC,CAA7B,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAA0C,CAAC,CAA3C,CAA6C,CAAC,CAA9C,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAsD,CAAC;QACnD,CAAJ,EAAA,CAAQ,CAAR,CAAA,CAAA,CAAA,CAAA,CAAA,CAAe,EAAE;YACX,CAAN,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAgB,CAAhB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAuB,CAAC,CAAxB,CAAA,CAAA,CAAA,CAAA,CAAA,CAA+B,CAAS;QACpC;IACF;IAEA,CAAF,CAAA,CAAA,CAAA,CAAA,EAAS,CAAT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAgB;AAChB;"}
@@ -1,4 +1,4 @@
1
- import equal from "@wry/equality";
1
+ import { equal } from "@wry/equality";
2
2
  import * as React from "react";
3
3
  import { useDeepMemo, wrapHook } from "./internal/index.js";
4
4
  import { useApolloClient } from "./useApolloClient.js";
@@ -1 +1 @@
1
- {"version":3,"file":"useFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,eAAe,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAmB/B,OAAO,EAAE,WAAW,EAAE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C,MAAM,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,SAAS,CAAC;gBAC5C,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE;oBACf,mEAAmE;oBACnE,sDAAsD;oBACtD,iBAAiB;oBACjB,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC;wBAAE,OAAO;oBACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;oBACrB,iEAAiE;oBACjE,kEAAkE;oBAClE,6DAA6D;oBAC7D,qEAAqE;oBACrE,YAAY,CAAC,WAAW,CAAC,CAAC;oBAC1B,WAAW,GAAG,UAAU,CAAC,WAAW,CAAQ,CAAC;gBAC/C,CAAC;aACF,CAAC,CAAC;QACP,OAAO,GAAG,EAAE;YACV,YAAY,EAAE,WAAW,EAAE,CAAC;YAC5B,YAAY,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EACD,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,CAAC,CAC9B,EACD,WAAW,EACX,WAAW,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,IAA6B;IAE7B,MAAM,MAAM,GAAG;QACb,IAAI,EAAE,IAAI,CAAC,MAAM;QACjB,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ;QACzB,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS;KACrB,CAAC,CAAC,uDAAuD;IAEvF,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;IACxC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import equal from \"@wry/equality\";\nimport * as React from \"react\";\n\nimport type {\n ApolloClient,\n DataValue,\n DocumentNode,\n GetDataState,\n OperationVariables,\n TypedDocumentNode,\n} from \"@apollo/client\";\nimport type {\n Cache,\n MissingTree,\n Reference,\n StoreObject,\n} from \"@apollo/client/cache\";\nimport type { FragmentType, MaybeMasked } from \"@apollo/client/masking\";\nimport type { NoInfer } from \"@apollo/client/utilities/internal\";\n\nimport { useDeepMemo, wrapHook } from \"./internal/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\n\nexport declare namespace useFragment {\n import _self = useFragment;\n export interface 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 * Any variables that the GraphQL query may depend on.\n */\n variables?: NoInfer<TVariables>;\n\n /**\n * An object containing a `__typename` and primary key fields (such as `id`) identifying the entity object from which the fragment will be retrieved, or a `{ __ref: \"...\" }` reference, or a `string` ID (uncommon).\n */\n from:\n | StoreObject\n | Reference\n | FragmentType<NoInfer<TData>>\n | string\n | null;\n\n /**\n * Whether to read from optimistic or non-optimistic cache data. If\n * this named option is provided, the optimistic parameter of the\n * readQuery method can be omitted.\n *\n * @defaultValue true\n */\n optimistic?: boolean;\n\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 namespace DocumentationTypes {\n namespace useFragment {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n > extends _self.Options<TData, TVariables> {}\n }\n }\n\n // TODO: Update this to return `null` when there is no data returned from the\n // fragment.\n export type Result<TData> =\n | ({\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#complete:member} */\n complete: true;\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#missing:member} */\n missing?: never;\n } & GetDataState<MaybeMasked<TData>, \"complete\">)\n | ({\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#complete:member} */\n complete: false;\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#missing:member} */\n missing?: MissingTree;\n } & GetDataState<MaybeMasked<TData>, \"partial\">);\n\n export namespace DocumentationTypes {\n namespace useFragment {\n export interface Result<TData> {\n data: MaybeMasked<TData> | DataValue.Partial<MaybeMasked<TData>>;\n complete: boolean;\n /**\n * 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.\n */\n missing?: MissingTree;\n }\n }\n }\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useFragment:function(1)} */\n export function useFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >({\n fragment,\n from,\n fragmentName,\n variables,\n optimistic,\n client,\n }: useFragment.Options<TData, TVariables>): useFragment.Result<TData>;\n }\n}\n\n/**\n * `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.\n *\n * 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.\n *\n * To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.\n */\nexport function useFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(options: useFragment.Options<TData, TVariables>): useFragment.Result<TData> {\n \"use no memo\";\n return wrapHook(\n \"useFragment\",\n // eslint-disable-next-line react-compiler/react-compiler\n useFragment_,\n useApolloClient(options.client)\n )(options);\n}\n\nfunction useFragment_<TData, TVariables extends OperationVariables>(\n options: useFragment.Options<TData, TVariables>\n): useFragment.Result<TData> {\n const client = useApolloClient(options.client);\n const { cache } = client;\n const { from, ...rest } = options;\n\n // We calculate the cache id seperately from `stableOptions` because we don't\n // want changes to non key fields in the `from` property to affect\n // `stableOptions` and retrigger our subscription. If the cache identifier\n // stays the same between renders, we want to reuse the existing subscription.\n const id = React.useMemo(\n () =>\n typeof from === \"string\" ? from\n : from === null ? null\n : cache.identify(from),\n [cache, from]\n );\n\n const stableOptions = useDeepMemo(() => ({ ...rest, from: id! }), [rest, id]);\n\n // Since .next is async, we need to make sure that we\n // get the correct diff on the next render given new diffOptions\n const diff = React.useMemo(() => {\n const { fragment, fragmentName, from, optimistic = true } = stableOptions;\n\n if (from === null) {\n return {\n result: diffToResult({\n result: {},\n complete: false,\n } as Cache.DiffResult<TData>),\n };\n }\n\n const { cache } = client;\n const diff = cache.diff<TData, TVariables>({\n ...stableOptions,\n returnPartialData: true,\n id: from,\n query: cache[\"getFragmentDoc\"](\n client[\"transform\"](fragment),\n fragmentName\n ),\n optimistic,\n });\n\n return {\n result: diffToResult<TData>({\n ...diff,\n result: client[\"queryManager\"].maskFragment({\n fragment,\n fragmentName,\n // TODO: Revert to `diff.result` once `useFragment` supports `null` as\n // valid return value\n data: diff.result === null ? {} : diff.result,\n }) as any,\n }),\n };\n }, [client, stableOptions]);\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = React.useCallback(() => diff.result, [diff]);\n\n return useSyncExternalStore(\n React.useCallback(\n (forceUpdate) => {\n let lastTimeout = 0;\n\n const subscription =\n stableOptions.from === null ?\n null\n : client.watchFragment(stableOptions).subscribe({\n next: (result) => {\n // Avoid unnecessarily rerendering this hook for the initial result\n // emitted from watchFragment which should be equal to\n // `diff.result`.\n if (equal(result, diff.result)) return;\n diff.result = result;\n // If we get another update before we've re-rendered, bail out of\n // the update and try again. This ensures that the relative timing\n // between useQuery and useFragment stays roughly the same as\n // fixed in https://github.com/apollographql/apollo-client/pull/11083\n clearTimeout(lastTimeout);\n lastTimeout = setTimeout(forceUpdate) as any;\n },\n });\n return () => {\n subscription?.unsubscribe();\n clearTimeout(lastTimeout);\n };\n },\n [client, stableOptions, diff]\n ),\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): useFragment.Result<TData> {\n const result = {\n data: diff.result,\n complete: !!diff.complete,\n dataState: diff.complete ? \"complete\" : \"partial\",\n } as useFragment.Result<TData>; // TODO: Remove assertion once useFragment returns null\n\n if (diff.missing) {\n result.missing = diff.missing.missing;\n }\n\n return result;\n}\n"]}
1
+ {"version":3,"file":"useFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAmB/B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AA4GjE;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAGzB,OAA+C;IAC/C,aAAa,CAAC;IACd,OAAO,QAAQ,CACb,aAAa;IACb,yDAAyD;IACzD,YAAY,EACZ,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAChC,CAAC,OAAO,CAAC,CAAC;AACb,CAAC;AAED,SAAS,YAAY,CACnB,OAA+C;IAE/C,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IACzB,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IAElC,6EAA6E;IAC7E,kEAAkE;IAClE,0EAA0E;IAC1E,8EAA8E;IAC9E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,WAAW,EACX,WAAW,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,IAA6B;IAE7B,MAAM,MAAM,GAAG;QACb,IAAI,EAAE,IAAI,CAAC,MAAM;QACjB,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ;QACzB,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS;KACrB,CAAC,CAAC,uDAAuD;IAEvF,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;IACxC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { equal } from \"@wry/equality\";\nimport * as React from \"react\";\n\nimport type {\n ApolloClient,\n DataValue,\n DocumentNode,\n GetDataState,\n OperationVariables,\n TypedDocumentNode,\n} from \"@apollo/client\";\nimport type {\n Cache,\n MissingTree,\n Reference,\n StoreObject,\n} from \"@apollo/client/cache\";\nimport type { FragmentType, MaybeMasked } from \"@apollo/client/masking\";\nimport type { NoInfer } from \"@apollo/client/utilities/internal\";\n\nimport { useDeepMemo, wrapHook } from \"./internal/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\n\nexport declare namespace useFragment {\n import _self = useFragment;\n export interface 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 * Any variables that the GraphQL query may depend on.\n */\n variables?: NoInfer<TVariables>;\n\n /**\n * An object containing a `__typename` and primary key fields (such as `id`) identifying the entity object from which the fragment will be retrieved, or a `{ __ref: \"...\" }` reference, or a `string` ID (uncommon).\n */\n from:\n | StoreObject\n | Reference\n | FragmentType<NoInfer<TData>>\n | string\n | null;\n\n /**\n * Whether to read from optimistic or non-optimistic cache data. If\n * this named option is provided, the optimistic parameter of the\n * readQuery method can be omitted.\n *\n * @defaultValue true\n */\n optimistic?: boolean;\n\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 namespace DocumentationTypes {\n namespace useFragment {\n export interface Options<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n > extends _self.Options<TData, TVariables> {}\n }\n }\n\n // TODO: Update this to return `null` when there is no data returned from the\n // fragment.\n export type Result<TData> =\n | ({\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#complete:member} */\n complete: true;\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#missing:member} */\n missing?: never;\n } & GetDataState<MaybeMasked<TData>, \"complete\">)\n | ({\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#complete:member} */\n complete: false;\n /** {@inheritDoc @apollo/client/react!useFragment.DocumentationTypes.useFragment.Result#missing:member} */\n missing?: MissingTree;\n } & GetDataState<MaybeMasked<TData>, \"partial\">);\n\n export namespace DocumentationTypes {\n namespace useFragment {\n export interface Result<TData> {\n data: MaybeMasked<TData> | DataValue.Partial<MaybeMasked<TData>>;\n complete: boolean;\n /**\n * 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.\n */\n missing?: MissingTree;\n }\n }\n }\n export namespace DocumentationTypes {\n /** {@inheritDoc @apollo/client/react!useFragment:function(1)} */\n export function useFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >({\n fragment,\n from,\n fragmentName,\n variables,\n optimistic,\n client,\n }: useFragment.Options<TData, TVariables>): useFragment.Result<TData>;\n }\n}\n\n/**\n * `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.\n *\n * 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.\n *\n * To view a `useFragment` example, see the [Fragments](https://www.apollographql.com/docs/react/data/fragments#usefragment) page.\n */\nexport function useFragment<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(options: useFragment.Options<TData, TVariables>): useFragment.Result<TData> {\n \"use no memo\";\n return wrapHook(\n \"useFragment\",\n // eslint-disable-next-line react-compiler/react-compiler\n useFragment_,\n useApolloClient(options.client)\n )(options);\n}\n\nfunction useFragment_<TData, TVariables extends OperationVariables>(\n options: useFragment.Options<TData, TVariables>\n): useFragment.Result<TData> {\n const client = useApolloClient(options.client);\n const { cache } = client;\n const { from, ...rest } = options;\n\n // We calculate the cache id seperately from `stableOptions` because we don't\n // want changes to non key fields in the `from` property to affect\n // `stableOptions` and retrigger our subscription. If the cache identifier\n // stays the same between renders, we want to reuse the existing subscription.\n const id = React.useMemo(\n () =>\n typeof from === \"string\" ? from\n : from === null ? null\n : cache.identify(from),\n [cache, from]\n );\n\n const stableOptions = useDeepMemo(() => ({ ...rest, from: id! }), [rest, id]);\n\n // Since .next is async, we need to make sure that we\n // get the correct diff on the next render given new diffOptions\n const diff = React.useMemo(() => {\n const { fragment, fragmentName, from, optimistic = true } = stableOptions;\n\n if (from === null) {\n return {\n result: diffToResult({\n result: {},\n complete: false,\n } as Cache.DiffResult<TData>),\n };\n }\n\n const { cache } = client;\n const diff = cache.diff<TData, TVariables>({\n ...stableOptions,\n returnPartialData: true,\n id: from,\n query: cache[\"getFragmentDoc\"](\n client[\"transform\"](fragment),\n fragmentName\n ),\n optimistic,\n });\n\n return {\n result: diffToResult<TData>({\n ...diff,\n result: client[\"queryManager\"].maskFragment({\n fragment,\n fragmentName,\n // TODO: Revert to `diff.result` once `useFragment` supports `null` as\n // valid return value\n data: diff.result === null ? {} : diff.result,\n }) as any,\n }),\n };\n }, [client, stableOptions]);\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = React.useCallback(() => diff.result, [diff]);\n\n return useSyncExternalStore(\n React.useCallback(\n (forceUpdate) => {\n let lastTimeout = 0;\n\n const subscription =\n stableOptions.from === null ?\n null\n : client.watchFragment(stableOptions).subscribe({\n next: (result) => {\n // Avoid unnecessarily rerendering this hook for the initial result\n // emitted from watchFragment which should be equal to\n // `diff.result`.\n if (equal(result, diff.result)) return;\n diff.result = result;\n // If we get another update before we've re-rendered, bail out of\n // the update and try again. This ensures that the relative timing\n // between useQuery and useFragment stays roughly the same as\n // fixed in https://github.com/apollographql/apollo-client/pull/11083\n clearTimeout(lastTimeout);\n lastTimeout = setTimeout(forceUpdate) as any;\n },\n });\n return () => {\n subscription?.unsubscribe();\n clearTimeout(lastTimeout);\n };\n },\n [client, stableOptions, diff]\n ),\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): useFragment.Result<TData> {\n const result = {\n data: diff.result,\n complete: !!diff.complete,\n dataState: diff.complete ? \"complete\" : \"partial\",\n } as useFragment.Result<TData>; // TODO: Remove assertion once useFragment returns null\n\n if (diff.missing) {\n result.missing = diff.missing.missing;\n }\n\n return result;\n}\n"]}
@@ -81,7 +81,7 @@ export declare namespace useMutation {
81
81
  *
82
82
  * @docGroup 1. Operation options
83
83
  */
84
- variables?: TConfiguredVariables;
84
+ variables?: Partial<TVariables> & TConfiguredVariables;
85
85
  /**
86
86
  * 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.
87
87
  *
@@ -1,6 +1,6 @@
1
1
  import { equal } from "@wry/equality";
2
2
  import * as React from "react";
3
- import { mergeOptions } from "@apollo/client/utilities/internal";
3
+ import { mergeOptions, preventUnhandledRejection, } from "@apollo/client/utilities/internal";
4
4
  import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
5
5
  import { useApolloClient } from "./useApolloClient.js";
6
6
  /**
@@ -78,7 +78,7 @@ export function useMutation(mutation, options) {
78
78
  }
79
79
  const mutationId = ++ref.current.mutationId;
80
80
  const clientOptions = mergeOptions(baseOptions, executeOptions);
81
- return client
81
+ return preventUnhandledRejection(client
82
82
  .mutate(clientOptions)
83
83
  .then((response) => {
84
84
  const { data, error } = response;
@@ -94,7 +94,8 @@ export function useMutation(mutation, options) {
94
94
  error,
95
95
  client,
96
96
  };
97
- if (ref.current.isMounted && !equal(ref.current.result, result)) {
97
+ if (ref.current.isMounted &&
98
+ !equal(ref.current.result, result)) {
98
99
  setResult((ref.current.result = result));
99
100
  }
100
101
  }
@@ -122,7 +123,7 @@ export function useMutation(mutation, options) {
122
123
  onError(error, clientOptions);
123
124
  }
124
125
  throw error;
125
- });
126
+ }));
126
127
  }, []);
127
128
  const reset = React.useCallback(() => {
128
129
  if (ref.current.isMounted) {
@@ -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,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEjE,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACpF,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAmJvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OASC;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,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 { mergeOptions } 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?: 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 client\n .mutate(\n clientOptions as ApolloClient.MutateOptions<TData, OperationVariables>\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 (ref.current.isMounted && !equal(ref.current.result, result)) {\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 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;A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,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 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,7 +1,9 @@
1
- import type { ApolloClient, DataState, DefaultContext, DocumentNode, ErrorLike, ErrorPolicy, GetDataState, InternalTypes, ObservableQuery, OperationVariables, RefetchWritePolicy, SubscribeToMoreFunction, TypedDocumentNode, UpdateQueryMapFn, WatchQueryFetchPolicy } from "@apollo/client";
1
+ import type { DataState, DefaultContext, DocumentNode, ErrorLike, ErrorPolicy, GetDataState, InternalTypes, ObservableQuery, OperationVariables, RefetchWritePolicy, SubscribeToMoreFunction, TypedDocumentNode, UpdateQueryMapFn, WatchQueryFetchPolicy } from "@apollo/client";
2
+ import type { ApolloClient } from "@apollo/client";
2
3
  import { NetworkStatus } from "@apollo/client";
3
4
  import type { MaybeMasked } from "@apollo/client/masking";
4
5
  import type { DocumentationTypes as UtilityDocumentationTypes, NoInfer, VariablesOption } from "@apollo/client/utilities/internal";
6
+ import type { SkipToken } from "./constants.js";
5
7
  export declare namespace useQuery {
6
8
  namespace Base {
7
9
  interface Options<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
@@ -119,7 +121,7 @@ export declare namespace useQuery {
119
121
  }
120
122
  }
121
123
  namespace Base {
122
- interface Result<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
124
+ interface Result<TData = unknown, TVariables extends OperationVariables = OperationVariables, TReturnVariables extends OperationVariables = TVariables> {
123
125
  /**
124
126
  * The instance of Apollo Client that executed the query.
125
127
  * Can be useful for manually executing followup queries or writing data to the cache.
@@ -213,7 +215,7 @@ export declare namespace useQuery {
213
215
  *
214
216
  * @docGroup 1. Operation data
215
217
  */
216
- variables: TVariables;
218
+ variables: TReturnVariables;
217
219
  /**
218
220
  * A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
219
221
  *
@@ -223,7 +225,7 @@ export declare namespace useQuery {
223
225
  fetchMore: <TFetchData = TData, TFetchVars extends OperationVariables = TVariables>(fetchMoreOptions: ObservableQuery.FetchMoreOptions<TData, TVariables, TFetchData, TFetchVars>) => Promise<ApolloClient.QueryResult<MaybeMasked<TFetchData>>>;
224
226
  }
225
227
  }
226
- type Result<TData = unknown, TVariables extends OperationVariables = OperationVariables, TStates extends DataState<TData>["dataState"] = DataState<TData>["dataState"]> = Base.Result<TData, TVariables> & GetDataState<MaybeMasked<TData>, TStates>;
228
+ type Result<TData = unknown, TVariables extends OperationVariables = OperationVariables, TStates extends DataState<TData>["dataState"] = DataState<TData>["dataState"], TReturnVariables extends OperationVariables = TVariables> = Base.Result<TData, TVariables, TReturnVariables> & GetDataState<MaybeMasked<TData>, TStates>;
227
229
  namespace DocumentationTypes {
228
230
  namespace useQuery {
229
231
  interface Result<TData = unknown, TVariables extends OperationVariables = OperationVariables> extends Base.Result<TData, TVariables>, UtilityDocumentationTypes.DataState<TData> {
@@ -345,6 +347,82 @@ export declare function useQuery<TData = unknown, TVariables extends OperationVa
345
347
  * @param options - Options to control how the query is executed.
346
348
  * @returns Query result object
347
349
  */
350
+ export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken): useQuery.Result<TData, TVariables, "empty", Record<string, never>>;
351
+ /**
352
+ * A hook for executing queries in an Apollo application.
353
+ *
354
+ * To run a query within a React component, call `useQuery` and pass it a GraphQL query document.
355
+ *
356
+ * When your component renders, `useQuery` returns an object from Apollo Client that contains `loading`, `error`, `dataState`, and `data` properties you can use to render your UI.
357
+ *
358
+ * > Refer to the [Queries](https://www.apollographql.com/docs/react/data/queries) section for a more in-depth overview of `useQuery`.
359
+ *
360
+ * @example
361
+ *
362
+ * ```jsx
363
+ * import { gql } from "@apollo/client";
364
+ * import { useQuery } from "@apollo/client/react";
365
+ *
366
+ * const GET_GREETING = gql`
367
+ * query GetGreeting($language: String!) {
368
+ * greeting(language: $language) {
369
+ * message
370
+ * }
371
+ * }
372
+ * `;
373
+ *
374
+ * function Hello() {
375
+ * const { loading, error, data } = useQuery(GET_GREETING, {
376
+ * variables: { language: "english" },
377
+ * });
378
+ * if (loading) return <p>Loading ...</p>;
379
+ * return <h1>Hello {data.greeting.message}!</h1>;
380
+ * }
381
+ * ```
382
+ *
383
+ * @param query - A GraphQL query document parsed into an AST by `gql`.
384
+ * @param options - Options to control how the query is executed.
385
+ * @returns Query result object
386
+ */
387
+ export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken | (useQuery.Options<NoInfer<TData>, NoInfer<TVariables>> & {
388
+ returnPartialData: true;
389
+ })): useQuery.Result<TData, TVariables, "empty" | "complete" | "streaming" | "partial", Partial<TVariables>>;
390
+ /**
391
+ * A hook for executing queries in an Apollo application.
392
+ *
393
+ * To run a query within a React component, call `useQuery` and pass it a GraphQL query document.
394
+ *
395
+ * When your component renders, `useQuery` returns an object from Apollo Client that contains `loading`, `error`, `dataState`, and `data` properties you can use to render your UI.
396
+ *
397
+ * > Refer to the [Queries](https://www.apollographql.com/docs/react/data/queries) section for a more in-depth overview of `useQuery`.
398
+ *
399
+ * @example
400
+ *
401
+ * ```jsx
402
+ * import { gql } from "@apollo/client";
403
+ * import { useQuery } from "@apollo/client/react";
404
+ *
405
+ * const GET_GREETING = gql`
406
+ * query GetGreeting($language: String!) {
407
+ * greeting(language: $language) {
408
+ * message
409
+ * }
410
+ * }
411
+ * `;
412
+ *
413
+ * function Hello() {
414
+ * const { loading, error, data } = useQuery(GET_GREETING, {
415
+ * variables: { language: "english" },
416
+ * });
417
+ * if (loading) return <p>Loading ...</p>;
418
+ * return <h1>Hello {data.greeting.message}!</h1>;
419
+ * }
420
+ * ```
421
+ *
422
+ * @param query - A GraphQL query document parsed into an AST by `gql`.
423
+ * @param options - Options to control how the query is executed.
424
+ * @returns Query result object
425
+ */
348
426
  export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: useQuery.Options<NoInfer<TData>, NoInfer<TVariables>> & {
349
427
  returnPartialData: boolean;
350
428
  }): useQuery.Result<TData, TVariables, "empty" | "complete" | "streaming" | "partial">;
@@ -384,9 +462,87 @@ export declare function useQuery<TData = unknown, TVariables extends OperationVa
384
462
  * @param options - Options to control how the query is executed.
385
463
  * @returns Query result object
386
464
  */
465
+ export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken | (useQuery.Options<NoInfer<TData>, NoInfer<TVariables>> & {
466
+ returnPartialData: boolean;
467
+ })): useQuery.Result<TData, TVariables, "empty" | "complete" | "streaming" | "partial", Partial<TVariables>>;
468
+ /**
469
+ * A hook for executing queries in an Apollo application.
470
+ *
471
+ * To run a query within a React component, call `useQuery` and pass it a GraphQL query document.
472
+ *
473
+ * When your component renders, `useQuery` returns an object from Apollo Client that contains `loading`, `error`, `dataState`, and `data` properties you can use to render your UI.
474
+ *
475
+ * > Refer to the [Queries](https://www.apollographql.com/docs/react/data/queries) section for a more in-depth overview of `useQuery`.
476
+ *
477
+ * @example
478
+ *
479
+ * ```jsx
480
+ * import { gql } from "@apollo/client";
481
+ * import { useQuery } from "@apollo/client/react";
482
+ *
483
+ * const GET_GREETING = gql`
484
+ * query GetGreeting($language: String!) {
485
+ * greeting(language: $language) {
486
+ * message
487
+ * }
488
+ * }
489
+ * `;
490
+ *
491
+ * function Hello() {
492
+ * const { loading, error, data } = useQuery(GET_GREETING, {
493
+ * variables: { language: "english" },
494
+ * });
495
+ * if (loading) return <p>Loading ...</p>;
496
+ * return <h1>Hello {data.greeting.message}!</h1>;
497
+ * }
498
+ * ```
499
+ *
500
+ * @param query - A GraphQL query document parsed into an AST by `gql`.
501
+ * @param options - Options to control how the query is executed.
502
+ * @returns Query result object
503
+ */
387
504
  export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [
388
505
  options?: useQuery.Options<NoInfer<TData>, NoInfer<TVariables>>
389
506
  ] : [options: useQuery.Options<NoInfer<TData>, NoInfer<TVariables>>]): useQuery.Result<TData, TVariables, "empty" | "complete" | "streaming">;
507
+ /**
508
+ * A hook for executing queries in an Apollo application.
509
+ *
510
+ * To run a query within a React component, call `useQuery` and pass it a GraphQL query document.
511
+ *
512
+ * When your component renders, `useQuery` returns an object from Apollo Client that contains `loading`, `error`, `dataState`, and `data` properties you can use to render your UI.
513
+ *
514
+ * > Refer to the [Queries](https://www.apollographql.com/docs/react/data/queries) section for a more in-depth overview of `useQuery`.
515
+ *
516
+ * @example
517
+ *
518
+ * ```jsx
519
+ * import { gql } from "@apollo/client";
520
+ * import { useQuery } from "@apollo/client/react";
521
+ *
522
+ * const GET_GREETING = gql`
523
+ * query GetGreeting($language: String!) {
524
+ * greeting(language: $language) {
525
+ * message
526
+ * }
527
+ * }
528
+ * `;
529
+ *
530
+ * function Hello() {
531
+ * const { loading, error, data } = useQuery(GET_GREETING, {
532
+ * variables: { language: "english" },
533
+ * });
534
+ * if (loading) return <p>Loading ...</p>;
535
+ * return <h1>Hello {data.greeting.message}!</h1>;
536
+ * }
537
+ * ```
538
+ *
539
+ * @param query - A GraphQL query document parsed into an AST by `gql`.
540
+ * @param options - Options to control how the query is executed.
541
+ * @returns Query result object
542
+ */
543
+ export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [
544
+ options?: SkipToken | useQuery.Options<NoInfer<TData>, NoInfer<TVariables>>
545
+ ] : [options: SkipToken | useQuery.Options<NoInfer<TData>, NoInfer<TVariables>>]): useQuery.Result<TData, TVariables, "empty" | "complete" | "streaming", Partial<TVariables>>;
390
546
  export declare namespace useQuery {
391
547
  var ssrDisabledResult: ObservableQuery.Result<any, "complete" | "streaming" | "partial" | "empty">;
392
548
  }
@@ -16,7 +16,8 @@ import * as React from "react";
16
16
  import { asapScheduler, observeOn } from "rxjs";
17
17
  import { NetworkStatus } from "@apollo/client";
18
18
  import { maybeDeepFreeze, mergeOptions, } from "@apollo/client/utilities/internal";
19
- import { wrapHook } from "./internal/index.js";
19
+ import { skipToken } from "./constants.js";
20
+ import { useDeepMemo, wrapHook } from "./internal/index.js";
20
21
  import { useApolloClient } from "./useApolloClient.js";
21
22
  import { useSyncExternalStore } from "./useSyncExternalStore.js";
22
23
  const lastWatchOptions = Symbol();
@@ -24,20 +25,12 @@ export function useQuery(query, ...[options]) {
24
25
  "use no memo";
25
26
  return wrapHook("useQuery",
26
27
  // eslint-disable-next-line react-compiler/react-compiler
27
- useQuery_, useApolloClient(options && options.client))(query, options);
28
+ useQuery_, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
28
29
  }
29
30
  function useQuery_(query, options = {}) {
30
- const client = useApolloClient(options.client);
31
- const { skip, ssr, ...opts } = options;
32
- const watchQueryOptions = mergeOptions(client.defaultOptions.watchQuery, { ...opts, query });
33
- if (skip) {
34
- // When skipping, we set watchQueryOptions.fetchPolicy initially to
35
- // "standby", but we also need/want to preserve the initial non-standby
36
- // fetchPolicy that would have been used if not skipping.
37
- watchQueryOptions.initialFetchPolicy =
38
- options.initialFetchPolicy || options.fetchPolicy;
39
- watchQueryOptions.fetchPolicy = "standby";
40
- }
31
+ const client = useApolloClient(typeof options === "object" ? options.client : undefined);
32
+ const { ssr } = typeof options === "object" ? options : {};
33
+ const watchQueryOptions = useOptions(query, options, client.defaultOptions.watchQuery);
41
34
  function createState(previous) {
42
35
  const observable = client.watchQuery(watchQueryOptions);
43
36
  return {
@@ -68,7 +61,7 @@ function useQuery_(query, options = {}) {
68
61
  useResubscribeIfNecessary(resultData, // might get mutated during render
69
62
  observable, // might get mutated during render
70
63
  watchQueryOptions);
71
- const result = useResult(observable, resultData, options.ssr);
64
+ const result = useResult(observable, resultData, ssr);
72
65
  const obsQueryFields = React.useMemo(() => ({
73
66
  refetch: observable.refetch.bind(observable),
74
67
  fetchMore: observable.fetchMore.bind(observable),
@@ -90,6 +83,26 @@ function useQuery_(query, options = {}) {
90
83
  };
91
84
  }, [result, client, observable, previousData, obsQueryFields]);
92
85
  }
86
+ const fromSkipToken = Symbol();
87
+ function useOptions(query, options, defaultOptions) {
88
+ return useDeepMemo(() => {
89
+ if (options === skipToken) {
90
+ const opts = mergeOptions(defaultOptions, {
91
+ query,
92
+ fetchPolicy: "standby",
93
+ });
94
+ opts[fromSkipToken] = true;
95
+ return opts;
96
+ }
97
+ const watchQueryOptions = mergeOptions(defaultOptions, { ...options, query });
98
+ if (options.skip) {
99
+ watchQueryOptions.initialFetchPolicy =
100
+ options.initialFetchPolicy || options.fetchPolicy;
101
+ watchQueryOptions.fetchPolicy = "standby";
102
+ }
103
+ return watchQueryOptions;
104
+ }, [query, options, defaultOptions]);
105
+ }
93
106
  function useInitialFetchPolicyIfNecessary(watchQueryOptions, observable) {
94
107
  "use no memo";
95
108
  if (!watchQueryOptions.fetchPolicy) {
@@ -145,6 +158,14 @@ observable, watchQueryOptions) {
145
158
  "use no memo";
146
159
  if (observable[lastWatchOptions] &&
147
160
  !equal(observable[lastWatchOptions], watchQueryOptions)) {
161
+ // If skipToken was used to generate options, we won't know the correct
162
+ // initialFetchPolicy until the hook is rerendered with real options, so we
163
+ // set it the next time we get real options
164
+ if (observable[lastWatchOptions][fromSkipToken] &&
165
+ !watchQueryOptions.initialFetchPolicy) {
166
+ watchQueryOptions.initialFetchPolicy =
167
+ watchQueryOptions.fetchPolicy;
168
+ }
148
169
  // Though it might be tempting to postpone this reobserve call to the
149
170
  // useEffect block, we need getCurrentResult to return an appropriate
150
171
  // loading:true result synchronously (later within the same call to