11package graphql .schema ;
22
33import graphql .AssertException ;
4+ import graphql .DirectivesUtil ;
45import graphql .Internal ;
56import graphql .PublicApi ;
67import graphql .language .ObjectTypeDefinition ;
78
89import java .util .ArrayList ;
10+ import java .util .Collections ;
911import java .util .LinkedHashMap ;
1012import java .util .List ;
1113import java .util .Map ;
1517import static graphql .Assert .assertNotNull ;
1618import static graphql .Assert .assertValidName ;
1719import static java .lang .String .format ;
20+ import static java .util .Collections .emptyList ;
1821
1922/**
2023 * This is the work horse type and represents an object with one or more field values that can be retrieved
2629 * See http://graphql.org/learn/schema/#object-types-and-fields for more details on the concept.
2730 */
2831@ PublicApi
29- public class GraphQLObjectType implements GraphQLType , GraphQLOutputType , GraphQLFieldsContainer , GraphQLCompositeType , GraphQLUnmodifiedType , GraphQLNullableType {
32+ public class GraphQLObjectType implements GraphQLType , GraphQLOutputType , GraphQLFieldsContainer , GraphQLCompositeType , GraphQLUnmodifiedType , GraphQLNullableType , GraphQLDirectiveContainer {
3033
3134
3235 private final String name ;
3336 private final String description ;
3437 private final Map <String , GraphQLFieldDefinition > fieldDefinitionsByName = new LinkedHashMap <>();
3538 private List <GraphQLOutputType > interfaces = new ArrayList <>();
39+ private final List <GraphQLDirective > directives ;
3640 private final ObjectTypeDefinition definition ;
3741
3842 @ Internal
3943 public GraphQLObjectType (String name , String description , List <GraphQLFieldDefinition > fieldDefinitions ,
4044 List <GraphQLOutputType > interfaces ) {
41- this (name , description , fieldDefinitions , interfaces , null );
45+ this (name , description , fieldDefinitions , interfaces , emptyList (), null );
4246 }
4347
4448 @ Internal
4549 public GraphQLObjectType (String name , String description , List <GraphQLFieldDefinition > fieldDefinitions ,
46- List <GraphQLOutputType > interfaces , ObjectTypeDefinition definition ) {
50+ List <GraphQLOutputType > interfaces , List < GraphQLDirective > directives , ObjectTypeDefinition definition ) {
4751 assertValidName (name );
4852 assertNotNull (fieldDefinitions , "fieldDefinitions can't be null" );
4953 assertNotNull (interfaces , "interfaces can't be null" );
5054 this .name = name ;
5155 this .description = description ;
5256 this .interfaces = interfaces ;
5357 this .definition = definition ;
58+ this .directives = assertNotNull (directives );
5459 buildDefinitionMap (fieldDefinitions );
5560 }
5661
@@ -69,6 +74,16 @@ private void buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
6974 }
7075 }
7176
77+ @ Override
78+ public List <GraphQLDirective > getDirectives () {
79+ return Collections .unmodifiableList (directives );
80+ }
81+
82+ @ Override
83+ public Map <String , GraphQLDirective > getDirectivesByName () {
84+ return DirectivesUtil .directivesByName (directives );
85+ }
86+
7287 public GraphQLFieldDefinition getFieldDefinition (String name ) {
7388 return fieldDefinitionsByName .get (name );
7489 }
@@ -121,6 +136,7 @@ public static class Builder {
121136 private String description ;
122137 private final List <GraphQLFieldDefinition > fieldDefinitions = new ArrayList <>();
123138 private final List <GraphQLOutputType > interfaces = new ArrayList <>();
139+ private final List <GraphQLDirective > directives = new ArrayList <>();
124140 private ObjectTypeDefinition definition ;
125141
126142 public Builder name (String name ) {
@@ -209,8 +225,13 @@ public Builder withInterfaces(GraphQLTypeReference... references) {
209225 return this ;
210226 }
211227
228+ public Builder withDirectives (GraphQLDirective ... directives ) {
229+ Collections .addAll (this .directives , directives );
230+ return this ;
231+ }
232+
212233 public GraphQLObjectType build () {
213- return new GraphQLObjectType (name , description , fieldDefinitions , interfaces , definition );
234+ return new GraphQLObjectType (name , description , fieldDefinitions , interfaces , directives , definition );
214235 }
215236
216237 }
0 commit comments