@@ -96,7 +96,7 @@ private GraphQLSchema(Builder builder) {
9696 this .introspectionSchemaType = builder .introspectionSchemaType ;
9797 this .introspectionSchemaField = Introspection .buildSchemaField (builder .introspectionSchemaType );
9898 this .introspectionTypeField = Introspection .buildTypeField (builder .introspectionSchemaType );
99- this .directiveDefinitionsHolder = new DirectivesUtil .DirectivesHolder (builder .additionalDirectives , emptyList ());
99+ this .directiveDefinitionsHolder = new DirectivesUtil .DirectivesHolder (builder .additionalDirectives . values () , emptyList ());
100100 this .schemaAppliedDirectivesHolder = new DirectivesUtil .DirectivesHolder (builder .schemaDirectives , builder .schemaAppliedDirectives );
101101 this .definition = builder .definition ;
102102 this .extensionDefinitions = nonNullCopyOf (builder .extensionDefinitions );
@@ -763,7 +763,7 @@ public static Builder newSchema(GraphQLSchema existingSchema) {
763763 .introspectionSchemaType (existingSchema .getIntrospectionSchemaType ())
764764 .codeRegistry (existingSchema .getCodeRegistry ())
765765 .clearAdditionalTypes ()
766- .additionalDirectives (new LinkedHashSet <>( existingSchema .getDirectives () ))
766+ .additionalDirectives (existingSchema .getDirectives ())
767767 .clearSchemaDirectives ()
768768 .withSchemaDirectives (schemaDirectivesArray (existingSchema ))
769769 .withSchemaAppliedDirectives (schemaAppliedDirectivesArray (existingSchema ))
@@ -813,7 +813,7 @@ public static class Builder {
813813 private List <SchemaExtensionDefinition > extensionDefinitions ;
814814 private String description ;
815815
816- private final Set < GraphQLDirective > additionalDirectives = new LinkedHashSet <>();
816+ private final Map < String , GraphQLDirective > additionalDirectives = new LinkedHashMap <>();
817817 private final Set <GraphQLNamedType > additionalTypes = new LinkedHashSet <>();
818818 private final List <GraphQLDirective > schemaDirectives = new ArrayList <>();
819819 private final List <GraphQLAppliedDirective > schemaAppliedDirectives = new ArrayList <>();
@@ -921,19 +921,49 @@ public Builder clearAdditionalTypes() {
921921 return this ;
922922 }
923923
924+ /**
925+ * Adds multiple directive definitions to the schema.
926+ *
927+ * @param additionalDirectives the directive definitions to add
928+ *
929+ * @return this builder
930+ *
931+ * @deprecated use {@link #additionalDirectives(Collection)} instead
932+ */
933+ @ Deprecated (since = "2026-05-20" )
924934 public Builder additionalDirectives (Set <GraphQLDirective > additionalDirectives ) {
925- this .additionalDirectives .addAll (additionalDirectives );
935+ return additionalDirectives ((Collection <? extends GraphQLDirective >) additionalDirectives );
936+ }
937+
938+ /**
939+ * Adds multiple directive definitions to the schema.
940+ *
941+ * @param additionalDirectives the directive definitions to add
942+ *
943+ * @return this builder
944+ */
945+ public Builder additionalDirectives (Collection <? extends GraphQLDirective > additionalDirectives ) {
946+ for (GraphQLDirective additionalDirective : additionalDirectives ) {
947+ additionalDirective (additionalDirective );
948+ }
926949 return this ;
927950 }
928951
929952 public Builder additionalDirective (GraphQLDirective additionalDirective ) {
930- this .additionalDirectives .add (additionalDirective );
953+ String name = additionalDirective .getName ();
954+ GraphQLDirective existing = additionalDirectives .get (name );
955+ if (existing != null && existing != additionalDirective ) {
956+ throw new AssertException (String .format ("Directive '%s' already exists with a different instance" , name ));
957+ }
958+ if (existing == null ) {
959+ additionalDirectives .put (name , additionalDirective );
960+ }
931961 return this ;
932962 }
933963
934964 /**
935965 * Clears all directives from this builder, including any that were previously added
936- * via {@link #additionalDirective(GraphQLDirective)} or {@link #additionalDirectives(Set )}.
966+ * via {@link #additionalDirective(GraphQLDirective)} or {@link #additionalDirectives(Collection )}.
937967 * Built-in directives ({@code @include}, {@code @skip}, {@code @deprecated}, etc.) will
938968 * always be added back automatically at build time by {@code ensureBuiltInDirectives()}.
939969 * <p>
@@ -1073,19 +1103,15 @@ private GraphQLSchema buildImpl() {
10731103
10741104 private void ensureBuiltInDirectives () {
10751105 // put built-in directives first, preserving user-supplied overrides by name
1076- Set <String > userDirectiveNames = new LinkedHashSet <>();
1077- for (GraphQLDirective d : additionalDirectives ) {
1078- userDirectiveNames .add (d .getName ());
1079- }
1080- LinkedHashSet <GraphQLDirective > ordered = new LinkedHashSet <>();
1106+ Map <String , GraphQLDirective > ordered = new LinkedHashMap <>();
10811107 for (GraphQLDirective builtIn : Directives .BUILT_IN_DIRECTIVES ) {
1082- if (!userDirectiveNames . contains (builtIn .getName ())) {
1083- ordered .add ( builtIn );
1108+ if (!additionalDirectives . containsKey (builtIn .getName ())) {
1109+ ordered .put ( builtIn . getName (), builtIn );
10841110 }
10851111 }
1086- ordered .addAll (additionalDirectives );
1112+ ordered .putAll (additionalDirectives );
10871113 additionalDirectives .clear ();
1088- additionalDirectives .addAll (ordered );
1114+ additionalDirectives .putAll (ordered );
10891115 }
10901116
10911117 private GraphQLSchema validateSchema (GraphQLSchema graphQLSchema ) {
0 commit comments