@apollo/client 4.2.0-alpha.0 → 4.2.0-alpha.2

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 (99) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/__cjs/core/ApolloClient.cjs +16 -3
  3. package/__cjs/core/ApolloClient.cjs.map +1 -1
  4. package/__cjs/core/ApolloClient.d.cts +180 -131
  5. package/__cjs/core/ObservableQuery.cjs +13 -1
  6. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  7. package/__cjs/core/QueryManager.cjs +2 -3
  8. package/__cjs/core/QueryManager.cjs.map +1 -1
  9. package/__cjs/core/defaultOptions.cjs +3 -0
  10. package/__cjs/core/defaultOptions.cjs.map +1 -0
  11. package/__cjs/core/defaultOptions.d.cts +90 -0
  12. package/__cjs/core/index.cjs.map +1 -1
  13. package/__cjs/core/index.d.cts +2 -1
  14. package/__cjs/react/hooks/useBackgroundQuery.cjs +3 -3
  15. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  16. package/__cjs/react/hooks/useBackgroundQuery.d.cts +254 -822
  17. package/__cjs/react/hooks/useLazyQuery.cjs +3 -5
  18. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  19. package/__cjs/react/hooks/useLazyQuery.d.cts +93 -146
  20. package/__cjs/react/hooks/useLoadableQuery.cjs +8 -4
  21. package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
  22. package/__cjs/react/hooks/useLoadableQuery.d.cts +105 -233
  23. package/__cjs/react/hooks/useQuery.cjs +9 -5
  24. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  25. package/__cjs/react/hooks/useQuery.d.cts +138 -308
  26. package/__cjs/react/hooks/useSuspenseQuery.cjs +3 -3
  27. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  28. package/__cjs/react/hooks/useSuspenseQuery.d.cts +147 -436
  29. package/__cjs/react/internal/cache/getSuspenseCache.cjs.map +1 -1
  30. package/__cjs/react/internal/cache/getSuspenseCache.d.cts +7 -0
  31. package/__cjs/utilities/internal/LazyType.cjs +3 -0
  32. package/__cjs/utilities/internal/LazyType.cjs.map +1 -0
  33. package/__cjs/utilities/internal/LazyType.d.cts +10 -0
  34. package/__cjs/utilities/internal/index.cjs.map +1 -1
  35. package/__cjs/utilities/internal/index.d.cts +3 -0
  36. package/__cjs/utilities/internal/types/OptionWithFallback.cjs +3 -0
  37. package/__cjs/utilities/internal/types/OptionWithFallback.cjs.map +1 -0
  38. package/__cjs/utilities/internal/types/OptionWithFallback.d.cts +5 -0
  39. package/__cjs/utilities/internal/types/SignatureStyle.cjs +3 -0
  40. package/__cjs/utilities/internal/types/SignatureStyle.cjs.map +1 -0
  41. package/__cjs/utilities/internal/types/SignatureStyle.d.cts +24 -0
  42. package/__cjs/version.cjs +1 -1
  43. package/core/ApolloClient.d.ts +180 -131
  44. package/core/ApolloClient.js +16 -3
  45. package/core/ApolloClient.js.map +1 -1
  46. package/core/ObservableQuery.js +13 -1
  47. package/core/ObservableQuery.js.map +1 -1
  48. package/core/QueryManager.js +3 -4
  49. package/core/QueryManager.js.map +1 -1
  50. package/core/defaultOptions.d.ts +90 -0
  51. package/core/defaultOptions.js +2 -0
  52. package/core/defaultOptions.js.map +1 -0
  53. package/core/index.d.ts +2 -1
  54. package/core/index.js.map +1 -1
  55. package/package.json +1 -1
  56. package/react/hooks/useBackgroundQuery.d.ts +254 -822
  57. package/react/hooks/useBackgroundQuery.js +2 -2
  58. package/react/hooks/useBackgroundQuery.js.map +1 -1
  59. package/react/hooks/useLazyQuery.d.ts +93 -146
  60. package/react/hooks/useLazyQuery.js +2 -4
  61. package/react/hooks/useLazyQuery.js.map +1 -1
  62. package/react/hooks/useLoadableQuery.d.ts +105 -233
  63. package/react/hooks/useLoadableQuery.js +7 -3
  64. package/react/hooks/useLoadableQuery.js.map +1 -1
  65. package/react/hooks/useQuery.d.ts +138 -308
  66. package/react/hooks/useQuery.js +7 -3
  67. package/react/hooks/useQuery.js.map +1 -1
  68. package/react/hooks/useSuspenseQuery.d.ts +147 -436
  69. package/react/hooks/useSuspenseQuery.js +2 -2
  70. package/react/hooks/useSuspenseQuery.js.map +1 -1
  71. package/react/hooks-compiled/useBackgroundQuery.d.ts +254 -822
  72. package/react/hooks-compiled/useBackgroundQuery.js +2 -2
  73. package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
  74. package/react/hooks-compiled/useLazyQuery.d.ts +93 -146
  75. package/react/hooks-compiled/useLazyQuery.js +2 -4
  76. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  77. package/react/hooks-compiled/useLoadableQuery.d.ts +105 -233
  78. package/react/hooks-compiled/useLoadableQuery.js +2 -2
  79. package/react/hooks-compiled/useLoadableQuery.js.map +1 -1
  80. package/react/hooks-compiled/useQuery.d.ts +138 -308
  81. package/react/hooks-compiled/useQuery.js +7 -3
  82. package/react/hooks-compiled/useQuery.js.map +1 -1
  83. package/react/hooks-compiled/useSuspenseQuery.d.ts +147 -436
  84. package/react/hooks-compiled/useSuspenseQuery.js +2 -2
  85. package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
  86. package/react/internal/cache/getSuspenseCache.d.ts +7 -0
  87. package/react/internal/cache/getSuspenseCache.js.map +1 -1
  88. package/utilities/internal/LazyType.d.ts +10 -0
  89. package/utilities/internal/LazyType.js +2 -0
  90. package/utilities/internal/LazyType.js.map +1 -0
  91. package/utilities/internal/index.d.ts +3 -0
  92. package/utilities/internal/index.js.map +1 -1
  93. package/utilities/internal/types/OptionWithFallback.d.ts +5 -0
  94. package/utilities/internal/types/OptionWithFallback.js +2 -0
  95. package/utilities/internal/types/OptionWithFallback.js.map +1 -0
  96. package/utilities/internal/types/SignatureStyle.d.ts +24 -0
  97. package/utilities/internal/types/SignatureStyle.js +2 -0
  98. package/utilities/internal/types/SignatureStyle.js.map +1 -0
  99. package/version.js +1 -1
