forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphqlDecoder.java
More file actions
164 lines (144 loc) · 4.92 KB
/
Copy pathGraphqlDecoder.java
File metadata and controls
164 lines (144 loc) · 4.92 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
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
/*
* Copyright © 2012 The Feign Authors ([email protected])
*
* 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
*
* http://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 feign.graphql;
import feign.Experimental;
import feign.Response;
import feign.Util;
import feign.codec.Decoder;
import feign.codec.JsonDecoder;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Experimental
public class GraphqlDecoder implements Decoder {
private final JsonDecoder jsonDecoder;
public GraphqlDecoder(JsonDecoder jsonDecoder) {
this.jsonDecoder = jsonDecoder;
}
@Override
public Object decode(Response response, Type type) throws IOException {
Type targetType = type;
boolean optional = isOptionalType(type);
if (optional) {
targetType = extractOptionalInnerType(type);
}
var result = doDecode(response, targetType);
if (result == null && isCollectionOrArrayType(targetType)) {
result = Util.emptyValueOf(targetType);
}
return optional ? Optional.ofNullable(result) : result;
}
@SuppressWarnings("unchecked")
private Object doDecode(Response response, Type type) throws IOException {
if (response.status() == 404 || response.status() == 204) {
return Util.emptyValueOf(type);
}
if (response.body() == null) {
return Util.emptyValueOf(type);
}
var root = (Map<String, Object>) jsonDecoder.decode(response, Map.class);
if (root == null) {
return Util.emptyValueOf(type);
}
var errors = root.get("errors");
if (errors instanceof List<?> errorList && !errorList.isEmpty()) {
var operationField = resolveOperationField(root, response);
throw new GraphqlErrorException(
response.status(), operationField, errors.toString(), response.request());
}
var data = root.get("data");
if (!(data instanceof Map)) {
return Util.emptyValueOf(type);
}
var dataMap = (Map<String, Object>) data;
var fieldNames = dataMap.keySet().iterator();
if (!fieldNames.hasNext()) {
return Util.emptyValueOf(type);
}
var firstField = fieldNames.next();
var operationData = dataMap.get(firstField);
if (operationData == null) {
return Util.emptyValueOf(type);
}
if (operationData instanceof List<?> list && !isCollectionOrArrayType(type)) {
if (list.isEmpty()) {
return Util.emptyValueOf(type);
}
operationData = list.get(0);
}
return jsonDecoder.convert(operationData, type);
}
@SuppressWarnings("unchecked")
private String resolveOperationField(Map<String, Object> root, Response response) {
var data = root.get("data");
if (data instanceof Map) {
var dataMap = (Map<String, Object>) data;
var names = dataMap.keySet().iterator();
if (names.hasNext()) {
return names.next();
}
}
if (response.request() != null && response.request().body() != null) {
try {
var fakeResponse =
Response.builder()
.status(200)
.headers(Collections.emptyMap())
.request(response.request())
.body(response.request().body())
.build();
var requestBody = (Map<String, Object>) jsonDecoder.decode(fakeResponse, Map.class);
if (requestBody != null) {
var query = requestBody.get("query");
if (query instanceof String queryStr) {
return GraphqlContract.extractOperationField(queryStr);
}
}
} catch (Exception e) {
// ignore parsing errors
}
}
return "unknown";
}
private boolean isOptionalType(Type type) {
if (type instanceof ParameterizedType pt && pt.getRawType() instanceof Class<?> cls) {
return cls == Optional.class;
}
if (type instanceof Class<?> cls) {
return cls == Optional.class;
}
return false;
}
private Type extractOptionalInnerType(Type type) {
if (type instanceof ParameterizedType pt) {
return pt.getActualTypeArguments()[0];
}
return Object.class;
}
private boolean isCollectionOrArrayType(Type type) {
if (type instanceof Class<?> cls) {
return cls.isArray() || Iterable.class.isAssignableFrom(cls);
}
if (type instanceof ParameterizedType pt && pt.getRawType() instanceof Class<?> cls) {
return Iterable.class.isAssignableFrom(cls);
}
return false;
}
}