Major Changes
-
#12539
dd0d6d6Thanks @jerelmiller! -onErrorlink now uses a singleerrorproperty to report the error that caused the link callback to be called. This will be an instance ofCombinedGraphQLErrorsin the event GraphQL errors were emitted from the terminating link,CombinedProtocolErrorsif the terminating link emitted protocol errors, or the unwrapped error type if any other non-GraphQL error was thrown or emitted.- const errorLink = onError(({ graphQLErrors, networkError, protocolErrors }) => { - graphQLErrors.forEach(error => console.log(error.message)); + const errorLink = onError(({ error }) => { + if (error.name === 'CombinedGraphQLErrors') { + error.errors.forEach(rawError => console.log(rawError.message)); + } });
-
#12533
73221d8Thanks @jerelmiller! - Remove theonErrorandsetOnErrormethods fromApolloLink.onErrorwas only used byMockLinkto rewrite errors ifsetOnErrorwas used. -
#12531
7784b46Thanks @jerelmiller! - Mocked responses passed toMockLinknow accept a callback for therequest.variablesoption. This is used to determine if the mock should be matched for a set of request variables. With this change, thevariableMatcheroption has been removed in favor of passing a callback tovariables. Update by moving the callback function fromvariableMatchertorequest.variables.new MockLink([ { request: { query, + variables: (requestVariables) => true }, - variableMatcher: (requestVariables) => true } ]); -
#12526
391af1dThanks @phryneas! - The@apollo/clientand@apollo/client/coreentry points are now equal.
In the next major, the@apollo/client/coreentry point will be removed.
Please change imports over from@apollo/client/coreto@apollo/client. -
#12525
8785186Thanks @jerelmiller! - Throw an error when a client-only query is used in a mocked response passed toMockLink. -
#12532
ae0dcadThanks @jerelmiller! - Default thedelayfor all mocked responses passed toMockLinkusingrealisticDelay. This ensures your test handles loading states by default and is not reliant on a specific timing.If you would like to restore the old behavior, use a global default delay of
0.MockLink.defaultOptions = { delay: 0, };
-
#12530
2973e2aThanks @jerelmiller! - RemovenewDataoption for mocked responses passed toMockLinkor themocksoption onMockedProvider. This option was undocumented and was nearly identical to using theresultoption as a callback.To replicate the old behavior of
newData, useresultas a callback and add themaxUsageCountoption with a value set toNumber.POSITIVE_INFINITY.with
MockLinknew MockLink([ { request: { query, variables }, - newData: (variables) => ({ data: { greeting: "Hello " + variables.greeting } }), + result: (variables) => ({ data: { greeting: "Hello " + variables.greeting } }), + maxUsageCount: Number.POSITIVE_INFINITY, } ])with
MockedProvider<MockedProvider mocks={[ { request: { query, variables }, - newData: (variables) => ({ data: { greeting: "Hello " + variables.greeting } }), + result: (variables) => ({ data: { greeting: "Hello " + variables.greeting } }), + maxUsageCount: Number.POSITIVE_INFINITY, } ]} />
Minor Changes
-
#12532
ae0dcadThanks @jerelmiller! - Allow mocked responses passed toMockLinkto accept a callback for thedelayoption. Thedelaycallback will be given the current operation which can be used to determine what delay should be used for the mock. -
#12532
ae0dcadThanks @jerelmiller! - Introduce a newrealisticDelayhelper function for use with thedelaycallback for mocked responses used withMockLink.realisticDelaywill generate a random value between 20 and 50ms to provide an experience closer to unpredictable network latency.realisticDelaycan be configured with aminandmaxto set different thresholds if the defaults are not sufficient.import { realisticDelay } from "@apollo/client/testing"; new MockLink([ { request: { query }, result: { data: { greeting: "Hello" } }, delay: realisticDelay(), }, { request: { query }, result: { data: { greeting: "Hello" } }, delay: realisticDelay({ min: 10, max: 100 }), }, ]);
-
#12532
ae0dcadThanks @jerelmiller! - Add ability to specify a defaultdelayfor all mocked responses passed toMockLink. Thisdelaycan be configured globally (all instances ofMockLinkwill use the global defaults), or per-instance (all mocks in a single instance will use the defaults). Adelaydefined on a single mock will supercede all default delays. Per-instance defaults supercede global defaults.Global defaults
MockLink.defaultOptions = { // Use a default delay of 20ms for all mocks in all instances without a specified delay delay: 20, // altenatively use a callback which will be executed for each mock delay: () => getRandomNumber(), // or use the built-in `realisticDelay`. This is the default delay: realisticDelay(), };
Per-instance defaults
new MockLink( [ // Use the default delay { request: { query }, result: { data: { greeting: "Hello" } }, }, { request: { query }, result: { data: { greeting: "Hello" } }, // Override the default for this mock delay: 10, }, ], { defaultOptions: { // Use a default delay of 20ms for all mocks without a specified delay delay: 20, // altenatively use a callback which will be executed for each mock delay: () => getRandomNumber(), // or use the built-in `realisticDelay`. This is the default delay: realisticDelay(), }, } );