Skip to content

Commit e96134d

Browse files
committed
redefine references api
Removes the TypeReference class in favour of the already existing GraphQLTypeReference.
1 parent 1b44b3e commit e96134d

13 files changed

+150
-153
lines changed

src/main/java/graphql/Assert.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
public class Assert {
66

7-
87
public static <T> T assertNotNull(T object, String errorMessage) {
98
if (object != null) return object;
109
throw new AssertException(errorMessage);
@@ -16,17 +15,19 @@ public static <T> Collection<T> assertNotEmpty(Collection<T> c, String errorMess
1615
}
1716

1817
private static final String invalidNameErrorMessage = "Name must be non-null, non-empty and match [_A-Za-z][_0-9A-Za-z]*";
18+
1919
/**
2020
* Validates that the Lexical token name matches the current spec.
21-
* currently non null, non empty,
21+
* currently non null, non empty,
22+
*
2223
* @param name - the name to be validated.
23-
* @return the name if valid, or AssertException if invalid.
24+
* @return the name if valid, or AssertException if invalid.
2425
*/
25-
public static String assertValidName(String name) {
26-
if (name != null && !name.isEmpty() && name.matches("[_A-Za-z][_0-9A-Za-z]*")) {
27-
return name;
28-
}
29-
throw new AssertException(invalidNameErrorMessage);
30-
}
26+
public static String assertValidName(String name) {
27+
if (name != null && !name.isEmpty() && name.matches("[_A-Za-z][_0-9A-Za-z]*")) {
28+
return name;
29+
}
30+
throw new AssertException(invalidNameErrorMessage);
31+
}
3132

3233
}

src/main/java/graphql/schema/GraphQLFieldDefinition.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.ArrayList;
77
import java.util.List;
88
import java.util.Map;
9-
import java.util.function.BinaryOperator;
109
import java.util.function.UnaryOperator;
1110

1211
import static graphql.Assert.assertNotNull;
@@ -43,7 +42,7 @@ public GraphQLFieldDefinition(String name, String description, GraphQLOutputType
4342

4443

4544
void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
46-
type = (GraphQLOutputType) new SchemaUtil().resolveTypeReference(type, typeMap);
45+
this.type = (GraphQLOutputType) new SchemaUtil().resolveTypeReference(this.type, typeMap);
4746
}
4847

4948
public String getName() {

src/main/java/graphql/schema/GraphQLInputObjectType.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package graphql.schema;
22

3+
import graphql.AssertException;
4+
import graphql.language.InputObjectTypeDefinition;
5+
36
import java.util.ArrayList;
4-
import java.util.Collections;
57
import java.util.LinkedHashMap;
68
import java.util.List;
79
import java.util.Map;
810
import java.util.function.UnaryOperator;
911

10-
import graphql.AssertException;
11-
import graphql.language.InputObjectTypeDefinition;
12-
1312
import static graphql.Assert.assertNotNull;
1413
import static graphql.Assert.assertValidName;
1514

@@ -61,10 +60,7 @@ public static Builder newInputObject() {
6160
return new Builder();
6261
}
6362

64-
public static Reference reference(String name) {
65-
return new Reference(name);
66-
}
67-
63+
6864
@Override
6965
public GraphQLInputObjectField getFieldDefinition(String name) {
7066
return fieldMap.get(name);
@@ -149,10 +145,4 @@ public GraphQLInputObjectType build() {
149145
}
150146

151147
}
152-
153-
private static class Reference extends GraphQLInputObjectType implements TypeReference {
154-
private Reference(String name) {
155-
super(name, "", Collections.emptyList());
156-
}
157-
}
158148
}

src/main/java/graphql/schema/GraphQLInterfaceType.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package graphql.schema;
22

3+
import graphql.AssertException;
4+
import graphql.language.InterfaceTypeDefinition;
5+
36
import java.util.ArrayList;
4-
import java.util.Collections;
57
import java.util.LinkedHashMap;
68
import java.util.List;
79
import java.util.Map;
810
import java.util.function.UnaryOperator;
911

10-
import graphql.AssertException;
11-
import graphql.language.InterfaceTypeDefinition;
12-
1312
import static graphql.Assert.assertNotNull;
1413
import static graphql.Assert.assertValidName;
1514

@@ -22,7 +21,7 @@ public class GraphQLInterfaceType implements GraphQLType, GraphQLOutputType, Gra
2221
private final InterfaceTypeDefinition definition;
2322

2423
public GraphQLInterfaceType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions, TypeResolver typeResolver) {
25-
this(name,description,fieldDefinitions,typeResolver,null);
24+
this(name, description, fieldDefinitions, typeResolver, null);
2625
}
2726

