forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphqlContract.java
More file actions
109 lines (93 loc) · 3.04 KB
/
Copy pathGraphqlContract.java
File metadata and controls
109 lines (93 loc) · 3.04 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
/*
* 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.DefaultContract;
import feign.Experimental;
import feign.Request.HttpMethod;
import feign.RequestTemplate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
@Experimental
public class GraphqlContract extends DefaultContract {
private static final Pattern OPERATION_FIELD_PATTERN =
Pattern.compile("\\{\\s*(\\w+)\\s*[({]", Pattern.DOTALL);
private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\s*(\\w+)\\s*:");
private final Map<String, QueryMetadata> metadata = new ConcurrentHashMap<>();
public GraphqlContract() {
super.registerMethodAnnotation(
GraphqlQuery.class,
(annotation, data) -> {
var query = annotation.value();
if (data.template().method() == null) {
data.template().method(HttpMethod.POST);
data.template().uri("/");
}
var variableName = extractFirstVariable(query);
metadata.put(data.configKey(), new QueryMetadata(query, variableName));
});
}
Map<String, QueryMetadata> queryMetadata() {
return metadata;
}
QueryMetadata lookupMetadata(RequestTemplate template) {
if (template.methodMetadata() == null) {
return null;
}
return metadata.get(template.methodMetadata().configKey());
}
static String extractOperationField(String query) {
int braceCount = 0;
boolean inOperation = false;
for (int i = 0; i < query.length(); i++) {
char c = query.charAt(i);
if (c == '{') {
braceCount++;
if (braceCount == 1) {
inOperation = true;
} else if (braceCount == 2 && inOperation) {
var prefix = query.substring(0, i).trim();
var m = OPERATION_FIELD_PATTERN.matcher(prefix + "{");
if (m.find()) {
return m.group(1);
}
}
} else if (c == '}') {
braceCount--;
}
}
var m = OPERATION_FIELD_PATTERN.matcher(query);
if (m.find()) {
return m.group(1);
}
return null;
}
static String extractFirstVariable(String query) {
var m = VARIABLE_PATTERN.matcher(query);
if (m.find()) {
return m.group(1);
}
return null;
}
static class QueryMetadata {
final String query;
final String variableName;
QueryMetadata(String query, String variableName) {
this.query = query;
this.variableName = variableName;
}
}
}