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
105 lines (84 loc) · 2.72 KB
/
TypeInfo.java
File metadata and controls
105 lines (84 loc) · 2.72 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
package graphql.execution;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLType;
import static graphql.Assert.assertNotNull;
/**
* The raw graphql type system (rightly) does not contain a hierarchy of child to parent types nor the non null ness of
* type instances. This add this during query execution.
*/
public class TypeInfo {
private final GraphQLType type;
private final boolean typeIsNonNull;
private final TypeInfo parentType;
private TypeInfo(GraphQLType type, TypeInfo parentType, boolean nonNull) {
this.parentType = parentType;
this.type = type;
this.typeIsNonNull = nonNull;
assertNotNull(this.type, "you must provide a graphql type");
}
public GraphQLType type() {
return type;
}
@SuppressWarnings("unchecked")
public <T extends GraphQLType> T castType(Class<T> clazz) {
return clazz.cast(type);
}
public boolean typeIsNonNull() {
return typeIsNonNull;
}
public TypeInfo parentTypeInfo() {
return parentType;
}
public boolean hasParentType() {
return parentType != null;
}
/**
* This allows you to morph a type into a more specialized form yet return the same
* parent and non-null ness
*
* @param type the new type to be
*
* @return a new type info with the same
*/
public TypeInfo asType(GraphQLType type) {
return new TypeInfo(unwrap(type), this.parentType, this.typeIsNonNull);
}
private static GraphQLType unwrap(GraphQLType type) {
// its possible to have non nulls wrapping non nulls of things but it must end at some point
while (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
}
return type;
}
@Override
public String toString() {
return String.format("TypeInfo { nonnull=%s, type=%s, parentType=%s }",
typeIsNonNull, type, parentType);
}
public static TypeInfo.Builder newTypeInfo() {
return new Builder();
}
public static class Builder {
GraphQLType type;
TypeInfo parentType;
/**
* @see TypeInfo#newTypeInfo()
*/
private Builder() {
}
public Builder type(GraphQLType type) {
this.type = type;
return this;
}
public Builder parentInfo(TypeInfo typeInfo) {
this.parentType = typeInfo;
return this;
}
public TypeInfo build() {
if (type instanceof GraphQLNonNull) {
return new TypeInfo(unwrap(type), parentType, true);
}
return new TypeInfo(type, parentType, false);
}
}
}