2827
public GraphQLInterfaceType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions, TypeResolver typeResolver, InterfaceTypeDefinition definition) {
@@ -84,10 +83,7 @@ public static Builder newInterface() {
8483
return new Builder();
8584
}
8685

87-
public static Reference reference(String name) {
88-
return new Reference(name);
89-
}
90-
86+
9187
public static class Builder {
9288
private String name;
9389
private String description;
@@ -161,13 +157,6 @@ public Builder typeResolver(TypeResolver typeResolver) {
161157
public GraphQLInterfaceType build() {
162158
return new GraphQLInterfaceType(name, description, fields, typeResolver, definition);
163159
}
164-
165-
166160
}
167161

168-
private static class Reference extends GraphQLInterfaceType implements TypeReference {
169-
private Reference(String name) {
170-
super(name, "", Collections.emptyList(), new TypeResolverProxy());
171-
}
172-
}
173162
}

src/main/java/graphql/schema/GraphQLObjectType.java

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,49 @@
44
import graphql.language.ObjectTypeDefinition;
55

66
import java.util.ArrayList;
7-
import java.util.Collections;
87
import java.util.LinkedHashMap;
98
import java.util.List;
109
import java.util.Map;
1110
import java.util.function.UnaryOperator;
11+
import java.util.stream.Collectors;
1212

1313
import static graphql.Assert.assertNotNull;
1414
import static graphql.Assert.assertValidName;
1515

16+
1617
public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQLFieldsContainer, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType {
1718

19+
1820
private final String name;
1921
private final String description;
2022
private final Map<String, GraphQLFieldDefinition> fieldDefinitionsByName = new LinkedHashMap<>();
21-
private final List<GraphQLInterfaceType> interfaces = new ArrayList<>();
23+
private final List<TypeOrReference<GraphQLInterfaceType>> tmpInterfaces = new ArrayList<>();
24+
private List<GraphQLInterfaceType> interfaces;
2225
private final ObjectTypeDefinition definition;
2326

2427
public GraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions,
25-
List<GraphQLInterfaceType> interfaces) {
26-
this(name, description, fieldDefinitions, interfaces, null);
28+
List<TypeOrReference<GraphQLInterfaceType>> tmpInterfaces) {
29+
this(name, description, fieldDefinitions, tmpInterfaces, null);
2730
}
2831

2932
public GraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions,
30-
List<GraphQLInterfaceType> interfaces, ObjectTypeDefinition definition) {
33+
List<TypeOrReference<GraphQLInterfaceType>> tmpInterfaces, ObjectTypeDefinition definition) {
3134
assertValidName(name);
3235
assertNotNull(fieldDefinitions, "fieldDefinitions can't be null");
33-
assertNotNull(interfaces, "interfaces can't be null");
34-
assertNotNull(interfaces, "unresolvedInterfaces can't be null");
36+
assertNotNull(tmpInterfaces, "interfaces can't be null");
37+
assertNotNull(tmpInterfaces, "unresolvedInterfaces can't be null");
3538
this.name = name;
3639
this.description = description;
37-
this.interfaces.addAll(interfaces);
40+
this.tmpInterfaces.addAll(tmpInterfaces);
3841
this.definition = definition;
3942
buildDefinitionMap(fieldDefinitions);
4043
}
4144

