-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
TypeRuntimeWiring.java
220 lines (193 loc) · 7.45 KB
/
TypeRuntimeWiring.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
package graphql.schema.idl;
import graphql.PublicApi;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.schema.TypeResolver;
import graphql.schema.idl.errors.StrictModeWiringException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.UnaryOperator;
import static graphql.Assert.assertNotNull;
import static java.lang.String.format;
/**
* A type runtime wiring is a specification of the data fetchers and possible type resolver for a given type name.
*
* This is used by {@link RuntimeWiring} to wire together a functional {@link GraphQLSchema}
*/
@PublicApi
public class TypeRuntimeWiring {
private final static AtomicBoolean DEFAULT_STRICT_MODE = new AtomicBoolean(false);
/**
* By default {@link TypeRuntimeWiring} builders are not in strict mode, but you can set a JVM wide value
* so that any created will be.
*
* @param strictMode the desired strict mode state
*
* @see Builder#strictMode()
*/
public static void setStrictModeJvmWide(boolean strictMode) {
DEFAULT_STRICT_MODE.set(strictMode);
}
/**
* @return the current JVM wide state of strict mode
*/
public static boolean getStrictModeJvmWide() {
return DEFAULT_STRICT_MODE.get();
}
private final String typeName;
private final DataFetcher defaultDataFetcher;
private final Map<String, DataFetcher> fieldDataFetchers;
private final TypeResolver typeResolver;
private final EnumValuesProvider enumValuesProvider;
private TypeRuntimeWiring(String typeName, DataFetcher defaultDataFetcher, Map<String, DataFetcher> fieldDataFetchers, TypeResolver typeResolver, EnumValuesProvider enumValuesProvider) {
this.typeName = typeName;
this.defaultDataFetcher = defaultDataFetcher;
this.fieldDataFetchers = fieldDataFetchers;
this.typeResolver = typeResolver;
this.enumValuesProvider = enumValuesProvider;
}
/**
* Creates a new type wiring builder
*
* @param typeName the name of the type to wire
*
* @return the builder
*/
public static Builder newTypeWiring(String typeName) {
assertNotNull(typeName, () -> "You must provide a type name");
return new Builder().typeName(typeName);
}
/**
* This form allows a lambda to be used as the builder
*
* @param typeName the name of the type to wire
* @param builderFunction a function that will be given the builder to use
*
* @return the same builder back please
*/
public static TypeRuntimeWiring newTypeWiring(String typeName, UnaryOperator<Builder> builderFunction) {
return builderFunction.apply(newTypeWiring(typeName)).build();
}
public String getTypeName() {
return typeName;
}
public Map<String, DataFetcher> getFieldDataFetchers() {
return fieldDataFetchers;
}
public DataFetcher getDefaultDataFetcher() {
return defaultDataFetcher;
}
public TypeResolver getTypeResolver() {
return typeResolver;
}
public EnumValuesProvider getEnumValuesProvider() {
return enumValuesProvider;
}
public static class Builder {
private final Map<String, DataFetcher> fieldDataFetchers = new LinkedHashMap<>();
private String typeName;
private DataFetcher defaultDataFetcher;
private TypeResolver typeResolver;
private EnumValuesProvider enumValuesProvider;
private boolean strictMode = DEFAULT_STRICT_MODE.get();
/**
* Sets the type name for this type wiring. You MUST set this.
*
* @param typeName the name of the type
*
* @return the current type wiring
*/
public Builder typeName(String typeName) {
this.typeName = typeName;
return this;
}
/**
* This puts the builder into strict mode, so if things get defined twice, for example, it
* will throw a {@link StrictModeWiringException}.
*
* @return this builder
*/
public Builder strictMode() {
this.strictMode = true;
return this;
}
/**
* Adds a data fetcher for the current type to the specified field
*
* @param fieldName the field that data fetcher should apply to
* @param dataFetcher the new data Fetcher
*
* @return the current type wiring
*/
public Builder dataFetcher(String fieldName, DataFetcher dataFetcher) {
assertNotNull(dataFetcher, () -> "you must provide a data fetcher");
assertNotNull(fieldName, () -> "you must tell us what field");
if (strictMode) {
assertFieldStrictly(fieldName);
}
fieldDataFetchers.put(fieldName, dataFetcher);
return this;
}
/**
* Adds data fetchers for the current type to the specified field
*
* @param dataFetchersMap a map of fields to data fetchers
*
* @return the current type wiring
*/
public Builder dataFetchers(Map<String, DataFetcher> dataFetchersMap) {
assertNotNull(dataFetchersMap, () -> "you must provide a data fetchers map");
if (strictMode) {
dataFetchersMap.forEach((fieldName, df) -> {
assertFieldStrictly(fieldName);
});
}
fieldDataFetchers.putAll(dataFetchersMap);
return this;
}
private void assertFieldStrictly(String fieldName) {
if (fieldDataFetchers.containsKey(fieldName)) {
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
}
}
/**
* All fields in a type need a data fetcher of some sort and this method is called to provide the default data fetcher
* that will be used for this type if no specific one has been provided per field.
*
* @param dataFetcher the default data fetcher to use for this type
*
* @return the current type wiring
*/
public Builder defaultDataFetcher(DataFetcher dataFetcher) {
assertNotNull(dataFetcher);
defaultDataFetcher = dataFetcher;
return this;
}
/**
* Adds a {@link TypeResolver} to the current type. This MUST be specified for Interface
* and Union types.
*
* @param typeResolver the type resolver in play
*
* @return the current type wiring
*/
public Builder typeResolver(TypeResolver typeResolver) {
assertNotNull(typeResolver, () -> "you must provide a type resolver");
this.typeResolver = typeResolver;
return this;
}
public Builder enumValues(EnumValuesProvider enumValuesProvider) {
assertNotNull(enumValuesProvider, () -> "you must provide an enum values provider");
this.enumValuesProvider = enumValuesProvider;
return this;
}
/**
* @return the built type wiring
*/
public TypeRuntimeWiring build() {
assertNotNull(typeName, () -> "you must provide a type name");
return new TypeRuntimeWiring(typeName, defaultDataFetcher, fieldDataFetchers, typeResolver, enumValuesProvider);
}
}
}