22
33import com .google .common .collect .ImmutableList ;
44import com .google .common .collect .ImmutableMap ;
5+ import graphql .Directives ;
56import graphql .DirectivesUtil ;
67import graphql .Internal ;
78import graphql .PublicApi ;
3435public class GraphQLInputObjectType implements GraphQLNamedInputType , GraphQLUnmodifiedType , GraphQLNullableType , GraphQLInputFieldsContainer , GraphQLDirectiveContainer {
3536
3637 private final String name ;
38+
39+ private final boolean isOneOf ;
3740 private final String description ;
3841 private final ImmutableMap <String , GraphQLInputObjectField > fieldMap ;
3942 private final InputObjectTypeDefinition definition ;
@@ -60,18 +63,37 @@ private GraphQLInputObjectType(String name,
6063 this .extensionDefinitions = ImmutableList .copyOf (extensionDefinitions );
6164 this .directives = new DirectivesUtil .DirectivesHolder (directives , appliedDirectives );
6265 this .fieldMap = buildDefinitionMap (fields );
66+ this .isOneOf = hasOneOf (directives , appliedDirectives );
6367 }
6468
6569 private ImmutableMap <String , GraphQLInputObjectField > buildDefinitionMap (List <GraphQLInputObjectField > fieldDefinitions ) {
6670 return ImmutableMap .copyOf (FpKit .getByName (fieldDefinitions , GraphQLInputObjectField ::getName ,
6771 (fld1 , fld2 ) -> assertShouldNeverHappen ("Duplicated definition for field '%s' in type '%s'" , fld1 .getName (), this .name )));
6872 }
6973
74+ private boolean hasOneOf (List <GraphQLDirective > directives , List <GraphQLAppliedDirective > appliedDirectives ) {
75+ if (appliedDirectives .stream ().anyMatch (d -> Directives .OneOfDirective .getName ().equals (d .getName ()))) {
76+ return true ;
77+ }
78+ // eventually GraphQLDirective as applied directive goes away
79+ return directives .stream ().anyMatch (d -> Directives .OneOfDirective .getName ().equals (d .getName ()));
80+ }
81+
7082 @ Override
7183 public String getName () {
7284 return name ;
7385 }
7486
87+
88+ /**
89+ * An Input Object is considered a OneOf Input Object if it has the `@oneOf` directive applied to it.
90+ *
91+ * @return true if it's a OneOf Input Object
92+ */
93+ public boolean isOneOf () {
94+ return isOneOf ;
95+ }
96+
7597 public String getDescription () {
7698 return description ;
7799 }
0 commit comments