v3.3.0
版本发布时间: 2020-11-25 05:57:47
apollographql/apollo-client最新发布版本:v3.12.0-alpha.0(2024-10-02 04:16:11)
Apollo Client 3.3.0
Bug Fixes
-
Update
@wry/equality
to consider undefined properties equivalent to missing properties. @benjamn in #7108 -
Prevent memory leaks involving unused
onBroadcast
function closure created inApolloClient
constructor. @kamilkisiela in #7161 -
Provide default empty cache object for root IDs like
ROOT_QUERY
, to avoid differences in behavior before/afterROOT_QUERY
data has been written intoInMemoryCache
. @benjamn in #7100 -
Cancel
queryInfo.notifyTimeout
inQueryInfo#markResult
to prevent unnecessary network requests when using aFetchPolicy
ofcache-and-network
ornetwork-only
in a React component with multipleuseQuery
calls. @benjamn in #7347
Potentially breaking changes
-
Ensure
cache.readQuery
andcache.readFragment
always returnTData | null
, instead of throwingMissingFieldError
exceptions when missing fields are encountered. @benjamn in #7098Since this change converts prior exceptions to
null
returns, and sincenull
was already a possible return value according to theTData | null
return type, we are confident this change will be backwards compatible (as long asnull
was properly handled before). -
HttpLink
will now automatically strip any unusedvariables
before sending queries to the GraphQL server, since those queries are very likely to fail validation, according to the All Variables Used rule in the GraphQL specification. If you depend on the preservation of unused variables, you can restore the previous behavior by passingincludeUnusedVariables: true
to theHttpLink
constructor (which is typically passed asoptions.link
to theApolloClient
constructor). @benjamn in #7127 -
Ensure
MockLink
(used byMockedProvider
) returns mock configuration errors (e.g.No more mocked responses for the query ...
) through the Link'sObservable
, instead of throwing them. These errors are now available through theerror
property of a result. @hwillson in #7110Returning mock configuration errors through the Link's
Observable
was the default behavior in Apollo Client 2.x. We changed it for 3, but the change has been problematic for those looking to migrate from 2.x to 3. We've decided to change this back with the understanding that not many people want or are relying onMockLink
's throwing exception approach. If you want to change this functionality, you can define custom error handling throughMockLink.setOnError
. -
Unsubscribing the last observer from an
ObservableQuery
will once again unsubscribe from the underlying networkObservable
in all cases, as in Apollo Client 2.x, allowing network requests to be cancelled by unsubscribing. @javier-garcia-meteologica in #7165 and #7170. -
The independent
QueryBaseOptions
andModifiableWatchQueryOptions
interface supertypes have been eliminated, and their fields are now defined byQueryOptions
. @DCtheTall in #7136 -
Internally, Apollo Client now avoids nested imports from the
graphql
package, importing everything from the top-level package instead. For example,import { visit } from "graphql/language/visitor"
is now just
import { visit } from "graphql"
Since the
graphql
package uses.mjs
modules, your bundler may need to be configured to recognize.mjs
files as ECMAScript modules rather than CommonJS modules. @benjamn in #7185
Improvements
-
Support inheritance of type and field policies, according to
possibleTypes
. @benjamn in #7065 -
Allow configuring custom
merge
functions, including themerge: true
andmerge: false
shorthands, in type policies as well as field policies. @benjamn in #7070 -
The verbosity of Apollo Client console messages can be globally adjusted using the
setLogVerbosity
function:import { setLogVerbosity } from "@apollo/client"; setLogVerbosity("log"); // display all messages setLogVerbosity("warn"); // display only warnings and errors (default) setLogVerbosity("error"); // display only errors setLogVerbosity("silent"); // hide all console messages
Remember that all logs, warnings, and errors are hidden in production. @benjamn in #7226
-
Modifying
InMemoryCache
fields that havekeyArgs
configured will now invalidate only the field value with matching key arguments, rather than invalidating all field values that share the same field name. IfkeyArgs
has not been configured, the cache must err on the side of invalidating by field name, as before. @benjamn in #7351 -
Shallow-merge
options.variables
when combining existing or default options with newly-provided options, so new variables do not completely overwrite existing variables. @amannn in #6927 -
Avoid displaying
Cache data may be lost...
warnings for scalar field values that happen to be objects, such as JSON data. @benjamn in #7075 -
In addition to the
result.data
property,useQuery
anduseLazyQuery
will now provide aresult.previousData
property, which can be useful when a network request is pending andresult.data
is undefined, sinceresult.previousData
can be rendered instead of rendering an empty/loading state. @hwillson in #7082 -
Passing
validate: true
to theSchemaLink
constructor will enable validation of incoming queries against the local schema before execution, returning validation errors inresult.errors
, just like a non-local GraphQL endpoint typically would. @amannn in #7094 -
Allow optional arguments in
keyArgs: [...]
arrays forInMemoryCache
field policies. @benjamn in #7109 -
Avoid registering
QueryPromise
whenskip
istrue
during server-side rendering. @izumin5210 in #7310 -
ApolloCache
objects (includingInMemoryCache
) may now be associated with or disassociated from individual reactive variables by callingreactiveVar.attachCache(cache)
and/orreactiveVar.forgetCache(cache)
. @benjamn in #7350