This repository was archived by the owner on Feb 27, 2023. It is now read-only.
forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphqlErrorHelper.java
More file actions
68 lines (57 loc) · 2.43 KB
/
GraphqlErrorHelper.java
File metadata and controls
68 lines (57 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package graphql;
import graphql.language.SourceLocation;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.toList;
/**
* This little helper allows GraphQlErrors to implement
* common things (hashcode/ equals ) and to specification more easily
*/
@SuppressWarnings("SimplifiableIfStatement")
public class GraphqlErrorHelper {
public static Map<String, Object> toSpecification(GraphQLError error) {
Map<String, Object> errorMap = new LinkedHashMap<>();
errorMap.put("message", error.getMessage());
if (error.getLocations() != null) {
errorMap.put("locations", locations(error.getLocations()));
}
if (error.getPath() != null) {
errorMap.put("path", error.getPath());
}
if (error.getExtensions() != null) {
errorMap.put("extensions", error.getExtensions());
}
return errorMap;
}
public static Object locations(List<SourceLocation> locations) {
return locations.stream().map(GraphqlErrorHelper::location).collect(toList());
}
public static Object location(SourceLocation location) {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("line", location.getLine());
map.put("column", location.getColumn());
return map;
}
public static int hashCode(GraphQLError dis) {
int result = dis.getMessage() != null ? dis.getMessage().hashCode() : 0;
result = 31 * result + (dis.getLocations() != null ? dis.getLocations().hashCode() : 0);
result = 31 * result + (dis.getPath() != null ? dis.getPath().hashCode() : 0);
result = 31 * result + dis.getErrorType().hashCode();
return result;
}
public static boolean equals(GraphQLError dis, Object o) {
if (dis == o) {
return true;
}
if (o == null || dis.getClass() != o.getClass()) return false;
GraphQLError dat = (GraphQLError) o;
if (dis.getMessage() != null ? !dis.getMessage().equals(dat.getMessage()) : dat.getMessage() != null)
return false;
if (dis.getLocations() != null ? !dis.getLocations().equals(dat.getLocations()) : dat.getLocations() != null)
return false;
if (dis.getPath() != null ? !dis.getPath().equals(dat.getPath()) : dat.getPath() != null)
return false;
return dis.getErrorType() == dat.getErrorType();
}
}