This repository was archived by the owner on Feb 27, 2023. It is now read-only.
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
138 lines (116 loc) · 3.71 KB
/
TypeInfo.java
File metadata and controls
138 lines (116 loc) · 3.71 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
package graphql.schema.idl;
import graphql.Internal;
import graphql.language.AstPrinter;
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.Objects;
import java.util.Stack;
/**
* This helper gives you access to the type info given a type definition
*/
@Internal
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<>();
private 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();
}
public boolean isList() {
return rawType instanceof ListType;
}
public boolean isNonNull() {
return rawType instanceof NonNullType;
}
public boolean isPlain() {
return !isList() && !isNonNull();
}
/**
* 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;
}
public static String getAstDesc(Type type) {
return AstPrinter.printAst(type);
}
public TypeInfo unwrapOne() {
if (rawType instanceof NonNullType) {
return typeInfo(((NonNullType) rawType).getType());
}
if (rawType instanceof ListType) {
return typeInfo(((ListType) rawType).getType());
}
return this;
}
public Type unwrapOneType() {
return unwrapOne().getRawType();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeInfo typeInfo = (TypeInfo) o;
return isNonNull() == typeInfo.isNonNull() &&
isList() == typeInfo.isList() &&
Objects.equals(typeName.getName(), typeInfo.typeName.getName());
}
@Override
public int hashCode() {
return Objects.hash(typeName.getName(), isNonNull(), isList());
}
@Override
public String toString() {
return "TypeInfo{" +
getAstDesc(rawType) +
'}';
}
}