Skip to content

Commit 4afab48

Browse files
committed
graphql-java#253 - hash code and equals on error helper
1 parent b549805 commit 4afab48

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

src/main/java/graphql/ExceptionWhileDataFetching.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ public String toString() {
3939
"exception=" + exception +
4040
'}';
4141
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
48+
ExceptionWhileDataFetching that = (ExceptionWhileDataFetching) o;
49+
50+
return Helper.equals(this, that);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Helper.hashCode(this);
56+
}
4257
}

src/main/java/graphql/GraphQLError.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,55 @@ public interface GraphQLError {
1313

1414
ErrorType getErrorType();
1515

16+
17+
/**
18+
* This little helper allows GraphQlErrors to implement
19+
* common things (hashcode/ equals) more easily
20+
*/
21+
@SuppressWarnings("SimplifiableIfStatement")
22+
class Helper {
23+
24+
String message;
25+
List<SourceLocation> locations;
26+
ErrorType errorType;
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
33+
Helper helper = (Helper) o;
34+
35+
if (message != null ? !message.equals(helper.message) : helper.message != null) return false;
36+
if (locations != null ? !locations.equals(helper.locations) : helper.locations != null) return false;
37+
return errorType == helper.errorType;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
int result = message != null ? message.hashCode() : 0;
43+
result = 31 * result + (locations != null ? locations.hashCode() : 0);
44+
result = 31 * result + errorType.hashCode();
45+
return result;
46+
}
47+
48+
public static int hashCode(GraphQLError dis) {
49+
int result = dis.getMessage() != null ? dis.getMessage().hashCode() : 0;
50+
result = 31 * result + (dis.getLocations() != null ? dis.getLocations().hashCode() : 0);
51+
result = 31 * result + dis.getErrorType().hashCode();
52+
return result;
53+
}
54+
55+
public static boolean equals(GraphQLError dis, GraphQLError dat) {
56+
if (dis == dat) {
57+
return true;
58+
}
59+
if (dis.getMessage() != null ? !dis.getMessage().equals(dat.getMessage()) : dat.getMessage() != null)
60+
return false;
61+
if (dis.getLocations() != null ? !dis.getLocations().equals(dat.getLocations()) : dat.getLocations() != null)
62+
return false;
63+
return dis.getErrorType() == dat.getErrorType();
64+
}
65+
}
66+
1667
}

src/main/java/graphql/InvalidSyntaxError.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ public String toString() {
4444
'}';
4545
}
4646

47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass()) return false;
51+
52+
InvalidSyntaxError that = (InvalidSyntaxError) o;
53+
54+
return Helper.equals(this,that);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Helper.hashCode(this);
60+
}
4761
}

src/main/java/graphql/validation/ValidationError.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,20 @@ public String toString() {
6161
", description='" + description + '\'' +
6262
'}';
6363
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
70+
ValidationError that = (ValidationError) o;
71+
72+
return Helper.equals(this, that);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Helper.hashCode(this);
78+
}
79+
6480
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package graphql
2+
3+
import graphql.language.SourceLocation
4+
import graphql.validation.ValidationError
5+
import graphql.validation.ValidationErrorType
6+
import spock.lang.Specification
7+
8+
9+
@SuppressWarnings("ChangeToOperator")
10+
class ErrorsTest extends Specification {
11+
12+
def src(int line, int col) {
13+
new SourceLocation(line,col)
14+
}
15+
16+
17+
private static void commonAssert(GraphQLError same1, GraphQLError same2, GraphQLError different1) {
18+
same1.equals(same2)
19+
same1.hashCode() == same2.hashCode()
20+
21+
same2.equals(same1)
22+
23+
// we should equal ourselves
24+
same1.equals(same1)
25+
26+
!same1.equals(different1)
27+
same1.hashCode() != different1.hashCode()
28+
}
29+
30+
def "InvalidSyntaxError equals and hashcode works"() {
31+
expect:
32+
33+
def same1 = new InvalidSyntaxError(src(15,34))
34+
def same2 = new InvalidSyntaxError(src(15,34))
35+
def different1 = new InvalidSyntaxError([src(24,100), src(15,34)])
36+
37+
commonAssert(same1, same2, different1)
38+
}
39+
40+
def "ValidationError equals and hashcode works"() {
41+
expect:
42+
43+
def same1 = new ValidationError(ValidationErrorType.BadValueForDefaultArg,[src(15,34),src(23,567)],"bad ju ju")
44+
def same2 = new ValidationError(ValidationErrorType.BadValueForDefaultArg,[src(15,34),src(23,567)],"bad ju ju")
45+
def different1 = new ValidationError(ValidationErrorType.FieldsConflict,[src(15,34),src(23,567)],"bad ju ju")
46+
47+
commonAssert(same1, same2, different1)
48+
}
49+
50+
def "ExceptionWhileDataFetching equals and hashcode works"() {
51+
expect:
52+
53+
def same1 = new ExceptionWhileDataFetching(new RuntimeException("bad ju ju"))
54+
def same2 = new ExceptionWhileDataFetching(new RuntimeException("bad ju ju"))
55+
def different1 = new ExceptionWhileDataFetching(new RuntimeException("unexpected ju ju"))
56+
57+
commonAssert(same1, same2, different1)
58+
}
59+
60+
61+
62+
}

0 commit comments

Comments
 (0)