apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

`ApolloError.graphQLErrors` are not of type `GraphQLError`

Open simPod opened this issue 1 year ago • 2 comments

Issue Description

I'd like to resurrect https://github.com/apollographql/apollo-client/issues/11168 since issue got stale and closed.

As you can see in the graphql spec, the errors returned by the server are not required to specify e.g. extensions key.

However,

import type { GraphQLError, GraphQLErrorExtensions } from "graphql";

...

export type GraphQLErrors = ReadonlyArray<GraphQLError>;

export class ApolloError extends Error {
  ...
  public graphQLErrors: GraphQLErrors;

if you look at the GraphQLError class, the extensions field is not optional

https://github.com/graphql/graphql-js/blob/8e9813f8c283d94da66fad6fd9562432846c17d4/src/error/GraphQLError.ts#L89

  /**
   * Extension fields to add to the formatted error.
   */
  readonly extensions: GraphQLErrorExtensions;

That is achieved by taking server response and routing it through GraphQLError class' constructor:

https://github.com/graphql/graphql-js/blob/8e9813f8c283d94da66fad6fd9562432846c17d4/src/error/GraphQLError.ts#L125

    this.extensions = extensions ?? originalExtensions ?? Object.create(null);

So that implies that using GraphQLError[] as a type for ApolloError.graphQLErrors is wrong since the server data were not manipulated and may not comply with the GraphQLError interface.

Link to Reproduction

https://spec.graphql.org/October2021/#sec-Errors.Error-result-format

Reproduction Steps

No response

@apollo/client version

3.9.1

simPod avatar Apr 16 '24 11:04 simPod

In tests it's done correctly https://github.com/apollographql/apollo-client/blob/65b4ba91de2415106e2327ae9f476c1d6f28f7e4/src/react/hooks/tests/useBackgroundQuery.test.tsx#L4395

simPod avatar Apr 16 '24 12:04 simPod

Thank you, that's a very good writeup and it's clear that we have to change something.

We're still undecided if we'll solve this in runtime or by adjusting our types, but I've opened #11789 to help us along with the decision process here.

phryneas avatar Apr 17 '24 13:04 phryneas

We adopted GraphQLFormattedError, so I believe we can close this.

phryneas avatar Dec 09 '24 08:12 phryneas

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] avatar Dec 09 '24 08:12 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using our Community Forum or Stack Overflow.

github-actions[bot] avatar Jan 09 '25 00:01 github-actions[bot]