Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApolloError.graphQLErrors are not of type GraphQLError #11787

Closed
simPod opened this issue Apr 16, 2024 · 5 comments · Fixed by #11789
Closed

ApolloError.graphQLErrors are not of type GraphQLError #11787

simPod opened this issue Apr 16, 2024 · 5 comments · Fixed by #11789
Labels

Comments

@simPod
Copy link

simPod commented Apr 16, 2024

Issue Description

I'd like to resurrect #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
Copy link
Author

simPod commented Apr 16, 2024

In tests it's done correctly

graphQLErrors: [new GraphQLError("Something went wrong")],

@phryneas
Copy link
Member

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
Copy link
Member

phryneas commented Dec 9, 2024

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

@phryneas phryneas closed this as completed Dec 9, 2024
Copy link
Contributor

github-actions bot commented Dec 9, 2024

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.

Copy link
Contributor

github-actions bot commented Jan 9, 2025

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 github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants