-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GraphQLTypeUtil.java
305 lines (277 loc) · 9.41 KB
/
GraphQLTypeUtil.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package graphql.schema;
import graphql.Assert;
import graphql.PublicApi;
import graphql.introspection.Introspection;
import graphql.schema.idl.DirectiveInfo;
import graphql.schema.idl.ScalarInfo;
import java.util.Stack;
import java.util.function.Predicate;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
/**
* A utility class that helps work with {@link graphql.schema.GraphQLType}s
*/
@PublicApi
public class GraphQLTypeUtil {
/**
* This will return the type in graphql SDL format, eg [typeName!]!
*
* @param type the type in play
*
* @return the type in graphql SDL format, eg [typeName!]!
*/
public static String simplePrint(GraphQLType type) {
Assert.assertNotNull(type, () -> "type can't be null");
StringBuilder sb = new StringBuilder();
if (isNonNull(type)) {
sb.append(simplePrint(unwrapOne(type)));
sb.append("!");
} else if (isList(type)) {
sb.append("[");
sb.append(simplePrint(unwrapOne(type)));
sb.append("]");
} else {
sb.append(((GraphQLNamedType) type).getName());
}
return sb.toString();
}
public static String simplePrint(GraphQLSchemaElement schemaElement) {
if (schemaElement instanceof GraphQLType) {
return simplePrint((GraphQLType) schemaElement);
}
if (schemaElement instanceof GraphQLNamedSchemaElement) {
return ((GraphQLNamedSchemaElement) schemaElement).getName();
}
// a schema element is either a GraphQLType or a GraphQLNamedSchemaElement
return assertShouldNeverHappen("unexpected schema element: " + schemaElement);
}
/**
* Returns true if the given type is a non null type
*
* @param type the type to check
*
* @return true if the given type is a non null type
*/
public static boolean isNonNull(GraphQLType type) {
return type instanceof GraphQLNonNull;
}
/**
* Returns true if the given type is a nullable type
*
* @param type the type to check
*
* @return true if the given type is a nullable type
*/
public static boolean isNullable(GraphQLType type) {
return !isNonNull(type);
}
/**
* Returns true if the given type is a list type
*
* @param type the type to check
*
* @return true if the given type is a list type
*/
public static boolean isList(GraphQLType type) {
return type instanceof GraphQLList;
}
/**
* Returns true if the given type is a non null or list type, that is a wrapped type
*
* @param type the type to check
*
* @return true if the given type is a non null or list type
*/
public static boolean isWrapped(GraphQLType type) {
return isList(type) || isNonNull(type);
}
/**
* Returns true if the given type is NOT a non null or list type
*
* @param type the type to check
*
* @return true if the given type is NOT a non null or list type
*/
public static boolean isNotWrapped(GraphQLType type) {
return !isWrapped(type);
}
/**
* Returns true if the given type is a scalar type
*
* @param type the type to check
*
* @return true if the given type is a scalar type
*/
public static boolean isScalar(GraphQLType type) {
return type instanceof GraphQLScalarType;
}
/**
* Returns true if the given type is an enum type
*
* @param type the type to check
*
* @return true if the given type is an enum type
*/
public static boolean isEnum(GraphQLType type) {
return type instanceof GraphQLEnumType;
}
/**
* Returns true if the given type is a leaf type, that it cant contain any more fields
*
* @param type the type to check
*
* @return true if the given type is a leaf type
*/
public static boolean isLeaf(GraphQLType type) {
GraphQLUnmodifiedType unmodifiedType = unwrapAll(type);
return
unmodifiedType instanceof GraphQLScalarType
|| unmodifiedType instanceof GraphQLEnumType;
}
/**
* Returns true if the given type is an input type
*
* @param type the type to check
*
* @return true if the given type is an input type
*/
public static boolean isInput(GraphQLType type) {
GraphQLUnmodifiedType unmodifiedType = unwrapAll(type);
return
unmodifiedType instanceof GraphQLScalarType
|| unmodifiedType instanceof GraphQLEnumType
|| unmodifiedType instanceof GraphQLInputObjectType;
}
/**
* Unwraps one layer of the type or just returns the type again if it's not a wrapped type
*
* @param type the type to unwrapOne
*
* @return the unwrapped type or the same type again if it's not wrapped
*/
public static GraphQLType unwrapOne(GraphQLType type) {
if (isNonNull(type)) {
return ((GraphQLNonNull) type).getWrappedType();
} else if (isList(type)) {
return ((GraphQLList) type).getWrappedType();
}
return type;
}
/**
* Unwraps one layer of the type or just returns the type again if it's not a wrapped type
* and then cast to the target type.
*
* @param type the type to unwrapOne
* @param <T> for two
*
* @return the unwrapped type or the same type again if it's not wrapped
*/
public static <T extends GraphQLType> T unwrapOneAs(GraphQLType type) {
//noinspection unchecked
return (T) unwrapOne(type);
}
/**
* Unwraps all layers of the type or just returns the type again if it's not a wrapped type
*
* @param type the type to unwrapOne
*
* @return the underlying type
*/
public static GraphQLUnmodifiedType unwrapAll(GraphQLType type) {
return unwrapAllAs(type);
}
/**
* Unwraps all layers of the type or just returns the type again if it's not a wrapped type
* and then cast to the target type.
*
* @param type the type to unwrapOne
* @param <T> for two
*
* @return the underlying type
*/
public static <T extends GraphQLType> T unwrapAllAs(GraphQLType type) {
//noinspection unchecked
return (T) unwrapAllImpl(type);
}
private static GraphQLType unwrapAllImpl(GraphQLType type) {
while (true) {
if (isNotWrapped(type)) {
return type;
}
type = unwrapOne(type);
}
}
/**
* Unwraps all non nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull}
*
* @param type the type to unwrap
*
* @return the underlying type that is not {@link GraphQLNonNull}
*/
public static GraphQLType unwrapNonNull(GraphQLType type) {
while (isNonNull(type)) {
type = unwrapOne(type);
}
return type;
}
/**
* Unwraps all non nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull}
* and then cast to the target type.
*
* @param type the type to unwrap
* @param <T> for two
*
* @return the underlying type that is not {@link GraphQLNonNull}
*/
public static <T extends GraphQLType> T unwrapNonNullAs(GraphQLType type) {
//noinspection unchecked
return (T) unwrapNonNull(type);
}
/**
* graphql types can be wrapped in {@link GraphQLNonNull} and {@link GraphQLList} type wrappers
* so this method will unwrap the type down to the raw unwrapped type and return that wrapping
* as a stack, with the top of the stack being the raw underling type.
*
* @param type the type to unwrap
*
* @return a stack of the type wrapping which will be at least 1 later deep
*/
public static Stack<GraphQLType> unwrapType(GraphQLType type) {
assertNotNull(type);
Stack<GraphQLType> decoration = new Stack<>();
while (true) {
decoration.push(type);
if (isNotWrapped(type)) {
break;
}
type = unwrapOne(type);
}
return decoration;
}
public static boolean isInterfaceOrUnion(GraphQLType type) {
return type instanceof GraphQLInterfaceType || type instanceof GraphQLUnionType;
}
public static boolean isObjectType(GraphQLType type) {
return type instanceof GraphQLObjectType;
}
/**
* This predicate returns true if the schema element is an inbuilt schema element
* such as the system scalars and directives or introspection types
*
* @return true if it's a system schema element
*/
public static Predicate<GraphQLNamedSchemaElement> isSystemElement() {
return schemaElement -> {
if (schemaElement instanceof GraphQLScalarType) {
return ScalarInfo.isGraphqlSpecifiedScalar((GraphQLScalarType) schemaElement);
}
if (schemaElement instanceof GraphQLDirective) {
return DirectiveInfo.isGraphqlSpecifiedDirective((GraphQLDirective) schemaElement);
}
if (schemaElement instanceof GraphQLNamedType) {
return Introspection.isIntrospectionTypes((GraphQLNamedType) schemaElement);
}
return false;
};
}
}