/
ResponseMapGraphQlResponse.java
233 lines (192 loc) · 6.56 KB
/
ResponseMapGraphQlResponse.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.client;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import graphql.ErrorClassification;
import graphql.GraphQLError;
import graphql.language.SourceLocation;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.ResponseError;
import org.springframework.graphql.support.AbstractGraphQlResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* {@link GraphQlResponse} that wraps a deserialized the GraphQL response map.
*
* @author Rossen Stoyanchev
*/
class ResponseMapGraphQlResponse extends AbstractGraphQlResponse {
private final Map<String, Object> responseMap;
private final List<ResponseError> errors;
ResponseMapGraphQlResponse(Map<String, Object> responseMap) {
Assert.notNull(responseMap, "'responseMap' is required");
this.responseMap = responseMap;
this.errors = wrapErrors(responseMap);
}
protected ResponseMapGraphQlResponse(GraphQlResponse response) {
Assert.notNull(response, "'GraphQlResponse' is required");
this.responseMap = response.toMap();
this.errors = response.getErrors();
}
@SuppressWarnings("unchecked")
private static List<ResponseError> wrapErrors(Map<String, Object> map) {
List<Map<String, Object>> errors = (List<Map<String, Object>>) map.get("errors");
errors = (errors != null) ? errors : Collections.emptyList();
return errors.stream().map(MapResponseError::new).collect(Collectors.toList());
}
@Override
public boolean isValid() {
return (this.responseMap.containsKey("data") && this.responseMap.get("data") != null);
}
@Override
public List<ResponseError> getErrors() {
return this.errors;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getData() {
return (T) this.responseMap.get("data");
}
@SuppressWarnings("unchecked")
@Override
public Map<Object, Object> getExtensions() {
return (Map<Object, Object>) this.responseMap.getOrDefault("extensions", Collections.emptyMap());
}
@Override
public Map<String, Object> toMap() {
return this.responseMap;
}
@Override
public boolean equals(Object other) {
return (other instanceof ResponseMapGraphQlResponse &&
this.responseMap.equals(((ResponseMapGraphQlResponse) other).responseMap));
}
@Override
public int hashCode() {
return this.responseMap.hashCode();
}
@Override
public String toString() {
return this.responseMap.toString();
}
/**
* {@link GraphQLError} that wraps a deserialized the GraphQL response map.
*/
private static final class MapResponseError implements ResponseError {
private final Map<String, Object> errorMap;
private final List<SourceLocation> locations;
private final String path;
MapResponseError(Map<String, Object> errorMap) {
Assert.notNull(errorMap, "'errorMap' is required");
this.errorMap = errorMap;
this.locations = initSourceLocations(errorMap);
this.path = initPath(errorMap);
}
@SuppressWarnings("unchecked")
private static List<SourceLocation> initSourceLocations(Map<String, Object> errorMap) {
List<Map<String, Object>> locations = (List<Map<String, Object>>) errorMap.get("locations");
if (locations == null) {
return Collections.emptyList();
}
return locations.stream()
.map((m) -> new SourceLocation(getInt(m, "line"), getInt(m, "column"), (String) m.get("sourceName")))
.collect(Collectors.toList());
}
private static int getInt(Map<String, Object> map, String key) {
if (map.get(key) instanceof Number number) {
return number.intValue();
}
else {
throw new IllegalArgumentException(
"Expected integer value: " + ObjectUtils.nullSafeClassName(map.get(key)));
}
}
@SuppressWarnings("unchecked")
private static String initPath(Map<String, Object> errorMap) {
List<Object> path = (List<Object>) errorMap.get("path");
if (path == null) {
return "";
}
return path.stream().reduce("",
(s, o) -> s + ((o instanceof Integer) ? "[" + o + "]" : (s.isEmpty() ? o : "." + o)),
(s, s2) -> null);
}
@Override
@Nullable
public String getMessage() {
return (String) this.errorMap.get("message");
}
@Override
public List<SourceLocation> getLocations() {
return this.locations;
}
@Override
public ErrorClassification getErrorType() {
String classification = (String) getExtensions().getOrDefault("classification", "");
try {
return graphql.ErrorType.valueOf(classification);
}
catch (IllegalArgumentException ex) {
return org.springframework.graphql.execution.ErrorType.valueOf(classification);
}
}
@Override
public String getPath() {
return this.path;
}
@SuppressWarnings("unchecked")
@Override
public List<Object> getParsedPath() {
return (List<Object>) this.errorMap.getOrDefault("path", Collections.emptyList());
}
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getExtensions() {
return (Map<String, Object>) this.errorMap.getOrDefault("extensions", Collections.emptyMap());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
ResponseError other = (ResponseError) o;
return (ObjectUtils.nullSafeEquals(getMessage(), other.getMessage()) &&
ObjectUtils.nullSafeEquals(getLocations(), other.getLocations()) &&
ObjectUtils.nullSafeEquals(getParsedPath(), other.getParsedPath()) &&
getErrorType() == other.getErrorType());
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + ObjectUtils.nullSafeHashCode(getMessage());
result = 31 * result + ObjectUtils.nullSafeHashCode(getLocations());
result = 31 * result + ObjectUtils.nullSafeHashCode(getParsedPath());
result = 31 * result + ObjectUtils.nullSafeHashCode(getErrorType());
return result;
}
@Override
public String toString() {
return this.errorMap.toString();
}
}
}