4245
void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
43-
for (int i = 0; i < interfaces.size(); i++) {
44-
GraphQLInterfaceType inter = interfaces.get(i);
45-
if (inter instanceof TypeReference) {
46-
this.interfaces.set(i, (GraphQLInterfaceType) new SchemaUtil().resolveTypeReference(inter, typeMap));
47-
}
48-
}
46+
this.interfaces = this.tmpInterfaces.stream()
47+
.map(TypeOrReference::getTypeOrReference)
48+
.map(type -> (GraphQLInterfaceType) new SchemaUtil().resolveTypeReference(type, typeMap))
49+
.collect(Collectors.toList());
4950
}
5051

5152
private void buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
@@ -68,6 +69,13 @@ public List<GraphQLFieldDefinition> getFieldDefinitions() {
6869

6970

7071
public List<GraphQLInterfaceType> getInterfaces() {
72+
if (this.interfaces == null) {
73+
return this.tmpInterfaces
74+
.stream()
75+
.filter(TypeOrReference::isType)
76+
.map(TypeOrReference::getType)
77+
.collect(Collectors.toList());
78+
}
7179
return new ArrayList<>(interfaces);
7280
}
7381

@@ -98,15 +106,11 @@ public static Builder newObject() {
98106
return new Builder();
99107
}
100108

101-
public static Reference reference(String name) {
102-
return new Reference(name);
103-
}
104-
105109
public static class Builder {
106110
private String name;
107111
private String description;
108112
private List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<>();
109-
private List<GraphQLInterfaceType> interfaces = new ArrayList<>();
113+
private List<TypeOrReference<GraphQLInterfaceType>> interfaces = new ArrayList<>();
110114
private ObjectTypeDefinition definition;
111115

112116
public Builder name(String name) {
@@ -169,7 +173,13 @@ public Builder fields(List<GraphQLFieldDefinition> fieldDefinitions) {
169173

170174
public Builder withInterface(GraphQLInterfaceType interfaceType) {
171175
assertNotNull(interfaceType, "interfaceType can't be null");
172-
this.interfaces.add(interfaceType);
176+
this.interfaces.add(new TypeOrReference<>(interfaceType));
177+
return this;
178+
}
179+
180+
public Builder withInterface(GraphQLTypeReference reference) {
181+
assertNotNull(reference, "reference can't be null");
182+
this.interfaces.add(new TypeOrReference<>(reference));
173183
return this;
174184
}
175185

@@ -180,15 +190,17 @@ public Builder withInterfaces(GraphQLInterfaceType... interfaceType) {
180190
return this;
181191
}
182192

193+
public Builder withInterfaces(GraphQLTypeReference... references) {
194+
for (GraphQLTypeReference reference : references) {
195+
withInterface(reference);
196+
}
197+
return this;
198+
}
199+
183200
public GraphQLObjectType build() {
184201
return new GraphQLObjectType(name, description, fieldDefinitions, interfaces, definition);
185202
}
186203

187204
}
188205

189-
private static class Reference extends GraphQLObjectType implements TypeReference {
190-
private Reference(String name) {
191-
super(name, "", Collections.emptyList(), Collections.emptyList(), null);
192-
}
193-
}
194206
}

src/main/java/graphql/schema/GraphQLTypeReference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package graphql.schema;
22

33

4-
import static graphql.Assert.assertNotNull;
54
import static graphql.Assert.assertValidName;
65

76
/**
87
* A special type to allow a object/interface types to reference itself. It's replaced with the real type
98
* object when the schema is build.
9+
*
1010
*/
11-
public class GraphQLTypeReference implements GraphQLType, GraphQLOutputType, GraphQLInputType, TypeReference {
11+
public class GraphQLTypeReference implements GraphQLType, GraphQLOutputType, GraphQLInputType {
1212

1313
private final String name;
1414

0 commit comments

Comments
 (0)