44import graphql .language .ObjectTypeDefinition ;
55
66import java .util .ArrayList ;
7- import java .util .Collections ;
87import java .util .LinkedHashMap ;
98import java .util .List ;
109import java .util .Map ;
1110import java .util .function .UnaryOperator ;
11+ import java .util .stream .Collectors ;
1212
1313import static graphql .Assert .assertNotNull ;
1414import static graphql .Assert .assertValidName ;
1515
16+
1617public 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}
0 commit comments