4242import static graphql .collect .ImmutableKit .nonNullCopyOf ;
4343import static graphql .schema .GraphqlTypeComparators .byNameAsc ;
4444import static graphql .schema .GraphqlTypeComparators .sortTypes ;
45- import static java .util .Arrays .asList ;
4645import static java .util .Collections .singletonList ;
4746
4847/**
@@ -812,9 +811,10 @@ public static class Builder {
812811 private List <SchemaExtensionDefinition > extensionDefinitions ;
813812 private String description ;
814813
815- // we default these in
814+ // We initially add these default directives (e.g., include and skip), but these can be
815+ // cleared by the user (unlike mandatory ones which are always re-added in buildImpl)
816816 private final Set <GraphQLDirective > additionalDirectives = new LinkedHashSet <>(
817- asList ( Directives .IncludeDirective , Directives . SkipDirective )
817+ Directives .getDefaultDirectives ( )
818818 );
819819 private final Set <GraphQLNamedType > additionalTypes = new LinkedHashSet <>();
820820 private final List <GraphQLDirective > schemaDirectives = new ArrayList <>();
@@ -1031,13 +1031,8 @@ private GraphQLSchema buildImpl() {
10311031 assertNotNull (additionalTypes , "additionalTypes can't be null" );
10321032 assertNotNull (additionalDirectives , "additionalDirectives can't be null" );
10331033
1034- // schemas built via the schema generator have the deprecated directive BUT we want it present for hand built
1035- // schemas - it's inherently part of the spec!
1036- addBuiltInDirective (Directives .DeprecatedDirective , additionalDirectives );
1037- addBuiltInDirective (Directives .SpecifiedByDirective , additionalDirectives );
1038- addBuiltInDirective (Directives .OneOfDirective , additionalDirectives );
1039- addBuiltInDirective (Directives .DeferDirective , additionalDirectives );
1040- addBuiltInDirective (Directives .ExperimentalDisableErrorPropagationDirective , additionalDirectives );
1034+ // Mandatory directives are always added, even after clearDirectives() - they're part of the spec
1035+ Directives .getMandatoryDirectives ().forEach (d -> addBuiltInDirective (d , additionalDirectives ));
10411036
10421037 // quick build - no traversing
10431038 final GraphQLSchema partiallyBuiltSchema = new GraphQLSchema (this );
@@ -1060,19 +1055,19 @@ private GraphQLSchema buildImpl() {
10601055 return validateSchema (finalSchema );
10611056 }
10621057
1063- private void addBuiltInDirective (GraphQLDirective qlDirective , Set <GraphQLDirective > additionalDirectives1 ) {
1064- if (additionalDirectives1 .stream ().noneMatch (d -> d .getName ().equals (qlDirective .getName ()))) {
1065- additionalDirectives1 .add (qlDirective );
1066- }
1067- }
1068-
10691058 private GraphQLSchema validateSchema (GraphQLSchema graphQLSchema ) {
10701059 Collection <SchemaValidationError > errors = new SchemaValidator ().validateSchema (graphQLSchema );
10711060 if (!errors .isEmpty ()) {
10721061 throw new InvalidSchemaException (errors );
10731062 }
10741063 return graphQLSchema ;
10751064 }
1065+
1066+ private void addBuiltInDirective (GraphQLDirective qlDirective , Set <GraphQLDirective > additionalDirectives1 ) {
1067+ if (additionalDirectives1 .stream ().noneMatch (d -> d .getName ().equals (qlDirective .getName ()))) {
1068+ additionalDirectives1 .add (qlDirective );
1069+ }
1070+ }
10761071 }
10771072
10781073 /**
@@ -1383,12 +1378,8 @@ public GraphQLSchema build() {
13831378 }
13841379
13851380 private void addBuiltInDirectivesIfMissing () {
1386- addDirectiveIfMissing (Directives .IncludeDirective );
1387- addDirectiveIfMissing (Directives .SkipDirective );
1388- addDirectiveIfMissing (Directives .DeprecatedDirective );
1389- addDirectiveIfMissing (Directives .SpecifiedByDirective );
1390- addDirectiveIfMissing (Directives .OneOfDirective );
1391- addDirectiveIfMissing (Directives .DeferDirective );
1381+ Directives .getDefaultDirectives ().forEach (this ::addDirectiveIfMissing );
1382+ Directives .getMandatoryDirectives ().forEach (this ::addDirectiveIfMissing );
13921383 }
13931384
13941385 private void addDirectiveIfMissing (GraphQLDirective directive ) {
0 commit comments