forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeInfo.java
More file actions
90 lines (77 loc) · 2.49 KB
/
Copy pathTypeInfo.java
File metadata and controls
90 lines (77 loc) · 2.49 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
package graphql.schema.idl;
import graphql.language.ListType;
import graphql.language.NonNullType;
import graphql.language.Type;
import graphql.language.TypeName;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLType;
import java.util.Stack;
/**
* This helper gives you access to the type info given a type definition
*/
public class TypeInfo {
public static TypeInfo typeInfo(Type type) {
return new TypeInfo(type);
}
private final Type rawType;
private final TypeName typeName;
private final Stack<Class<?>> decoration = new Stack<>();
public TypeInfo(Type type) {
this.rawType = type;
while (!(type instanceof TypeName)) {
if (type instanceof NonNullType) {
decoration.push(NonNullType.class);
type = ((NonNullType) type).getType();
}
if (type instanceof ListType) {
decoration.push(ListType.class);
type = ((ListType) type).getType();
}
}
this.typeName = (TypeName) type;
}
public Type getRawType() {
return rawType;
}
public TypeName getTypeName() {
return typeName;
}
public String getName() {
return typeName.getName();
}
/**
* This will decorate a graphql type with the original hierarchy of non null and list'ness
* it originally contained in its definition type
*
* @param objectType this should be a graphql type that was originally built from this raw type
* @param <T> the type
*
* @return the decorated type
*/
public <T extends GraphQLType> T decorate(GraphQLType objectType) {
GraphQLType out = objectType;
Stack<Class<?>> wrappingStack = new Stack<>();
wrappingStack.addAll(this.decoration);
while (!wrappingStack.isEmpty()) {
Class<?> clazz = wrappingStack.pop();
if (clazz.equals(NonNullType.class)) {
out = new GraphQLNonNull(out);
}
if (clazz.equals(ListType.class)) {
out = new GraphQLList(out);
}
}
// we handle both input and output graphql types
//noinspection unchecked
return (T) out;
}
@Override
public String toString() {
return "TypeInfo{" +
"rawType=" + rawType +
", typeName=" + typeName +
", isNonNull=" + decoration +
'}';
}
}