Skip to content

Implement equals/hashCode for GraphqlErrorImpl #3720

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

Conversation

AlexandreCarlton
Copy link
Contributor

We have a great many tests that verify that the errors emitted from a DataFetcher fit a certain shape.

However, verifying equality of these errors is fiddly and error-prone, as we must directly check each individual attribute on every error - this is painful especially when we are trying to perform assertions on a List of GraphQLErrors.

To this end, we add #equals / #hashCode methods on GraphqlErrorImpl. However, it is important to note that equals will return true if all the attributes match, even if the implementing class is not a GraphqlErrorImpl. This is done so that different implementations may be swapped in and out with causing test failures.

We have a great many tests that verify that the errors emitted from a
`DataFetcher` fit a certain shape.

However, verifying equality of these errors is fiddly and error-prone,
as we must directly check each individual attribute on every error -
this is painful especially when we are trying to perform assertions on a
`List` of `GraphQLError`s.

To this end, we add `#equals` / `#hashCode` methods on
`GraphqlErrorImpl`. However, it is important to note that `equals` will
return `true` if all the attributes match, even if the implementing
class is _not_ a `GraphqlErrorImpl`. This is done so that different
implementations may be swapped in and out with causing test failures.
GraphQLError that = (GraphQLError) o;
return Objects.equals(getMessage(), that.getMessage())
&& Objects.equals(getLocations(), that.getLocations())
&& Objects.equals(getErrorType(), that.getErrorType())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graphql.ErrorClassification is an interface so this is a place where technically a bad graphql.ErrorClassification would break you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now added this in the Javadoc - is there another way you would like this to be handled?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No just articulating a challenge

errors == [error2, error3] as Set
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we have some more tests cases with the other attrs such as source location, path say please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've structured this in such a way that should make it easy to add more tests if the existing coverage is not enough.

@dondonz dondonz added this to the 23.x breaking changes milestone Nov 13, 2024
This now tests the other attributes, and also checks inequality
explicitly.
Copy link
Member

@bbakerman bbakerman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution

@bbakerman bbakerman added this pull request to the merge queue Nov 16, 2024
Merged via the queue into graphql-java:master with commit 4703692 Nov 16, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants