3737import static graphql .collect .ImmutableKit .nonNullCopyOf ;
3838import static graphql .schema .GraphqlTypeComparators .byNameAsc ;
3939import static graphql .schema .GraphqlTypeComparators .sortTypes ;
40- import static java . util . Arrays . asList ;
40+
4141
4242/**
4343 * The schema represents the combined type system of the graphql engine. This is how the engine knows
@@ -692,7 +692,6 @@ public static Builder newSchema(GraphQLSchema existingSchema) {
692692 .introspectionSchemaType (existingSchema .getIntrospectionSchemaType ())
693693 .codeRegistry (existingSchema .getCodeRegistry ())
694694 .clearAdditionalTypes ()
695- .clearDirectives ()
696695 .additionalDirectives (new LinkedHashSet <>(existingSchema .getDirectives ()))
697696 .clearSchemaDirectives ()
698697 .withSchemaDirectives (schemaDirectivesArray (existingSchema ))
@@ -741,10 +740,7 @@ public static class Builder {
741740 private List <SchemaExtensionDefinition > extensionDefinitions ;
742741 private String description ;
743742
744- // we default these in
745- private final Set <GraphQLDirective > additionalDirectives = new LinkedHashSet <>(
746- asList (Directives .IncludeDirective , Directives .SkipDirective )
747- );
743+ private final Set <GraphQLDirective > additionalDirectives = new LinkedHashSet <>();
748744 private final Set <GraphQLNamedType > additionalTypes = new LinkedHashSet <>();
749745 private final List <GraphQLDirective > schemaDirectives = new ArrayList <>();
750746 private final List <GraphQLAppliedDirective > schemaAppliedDirectives = new ArrayList <>();
@@ -862,12 +858,6 @@ public Builder additionalDirective(GraphQLDirective additionalDirective) {
862858 return this ;
863859 }
864860
865- public Builder clearDirectives () {
866- this .additionalDirectives .clear ();
867- return this ;
868- }
869-
870-
871861 public Builder withSchemaDirectives (GraphQLDirective ... directives ) {
872862 for (GraphQLDirective directive : directives ) {
873863 withSchemaDirective (directive );
@@ -960,13 +950,8 @@ private GraphQLSchema buildImpl() {
960950 assertNotNull (additionalTypes , "additionalTypes can't be null" );
961951 assertNotNull (additionalDirectives , "additionalDirectives can't be null" );
962952
963- // schemas built via the schema generator have the deprecated directive BUT we want it present for hand built
964- // schemas - it's inherently part of the spec!
965- addBuiltInDirective (Directives .DeprecatedDirective , additionalDirectives );
966- addBuiltInDirective (Directives .SpecifiedByDirective , additionalDirectives );
967- addBuiltInDirective (Directives .OneOfDirective , additionalDirectives );
968- addBuiltInDirective (Directives .DeferDirective , additionalDirectives );
969- addBuiltInDirective (Directives .ExperimentalDisableErrorPropagationDirective , additionalDirectives );
953+ // built-in directives are always present in a schema and come first
954+ ensureBuiltInDirectives ();
970955
971956 // quick build - no traversing
972957 final GraphQLSchema partiallyBuiltSchema = new GraphQLSchema (this );
@@ -989,10 +974,21 @@ private GraphQLSchema buildImpl() {
989974 return validateSchema (finalSchema );
990975 }
991976
992- private void addBuiltInDirective (GraphQLDirective qlDirective , Set <GraphQLDirective > additionalDirectives1 ) {
993- if (additionalDirectives1 .stream ().noneMatch (d -> d .getName ().equals (qlDirective .getName ()))) {
994- additionalDirectives1 .add (qlDirective );
977+ private void ensureBuiltInDirectives () {
978+ // put built-in directives first, preserving user-supplied overrides by name
979+ Set <String > userDirectiveNames = new LinkedHashSet <>();
980+ for (GraphQLDirective d : additionalDirectives ) {
981+ userDirectiveNames .add (d .getName ());
982+ }
983+ LinkedHashSet <GraphQLDirective > ordered = new LinkedHashSet <>();
984+ for (GraphQLDirective builtIn : Directives .BUILT_IN_DIRECTIVES ) {
985+ if (!userDirectiveNames .contains (builtIn .getName ())) {
986+ ordered .add (builtIn );
987+ }
995988 }
989+ ordered .addAll (additionalDirectives );
990+ additionalDirectives .clear ();
991+ additionalDirectives .addAll (ordered );
996992 }
997993
998994 private GraphQLSchema validateSchema (GraphQLSchema graphQLSchema ) {
0 commit comments