package/CHANGELOG.md CHANGED
@@ -1,11 +1,181 @@
1
1
  # @apollo/client
2
2
 
3
+ ## 4.2.0-alpha.2
4
+
5
+ ### Minor Changes
6
+
7
+ - [#13132](https://github.com/apollographql/apollo-client/pull/13132) [`f3ce805`](https://github.com/apollographql/apollo-client/commit/f3ce805425d10a9666218a8e109288a2d46dcab1) Thanks [@phryneas](https://github.com/phryneas)! - Introduce "classic" and "modern" method and hook signatures.
8
+
9
+ Apollo Client 4.2 introduces two signature styles for methods and hooks. All signatures previously present are now "classic" signatures, and a new set of "modern" signatures are added alongside them.
10
+
11
+ **Classic signatures** are the default and are identical to the signatures before Apollo Client 4.2, preserving backward compatibility. Classic signatures still work with manually specified TypeScript generics (e.g., `useSuspenseQuery<MyData>(...)`). However, manually specifying generics has been discouraged for a long time—instead, we recommend using `TypedDocumentNode` to automatically infer types, which provides more accurate results without any manual annotations.
12
+
13
+ **Modern signatures** automatically incorporate your declared `defaultOptions` into return types, providing more accurate types. Modern signatures infer types from the document node and do not support manually passing generic type arguments; TypeScript will produce a type error if you attempt to do so.
14
+
15
+ Methods and hooks automatically switch to modern signatures the moment any non-optional property is declared in `DeclareDefaultOptions`. The switch happens across all methods and hooks globally:
16
+
17
+ ```ts
18
+ // apollo.d.ts
19
+ import "@apollo/client";
20
+ declare module "@apollo/client" {
21
+ namespace ApolloClient {
22
+ namespace DeclareDefaultOptions {
23
+ interface WatchQuery {
24
+ errorPolicy: "all"; // non-optional → modern signatures activated automatically
25
+ }
26
+ }
27
+ }
28
+ }
29
+ ```
30
+
31
+ Users can also manually switch to modern signatures without declaring any `defaultOptions`, for example when wanting accurate type inference without relying on global `defaultOptions`:
32
+
33
+ ```ts
34
+ // apollo.d.ts
35
+ import "@apollo/client";
36
+ declare module "@apollo/client" {
37
+ export interface TypeOverrides {
38
+ signatureStyle: "modern";
39
+ }
40
+ }
41
+ ```
42
+
43
+ Users can do a global `DeclareDefaultOptions` type augmentation and then manually switch back to "classic" for migration purposes:
44
+
45
+ ```ts
46
+ // apollo.d.ts
47
+ import "@apollo/client";
48
+ declare module "@apollo/client" {
49
+ export interface TypeOverrides {
50
+ signatureStyle: "classic";
51
+ }
52
+ }
53
+ ```
54
+
55
+ Note that this is **not recommended for long-term use**. When combined with `DeclareDefaultOptions`, switching back to classic results in the same incorrect types as before Apollo Client 4.2—methods and hooks will not reflect the `defaultOptions` you've declared.
56
+
57
+ - [#13132](https://github.com/apollographql/apollo-client/pull/13132) [`f3ce805`](https://github.com/apollographql/apollo-client/commit/f3ce805425d10a9666218a8e109288a2d46dcab1) Thanks [@phryneas](https://github.com/phryneas)! - Synchronize method and hook return types with `defaultOptions`.
58
+
59
+ Prior to this change, the following code snippet would always apply:
60
+
61
+ ```ts
62
+ declare const MY_QUERY: TypedDocumentNode<TData, TVariables>;
63
+ const result1 = useSuspenseQuery(MY_QUERY);
64
+ result1.data;
65
+ // ^? TData
66
+ const result2 = useSuspenseQuery(MY_QUERY, { errorPolicy: "all" });
67
+ result2.data;
68
+ // ^? TData | undefined
69
+ ```
70
+
71
+ While these types are generally correct, if you were to set `errorPolicy: 'all'` as a default option, the type of `result.data` for the first query would remain `TData` instead of changing to `TData | undefined` to match the runtime behavior.
72
+
73
+ We are now enforcing that certain `defaultOptions` types need to be registered globally. This means that if you want to use `errorPolicy: 'all'` as a default option for a query, you will need to register its type like this:
74
+
75
+ ```ts
76
+ // apollo.d.ts
77
+ import "@apollo/client";
78
+
79
+ declare module "@apollo/client" {
80
+ namespace ApolloClient {
81
+ namespace DeclareDefaultOptions {
82
+ interface WatchQuery {
83
+ // possible global-registered values:
84
+ // * `errorPolicy`
85
+ // * `returnPartialData`
86
+ errorPolicy: "all";
87
+ }
88
+ interface Query {
89
+ // possible global-registered values:
90
+ // * `errorPolicy`
91
+ }
92
+ interface Mutate {
93
+ // possible global-registered values:
94
+ // * `errorPolicy`
95
+ }
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ Once this type declaration is in place, the type of `result.data` in the above example will correctly be changed to `TData | undefined`, reflecting the possibility that if an error occurs, `data` might be `undefined`. Manually specifying `useSuspenseQuery(MY_QUERY, { errorPolicy: "none" });` changes `result.data` to `TData` to reflect the local override.
102
+
103
+ This change means that you will need to declare your default options types in order to use `defaultOptions` with `ApolloClient`, otherwise you will see a TypeScript error.
104
+
105
+ Without the type declaration, the following (previously valid) code will now error:
106
+
107
+ ```ts
108
+ new ApolloClient({
109
+ link: ApolloLink.empty(),
110
+ cache: new InMemoryCache(),
111
+ defaultOptions: {
112
+ watchQuery: {
113
+ // results in a type error:
114
+ // Type '"all"' is not assignable to type '"A default option for watchQuery.errorPolicy must be declared in ApolloClient.DeclareDefaultOptions before usage. See https://www.apollographql.com/docs/react/data/typescript#declaring-default-options-for-type-safety."'.
115
+ errorPolicy: "all",
116
+ },
117
+ },
118
+ });
119
+ ```
120
+
121
+ If you are creating multiple instances of Apollo Client with conflicting default options and you cannot register a single `defaultOptions` value as a result, you can relax the constraints by declaring those options as union types covering all values used by all clients. The properties can be required (to enforce them in `defaultOptions`) or optional (if some constructor calls won't pass a value):
122
+
123
+ ```ts
124
+ // apollo.d.ts
125
+ import "@apollo/client";
126
+
127
+ declare module "@apollo/client" {
128
+ export namespace ApolloClient {
129
+ export namespace DeclareDefaultOptions {
130
+ interface WatchQuery {
131
+ errorPolicy?: "none" | "all" | "ignore";
132
+ returnPartialData?: boolean;
133
+ }
134
+ interface Query {
135
+ errorPolicy?: "none" | "all" | "ignore";
136
+ }
137
+ interface Mutate {
138
+ errorPolicy?: "none" | "all" | "ignore";
139
+ }
140
+ }
141
+ }
142
+ }
143
+ ```
144
+
145
+ With this declaration, the `ApolloClient` constructor accepts any of those values in `defaultOptions`. The tradeoff is that hook and method return types become more generic. For example, calling `useSuspenseQuery` without an explicit `errorPolicy` will return a result typed as if all error policies are possible, since TypeScript can't know which specific value your instance uses at runtime.
146
+
147
+ Note that making a property optional (`errorPolicy?:`) is equivalent to adding the TypeScript default value (`"none"`) to the union. So `errorPolicy?: "all" | "ignore"` has the same effect on return types as `errorPolicy: "none" | "all" | "ignore"`, because TypeScript assumes the option could also be absent (i.e., `"none"`).
148
+
149
+ You can also use a **partial union** that only lists the values you actually use. For example, if you only ever use `"all"` or `"ignore"`, declare `errorPolicy: "all" | "ignore"` (required) to keep the union narrow and avoid unused values broadening your signatures unnecessarily.
150
+
151
+ ## 4.2.0-alpha.1
152
+
153
+ ### Patch Changes
154
+
155
+ - [#13166](https://github.com/apollographql/apollo-client/pull/13166) [`0537d97`](https://github.com/apollographql/apollo-client/commit/0537d97161a51479141a182d869458912e1b8e1d) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Release changes in 4.1.5 and 4.1.6.
156
+
3
157
  ## 4.2.0-alpha.0
4
158
 
5
159
  ### Minor Changes
6
160
 
7
161
  - [#13130](https://github.com/apollographql/apollo-client/pull/13130) [`dd12231`](https://github.com/apollographql/apollo-client/commit/dd122316028b55307de4a40335512307c8fa916a) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Improve the accuracy of `client.query` return type to better detect the current `errorPolicy`. The `data` property is no longer nullable when the `errorPolicy` is `none`. This makes it possible to remove the `undefined` checks or optional chaining in most cases.
8
162
 
163
+ ## 4.1.6
164
+
165
+ ### Patch Changes
166
+
167
+ - [#13128](https://github.com/apollographql/apollo-client/pull/13128) [`6c0b8e4`](https://github.com/apollographql/apollo-client/commit/6c0b8e4301609b62ed599340589c978e4f51f020) Thanks [@pavelivanov](https://github.com/pavelivanov)! - Fix `useQuery` hydration mismatch when `ssr: false` and `skip: true` are used together
168
+
169
+ When both options were combined, the server would return `loading: false` (because `useSSRQuery` checks `skip` first), but the client's `getServerSnapshot` was returning `ssrDisabledResult` with `loading: true`, causing a hydration mismatch.
170
+
171
+ ## 4.1.5
172
+
173
+ ### Patch Changes
174
+
175
+ - [#13155](https://github.com/apollographql/apollo-client/pull/13155) [`3ba1583`](https://github.com/apollographql/apollo-client/commit/3ba1583f93c40343501acd9d598ce506537d1c9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix an issue where `useQuery` would poll with `pollInterval` when `skip` was initialized to `true`.
176
+
177
+ - [#13135](https://github.com/apollographql/apollo-client/pull/13135) [`fd42142`](https://github.com/apollographql/apollo-client/commit/fd42142495d24859a9bc7145a85bc8f8d857ec88) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix issue where `client.query` would apply options from `defaultOptions.watchQuery`.
178
+
9
179
  ## 4.1.4
10
180
 
11
181
  ### Patch Changes
@@ -84,7 +84,11 @@ class ApolloClient {
84
84
  this.link = link;
85
85
  this.cache = cache;
86
86
  this.queryDeduplication = queryDeduplication;
87
- this.defaultOptions = defaultOptions || {};
87
+ // `DefaultOptions.Input` and `DefaultOptions` are inherently incompatible,
88
+ // as the `.Input` type restricts certain values from being passed in if not
89
+ // configured globally.
90
+ // Without those, it's compatible though so it is fine to cast.
91
+ this.defaultOptions = (defaultOptions || {});
88
92
  this.devtoolsConfig = {
89
93
  ...devtools,
90
94
  enabled: devtools?.enabled ?? environment_1.__DEV__,
@@ -225,7 +229,16 @@ class ApolloClient {
225
229
  }
226
230
  return this.queryManager.watchQuery(options);
227
231
  }
228
- query(options) {
232
+ /**
233
+ * This resolves a single query according to the options specified and
234
+ * returns a `Promise` which is either resolved with the resulting data
235
+ * or rejected with an error.
236
+ *
237
+ * @param options - An object of type `QueryOptions` that allows us to
238
+ * describe how this query should be treated e.g. whether it should hit the
239
+ * server at all or just resolve from the cache, etc.
240
+ */
241
+ query = (options) => {
229
242
  if (this.defaultOptions.query) {
230
243
  options = (0, internal_1.mergeOptions)(this.defaultOptions.query, options);
231
244
  }
@@ -239,7 +252,7 @@ class ApolloClient {
239
252
  (0, invariant_1.invariant)(!options.notifyOnNetworkStatusChange, 76);
240
253
  }
241
254
  return this.queryManager.query(options);
242
- }
255
+ };
243
256
  /**
244
257
  * This resolves a single mutation according to the options specified and returns a
245
258
  * Promise which is either resolved with the resulting data or rejected with an