Skip to content

Commit 5d605ed

Browse files
authored
Merge branch 'release-4.0' into pr/fix-type-exports
2 parents 73566d2 + 51d26ae commit 5d605ed

20 files changed

+594
-37
lines changed

.api-reports/api-report-errors.api.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,47 @@ import type { FetchResult } from '@apollo/client/link/core';
99
import type { FetchResult as FetchResult_2 } from '@apollo/client';
1010
import type { GraphQLFormattedError } from 'graphql';
1111

12+
// @public (undocumented)
13+
export namespace CombinedGraphQLErrors {
14+
// (undocumented)
15+
export type MessageFormatter = (errors: ReadonlyArray<GraphQLFormattedError>, options: MessageFormatterOptions) => string;
16+
// (undocumented)
17+
export interface MessageFormatterOptions {
18+
// (undocumented)
19+
defaultFormatMessage: (errors: ReadonlyArray<GraphQLFormattedError>) => string;
20+
// (undocumented)
21+
result: FetchResult_2<unknown>;
22+
}
23+
}
24+
1225
// @public
1326
export class CombinedGraphQLErrors extends Error {
1427
constructor(result: FetchResult_2<unknown>);
1528
readonly data: Record<string, unknown> | null | undefined;
1629
readonly errors: ReadonlyArray<GraphQLFormattedError>;
30+
static formatMessage: CombinedGraphQLErrors.MessageFormatter;
31+
static is(error: unknown): error is CombinedGraphQLErrors;
32+
}
33+
34+
// @public (undocumented)
35+
export namespace CombinedProtocolErrors {
36+
// (undocumented)
37+
export type MessageFormatter = (errors: ReadonlyArray<GraphQLFormattedError>, options: MessageFormatterOptions) => string;
38+
// (undocumented)
39+
export interface MessageFormatterOptions {
40+
// (undocumented)
41+
defaultFormatMessage: (errors: ReadonlyArray<GraphQLFormattedError>) => string;
42+
}
1743
}
1844

1945
// @public
2046
export class CombinedProtocolErrors extends Error {
2147
constructor(protocolErrors: Array<GraphQLFormattedError> | ReadonlyArray<GraphQLFormattedError>);
2248
// (undocumented)
2349
errors: ReadonlyArray<GraphQLFormattedError>;
50+
// (undocumented)
51+
static formatMessage: CombinedProtocolErrors.MessageFormatter;
52+
static is(error: unknown): error is CombinedProtocolErrors;
2453
}
2554

2655
// @public (undocumented)
@@ -43,6 +72,7 @@ export const PROTOCOL_ERRORS_SYMBOL: unique symbol;
4372
export class ServerError extends Error {
4473
// Warning: (ae-forgotten-export) The symbol "ServerErrorOptions" needs to be exported by the entry point index.d.ts
4574
constructor(message: string, options: ServerErrorOptions);
75+
static is(error: unknown): error is ServerError;
4676
response: Response;
4777
result: Record<string, any> | string;
4878
statusCode: number;
@@ -61,6 +91,7 @@ export class ServerParseError extends Error {
6191
// Warning: (ae-forgotten-export) The symbol "ServerParseErrorOptions" needs to be exported by the entry point index.d.ts
6292
constructor(originalParseError: unknown, options: ServerParseErrorOptions);
6393
bodyText: string;
94+
static is(error: unknown): error is ServerParseError;
6495
response: Response;
6596
statusCode: number;
6697
}
@@ -79,6 +110,7 @@ export function toErrorLike(error: unknown): ErrorLike;
79110
// @public
80111
export class UnconventionalError extends Error {
81112
constructor(errorType: unknown);
113+
static is(error: unknown): error is UnconventionalError;
82114
}
83115

84116
// (No @packageDocumentation comment for this package)

.api-reports/api-report.api.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,47 @@ type CombineByTypeName<T extends {
390390
[TypeName in NonNullable<T["__typename"]>]: Prettify<MergeUnions<ExtractByMatchingTypeNames<T, TypeName>>>;
391391
}[NonNullable<T["__typename"]>];
392392

393+
// @public (undocumented)
394+
export namespace CombinedGraphQLErrors {
395+
// (undocumented)
396+
export type MessageFormatter = (errors: ReadonlyArray<GraphQLFormattedError>, options: MessageFormatterOptions) => string;
397+
// (undocumented)
398+
export interface MessageFormatterOptions {
399+
// (undocumented)
400+
defaultFormatMessage: (errors: ReadonlyArray<GraphQLFormattedError>) => string;
401+
// (undocumented)
402+
result: FetchResult<unknown>;
403+
}
404+
}
405+
393406
// @public
394407
export class CombinedGraphQLErrors extends Error {
395408
constructor(result: FetchResult<unknown>);
396409
readonly data: Record<string, unknown> | null | undefined;
397410
readonly errors: ReadonlyArray<GraphQLFormattedError>;
411+
static formatMessage: CombinedGraphQLErrors.MessageFormatter;
412+
static is(error: unknown): error is CombinedGraphQLErrors;
413+
}
414+
415+
// @public (undocumented)
416+
export namespace CombinedProtocolErrors {
417+
// (undocumented)
418+
export type MessageFormatter = (errors: ReadonlyArray<GraphQLFormattedError>, options: MessageFormatterOptions) => string;
419+
// (undocumented)
420+
export interface MessageFormatterOptions {
421+
// (undocumented)
422+
defaultFormatMessage: (errors: ReadonlyArray<GraphQLFormattedError>) => string;
423+
}
398424
}
399425

400426
// @public
401427
export class CombinedProtocolErrors extends Error {
402428
constructor(protocolErrors: Array<GraphQLFormattedError> | ReadonlyArray<GraphQLFormattedError>);
403429
// (undocumented)
404430
errors: ReadonlyArray<GraphQLFormattedError>;
431+
// (undocumented)
432+
static formatMessage: CombinedProtocolErrors.MessageFormatter;
433+
static is(error: unknown): error is CombinedProtocolErrors;
405434
}
406435

407436
// Warning: (ae-forgotten-export) The symbol "CombineByTypeName" needs to be exported by the entry point index.d.ts
@@ -2154,6 +2183,7 @@ export const serializeFetchParameter: (p: any, label: string) => string;
21542183
export class ServerError extends Error {
21552184
// Warning: (ae-forgotten-export) The symbol "ServerErrorOptions" needs to be exported by the entry point index.d.ts
21562185
constructor(message: string, options: ServerErrorOptions);
2186+
static is(error: unknown): error is ServerError;
21572187
response: Response;
21582188
result: Record<string, any> | string;
21592189
statusCode: number;
@@ -2172,6 +2202,7 @@ export class ServerParseError extends Error {
21722202
// Warning: (ae-forgotten-export) The symbol "ServerParseErrorOptions" needs to be exported by the entry point index.d.ts
21732203
constructor(originalParseError: unknown, options: ServerParseErrorOptions);
21742204
bodyText: string;
2205+
static is(error: unknown): error is ServerParseError;
21752206
response: Response;
21762207
statusCode: number;
21772208
}
@@ -2337,6 +2368,7 @@ export type TypePolicy = {
23372368
// @public
23382369
export class UnconventionalError extends Error {
23392370
constructor(errorType: unknown);
2371+
static is(error: unknown): error is UnconventionalError;
23402372
}
23412373

23422374
// @public (undocumented)

.changeset/cool-kiwis-hunt.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
Add ability to specify message formatter for `CombinedGraphQLErrors` and `CombinedProtocolErrors`. To provide your own message formatter, override the static `formatMessage` property on these classes.
6+
7+
```ts
8+
CombinedGraphQLErrors.formatMessage = (errors, { result, defaultFormatMessage }) => {
9+
return "Some formatted message"
10+
};
11+
12+
CombinedProtocolErrors.formatMessage = (errors, { defaultFormatMessage }) => {
13+
return "Some formatted message"
14+
};
15+
```

.changeset/four-countries-clean.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
Add a static `is` method to error types defined by Apollo Client. `is` makes it simpler to determine whether an error is a specific type, which can be helpful in cases where you'd like to narrow the error type in order to use specific properties from that error.
6+
7+
This change applies to the following error types:
8+
- `CombinedGraphQLErrors`
9+
- `CombinedProtocolErrors`
10+
- `ServerError`
11+
- `ServerParseError`
12+
- `UnconventionalError`
13+
14+
**Example**
15+
16+
```ts
17+
import { CombinedGraphQLErrors } from "@apollo/client";
18+
19+
if (CombinedGraphQLErrors.is(error)) {
20+
console.log(error.message);
21+
error.errors.forEach((graphQLError) => console.log(graphQLError.message))
22+
}
23+
```
24+

.changeset/seven-dragons-repair.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Update format of the error message for `CombinedGraphQLErrors` and `CombinedProtocolErrors` to be more like v3.x.
6+
7+
```diff
8+
console.log(error.message);
9+
- `The GraphQL server returned with errors:
10+
- - Email not found
11+
- - Username already in use`
12+
+ `Email not found
13+
+ Username already in use`
14+
```

.size-limits.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 42972,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38452,
4-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 32914,
5-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27847
2+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43116,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38560,
4+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33026,
5+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27953
66
}

integration-tests/next/src/libs/apolloClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const APOLLO_STATE_PROP_NAME = "__APOLLO_STATE__";
2020
let apolloClient: ApolloClient;
2121

2222
const errorLink = onError(({ error }) => {
23-
if (error instanceof CombinedGraphQLErrors) {
23+
if (CombinedGraphQLErrors.is(error)) {
2424
error.errors.forEach(({ message, locations, path }) =>
2525
console.log(
2626
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`

src/config/jest/areCombinedGraphQLErrorsEqual.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { Tester } from "@jest/expect-utils";
22

3+
import { CombinedGraphQLErrors } from "@apollo/client";
4+
35
export const areCombinedGraphQLErrorsEqual: Tester = function (
46
a,
57
b,
68
customTesters
79
) {
8-
const isACombinedGraphQLErrors = a && a.name === "CombinedGraphQLErrors";
9-
const isBCombinedGraphQLErrors = b && b.name === "CombinedGraphQLErrors";
10+
const isACombinedGraphQLErrors = CombinedGraphQLErrors.is(a);
11+
const isBCombinedGraphQLErrors = CombinedGraphQLErrors.is(b);
1012

1113
if (isACombinedGraphQLErrors && isBCombinedGraphQLErrors) {
1214
return (

src/config/jest/areCombinedProtocolErrorsEqual.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { Tester } from "@jest/expect-utils";
22

3+
import { CombinedProtocolErrors } from "@apollo/client";
4+
35
export const areCombinedProtocolErrorsEqual: Tester = function (
46
a,
57
b,
68
customTesters
79
) {
8-
const isACombinedProtocolErrors = a && a.name === "CombinedProtocolErrors";
9-
const isBCombinedProtocolErrors = b && b.name === "CombinedProtocolErrors";
10+
const isACombinedProtocolErrors = CombinedProtocolErrors.is(a);
11+
const isBCombinedProtocolErrors = CombinedProtocolErrors.is(b);
1012

1113
if (isACombinedProtocolErrors && isBCombinedProtocolErrors) {
1214
return (

src/config/jest/areServerErrorsEqual.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Tester } from "@jest/expect-utils";
22

3+
import { ServerError } from "@apollo/client";
4+
35
export const areServerErrorsEqual: Tester = function (a, b, customTesters) {
4-
const isAServerError = a && a.name === "ServerError";
5-
const isBServerError = b && b.name === "ServerError";
6+
const isAServerError = ServerError.is(a);
7+
const isBServerError = ServerError.is(b);
68

79
if (isAServerError && isBServerError) {
810
return (

0 commit comments

Comments
 (0)