Implement equals/hashCode for GraphqlErrorImpl#3720
Merged
bbakerman merged 2 commits intographql-java:masterfrom Nov 16, 2024
Merged
Conversation
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.
bbakerman
reviewed
Oct 12, 2024
| GraphQLError that = (GraphQLError) o; | ||
| return Objects.equals(getMessage(), that.getMessage()) | ||
| && Objects.equals(getLocations(), that.getLocations()) | ||
| && Objects.equals(getErrorType(), that.getErrorType()) |
Member
There was a problem hiding this comment.
graphql.ErrorClassification is an interface so this is a place where technically a bad graphql.ErrorClassification would break you
Contributor
Author
There was a problem hiding this comment.
I've now added this in the Javadoc - is there another way you would like this to be handled?
Member
There was a problem hiding this comment.
No just articulating a challenge
bbakerman
reviewed
Oct 12, 2024
| errors == [error1, error3] as Set | ||
| errors == [error2, error3] as Set | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
could we have some more tests cases with the other attrs such as source location, path say please
Contributor
Author
There was a problem hiding this comment.
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.
This now tests the other attributes, and also checks inequality explicitly.
bbakerman
approved these changes
Nov 16, 2024
Member
bbakerman
left a comment
There was a problem hiding this comment.
Thanks for your contribution
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We have a great many tests that verify that the errors emitted from a
DataFetcherfit 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
ListofGraphQLErrors.To this end, we add
#equals/#hashCodemethods onGraphqlErrorImpl. However, it is important to note thatequalswill returntrueif all the attributes match, even if the implementing class is not aGraphqlErrorImpl. This is done so that different implementations may be swapped in and out with causing test failures.