@@ -152,4 +152,66 @@ class GraphqlErrorBuilderTest extends Specification {
152
152
error. path == null
153
153
154
154
}
155
- }
155
+
156
+ def " implements equals/hashCode correctly for matching errors" () {
157
+ when :
158
+ def firstError = toGraphQLError(first)
159
+ def secondError = toGraphQLError(second)
160
+
161
+ then :
162
+ firstError == secondError
163
+ firstError. hashCode() == secondError. hashCode()
164
+
165
+ where :
166
+ first | second
167
+ [message : " msg" ] | [message : " msg" ]
168
+ [message : " msg" , locations : [new SourceLocation (1 , 2 )]] | [message : " msg" , locations : [new SourceLocation (1 , 2 )]]
169
+ [message : " msg" , errorType : ErrorType.InvalidSyntax ] | [message : " msg" , errorType : ErrorType.InvalidSyntax ]
170
+ [message : " msg" , path : [" items" , 1 , " item" ]] | [message : " msg" , path : [" items" , 1 , " item" ]]
171
+ [message : " msg" , extensions : [aBoolean : true , aString : " foo" ]] | [message : " msg" , extensions : [aBoolean : true , aString : " foo" ]]
172
+ }
173
+
174
+ def " implements equals/hashCode correctly for different errors" () {
175
+ when :
176
+ def firstError = toGraphQLError(first)
177
+ def secondError = toGraphQLError(second)
178
+
179
+ then :
180
+ firstError != secondError
181
+ firstError. hashCode() != secondError. hashCode()
182
+
183
+ where :
184
+ first | second
185
+ [message : " msg" ] | [message : " different msg" ]
186
+ [message : " msg" , locations : [new SourceLocation (1 , 2 )]] | [message : " msg" , locations : [new SourceLocation (3 , 4 )]]
187
+ [message : " msg" , errorType : ErrorType.InvalidSyntax ] | [message : " msg" , errorType : ErrorType.DataFetchingException ]
188
+ [message : " msg" , path : [" items" , " 1" , " item" ]] | [message : " msg" , path : [" items" ]]
189
+ [message : " msg" , extensions : [aBoolean : false ]] | [message : " msg" , extensions : [aString : " foo" ]]
190
+ }
191
+
192
+ private static GraphQLError toGraphQLError (Map<String , Object > errorFields ) {
193
+ def errorBuilder = GraphQLError . newError();
194
+ errorFields. forEach { key , value ->
195
+ if (value != null ) {
196
+ switch (key) {
197
+ case " message" :
198
+ errorBuilder. message(value as String );
199
+ break ;
200
+ case " locations" :
201
+ errorBuilder. locations(value as List<SourceLocation > );
202
+ break ;
203
+ case " errorType" :
204
+ errorBuilder. errorType(value as ErrorClassification );
205
+ break ;
206
+ case " path" :
207
+ errorBuilder. path(value as List<Object > );
208
+ break ;
209
+ case " extensions" :
210
+ errorBuilder. extensions(value as Map<String , Object > );
211
+ break ;
212
+ }
213
+ }
214
+ }
215
+ return errorBuilder. build();
216
+ }
217
+ }
0 commit comments