CombinedGraphQLErrors
API reference
Represents the combined list of GraphQL errors returned from the server in a
GraphQL response. This error type is used when your GraphQL operation returns
errors in the errors field of the response.
When your GraphQL operation encounters errors on the server side (such as
resolver errors, validation errors, or syntax errors), the server returns
these errors in the errors array of the GraphQL response. Apollo Client
wraps these errors in a CombinedGraphQLErrors object, which provides access
to the individual errors while maintaining additional context about the
response.
1 import { CombinedGraphQLErrors } from "@apollo/client/errors";
2
3 // Check if an error is a CombinedGraphQLErrors object
4 if (CombinedGraphQLErrors.is(error)) {
5 // Access individual GraphQL errors
6 error.errors.forEach((graphQLError) => {
7 console.log(graphQLError.message);
8 console.log(graphQLError.path);
9 console.log(graphQLError.locations);
10 });
11
12 // Access the original GraphQL result
13 console.log(error.result);
14 }Providing a custom message formatter
By default, CombinedGraphQLErrors formats the message property by
joining each error's message field with a newline. To customize the
format of the message, such as changing the delimiter or adding a message
prefix, override the static formatMessage method.
The following example demonstrates how to format the error message by joining each error with a comma.
1import { CombinedGraphQLErrors } from "@apollo/client/errors";
2
3CombinedGraphQLErrors.formatMessage = (errors) => {
4 return errors.map((error) => error.message).join(", ");
5};See the formatMessage docs for details about the parameters provided to the formatMessage function.
ApolloClient instance.Using the default message formatter
To format part of the message using the default message formatter, call
the defaultFormatMessage function provided to the options argument of
your message formatter.
The following example prepends a string to the message and uses the default message formatter to format the error messages.
1CombinedGraphQLErrors.formatMessage = (errors, { defaultFormatMessage }) => {
2 return `[GraphQL errors]: ${defaultFormatMessage(errors)}`;
3};Static methods
A method that determines whether an error is a CombinedGraphQLErrors
object. This method enables TypeScript to narrow the error type.
Example
1 if (CombinedGraphQLErrors.is(error)) {
2 // TypeScript now knows `error` is a `CombinedGraphQLErrors` object
3 console.log(error.errors);
4 }Signature
1is(
2 error: unknown
3): error is CombinedGraphQLErrorsSee the instance properties for more details about the available properties provided by the CombinedGraphQLErrors object.
A function that formats the error message used for the error's message
property. Override this method to provide your own formatting.
The formatMessage function is called by the CombinedGraphQLErrors
constructor to provide a formatted message as the message property of the
CombinedGraphQLErrors object. Follow the "Providing a custom message
formatter" guide to learn how to modify the message format.
Signature
1formatMessage(
2 errors: ReadonlyArray<GraphQLFormattedError>,
3 options: MessageFormatterOptions
4): stringParameters
(errors: ReadonlyArray<GraphQLFormattedError>) => stringThe default message formatter. Call this to get a string with the default formatted message.
Read more...
To format part of the message using the default message formatter, call
the defaultFormatMessage function provided to the options argument of
your message formatter.
The following example prepends a string to the message and uses the default message formatter to format the error messages.
1CombinedGraphQLErrors.formatMessage = (errors, { defaultFormatMessage }) => {
2 return `[GraphQL errors]: ${defaultFormatMessage(errors)}`;
3};ApolloLink.Result<unknown>The raw result returned from the server.
Instance properties
These properties are specific to the CombinedGraphQLErrors object. Standard error instance properties are also available.
Record<string, unknown> | null | undefinedPartial data returned in the data field of the GraphQL response.
ReadonlyArray<GraphQLFormattedError>The raw list of GraphQL errors returned by the errors field in the GraphQL response.
Record<string, unknown> | undefinedExtensions returned by the extensions field in the GraphQL response.