11package graphql .schema ;
22
3- import graphql .AssertException ;
4-
53import java .util .ArrayList ;
4+ import java .util .Collections ;
65import java .util .LinkedHashMap ;
76import java .util .List ;
87import java .util .Map ;
98
9+ import graphql .AssertException ;
10+
1011import static graphql .Assert .assertNotNull ;
1112
1213public class GraphQLObjectType implements GraphQLType , GraphQLOutputType , GraphQLFieldsContainer , GraphQLCompositeType , GraphQLUnmodifiedType , GraphQLNullableType {
@@ -16,16 +17,27 @@ public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQ
1617 private final Map <String , GraphQLFieldDefinition > fieldDefinitionsByName = new LinkedHashMap <String , GraphQLFieldDefinition >();
1718 private final List <GraphQLInterfaceType > interfaces = new ArrayList <GraphQLInterfaceType >();
1819
19- public GraphQLObjectType (String name , String description , List <GraphQLFieldDefinition > fieldDefinitions , List <GraphQLInterfaceType > interfaces ) {
20+ public GraphQLObjectType (String name , String description , List <GraphQLFieldDefinition > fieldDefinitions ,
21+ List <GraphQLInterfaceType > interfaces ) {
2022 assertNotNull (name , "name can't be null" );
2123 assertNotNull (fieldDefinitions , "fieldDefinitions can't be null" );
2224 assertNotNull (interfaces , "interfaces can't be null" );
25+ assertNotNull (interfaces , "unresolvedInterfaces can't be null" );
2326 this .name = name ;
2427 this .description = description ;
2528 this .interfaces .addAll (interfaces );
2629 buildDefinitionMap (fieldDefinitions );
2730 }
2831
32+ void replaceTypeReferences (Map <String , GraphQLType > typeMap ) {
33+ for (int i = 0 ; i < interfaces .size (); i ++) {
34+ GraphQLInterfaceType inter = interfaces .get (i );
35+ if (inter instanceof TypeReference ) {
36+ this .interfaces .set (i , (GraphQLInterfaceType ) new SchemaUtil ().resolveTypeReference (inter , typeMap ));
37+ }
38+ }
39+ }
40+
2941 private void buildDefinitionMap (List <GraphQLFieldDefinition > fieldDefinitions ) {
3042 for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions ) {
3143 String name = fieldDefinition .getName ();
@@ -35,7 +47,6 @@ private void buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
3547 }
3648 }
3749
38-
3950 public GraphQLFieldDefinition getFieldDefinition (String name ) {
4051 return fieldDefinitionsByName .get (name );
4152 }
@@ -74,7 +85,10 @@ public static Builder newObject() {
7485 return new Builder ();
7586 }
7687
77-
88+ public static Reference reference (String name ) {
89+ return new Reference (name );
90+ }
91+
7892 public static class Builder {
7993 private String name ;
8094 private String description ;
@@ -151,6 +165,11 @@ public GraphQLObjectType build() {
151165 return new GraphQLObjectType (name , description , fieldDefinitions , interfaces );
152166 }
153167
168+ }
154169
170+ private static class Reference extends GraphQLObjectType implements TypeReference {
171+ private Reference (String name ) {
172+ super (name , "" , Collections .<GraphQLFieldDefinition >emptyList (), Collections .<GraphQLInterfaceType >emptyList ());
173+ }
155174 }
156175}
0 commit comments