diff --git a/src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs b/src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs
index c97ba72c5f6..c9e4a306a3c 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs
@@ -1,6 +1,7 @@
using HotChocolate.Events.Contracts;
using HotChocolate.Language;
using HotChocolate.Types;
+using DirectiveLocation = HotChocolate.Types.DirectiveLocation;
namespace HotChocolate.Events;
@@ -45,9 +46,13 @@ public readonly record struct DirectiveArgumentAssignmentEvent(
///
/// Represents an event that is triggered when a directive is encountered during schema validation.
///
+/// The applied directive.
+/// The type system member the directive is applied to.
+/// The location at which the directive is applied.
public readonly record struct DirectiveEvent(
IDirective Directive,
- ITypeSystemMember Member) : IValidationEvent;
+ ITypeSystemMember Member,
+ DirectiveLocation Location) : IValidationEvent;
///
/// Represents an event that is triggered when an Enum type is encountered during schema validation.
diff --git a/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryCodes.cs b/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryCodes.cs
index c793227309d..7e46af4d474 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryCodes.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryCodes.cs
@@ -36,4 +36,5 @@ internal static class LogEntryCodes
public const string IncompatibleInputFieldDefaultValue = "HCV0029";
public const string InvalidObjectDeprecation = "HCV0030";
public const string DirectiveNotUnique = "HCV0031";
+ public const string DirectiveInInvalidLocation = "HCV0032";
}
diff --git a/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryHelper.cs b/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryHelper.cs
index e16bc465af0..e770f500c6a 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryHelper.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Logging/LogEntryHelper.cs
@@ -67,6 +67,23 @@ public static LogEntry DirectiveDefinitionSelfApplication(IDirectiveDefinition d
.Build();
}
+ public static LogEntry DirectiveInInvalidLocation(
+ IDirective directive,
+ ITypeSystemMember member,
+ DirectiveLocation location)
+ {
+ return LogEntryBuilder.New()
+ .SetMessage(
+ LogEntryHelper_DirectiveInInvalidLocation,
+ directive.Name,
+ GetDirectiveMemberName(member),
+ location.Format())
+ .SetCode(LogEntryCodes.DirectiveInInvalidLocation)
+ .SetSeverity(LogSeverity.Error)
+ .SetTypeSystemMember(member)
+ .Build();
+ }
+
public static LogEntry DirectiveNotUnique(IDirective directive, ITypeSystemMember member)
{
return LogEntryBuilder.New()
diff --git a/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.Designer.cs b/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.Designer.cs
index 9fc644509c9..80776c8f4b5 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.Designer.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.Designer.cs
@@ -185,6 +185,15 @@ internal static string LogEntryHelper_DirectiveDefinitionSelfApplication {
}
}
+ ///
+ /// Looks up a localized string similar to The directive '@{0}' on '{1}' is not allowed on the location '{2}'..
+ ///
+ internal static string LogEntryHelper_DirectiveInInvalidLocation {
+ get {
+ return ResourceManager.GetString("LogEntryHelper_DirectiveInInvalidLocation", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to The non-repeatable directive '@{0}' on '{1}' is applied more than once..
///
diff --git a/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.resx b/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.resx
index 90040acc9d8..306c519b2ac 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.resx
+++ b/src/HotChocolate/Core/src/Types.Validation/Properties/ValidationResources.resx
@@ -60,6 +60,9 @@
The directive definition '@{0}' must not reference itself.
+
+ The directive '@{0}' on '{1}' is not allowed on the location '{2}'.
+
The non-repeatable directive '@{0}' on '{1}' is applied more than once.
diff --git a/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsDefinedRule.cs b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsDefinedRule.cs
index e000c5708bd..503590d0f2e 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsDefinedRule.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsDefinedRule.cs
@@ -15,7 +15,7 @@ public sealed class DirectiveIsDefinedRule : IValidationEventHandler
public void Handle(DirectiveEvent @event, ValidationContext context)
{
- var (directive, member) = @event;
+ var (directive, member, _) = @event;
if (DirectiveNames.IsSpecDirective(directive.Name))
{
diff --git a/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsInValidLocationRule.cs b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsInValidLocationRule.cs
new file mode 100644
index 00000000000..393bf715b86
--- /dev/null
+++ b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsInValidLocationRule.cs
@@ -0,0 +1,33 @@
+using HotChocolate.Events;
+using HotChocolate.Events.Contracts;
+using HotChocolate.Types;
+using static HotChocolate.Logging.LogEntryHelper;
+
+namespace HotChocolate.Rules;
+
+///
+/// Checks that a directive is applied only in locations its definition declares.
+///
+public sealed class DirectiveIsInValidLocationRule : IValidationEventHandler
+{
+ ///
+ /// Checks that a directive is applied only in locations its definition declares.
+ ///
+ public void Handle(DirectiveEvent @event, ValidationContext context)
+ {
+ var (directive, member, location) = @event;
+
+ // DirectiveIsDefinedRule reports directives without a definition, and
+ // DirectiveDefinitionIncludesLocationRule reports definitions without locations.
+ if (directive.Definition is IMissingDirectiveDefinition
+ || directive.Definition.Locations == 0)
+ {
+ return;
+ }
+
+ if ((directive.Definition.Locations & location) != location)
+ {
+ context.Log.Write(DirectiveInInvalidLocation(directive, member, location));
+ }
+ }
+}
diff --git a/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsUniqueRule.cs b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsUniqueRule.cs
index 5d493735969..fde3cafdfd6 100644
--- a/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsUniqueRule.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/Rules/DirectiveIsUniqueRule.cs
@@ -15,7 +15,7 @@ public sealed class DirectiveIsUniqueRule : IValidationEventHandler
public void Handle(DirectiveEvent @event, ValidationContext context)
{
- var (directive, member) = @event;
+ var (directive, member, _) = @event;
if (directive.Definition.IsRepeatable || member is not IDirectivesProvider provider)
{
diff --git a/src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs b/src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs
index 0ea26998999..2d2c0acaee8 100644
--- a/src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs
+++ b/src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs
@@ -4,6 +4,7 @@
using HotChocolate.Logging.Contracts;
using HotChocolate.Rules;
using HotChocolate.Types;
+using DirectiveLocation = HotChocolate.Types.DirectiveLocation;
namespace HotChocolate;
@@ -52,6 +53,7 @@ public void AddDefaultRules()
_rules.Add(new DirectiveDefinitionIncludesLocationRule());
_rules.Add(new DirectiveDefinitionNoSelfReferenceRule());
_rules.Add(new DirectiveIsDefinedRule());
+ _rules.Add(new DirectiveIsInValidLocationRule());
_rules.Add(new DirectiveIsUniqueRule());
_rules.Add(new EnumValueIsDefinedRule());
_rules.Add(new NoInputObjectCycleRule());
@@ -96,12 +98,12 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new InputObjectTypesEvent(schema.Types.OfType()), context);
- PublishDirectiveEvents(schema, context);
+ PublishDirectiveEvents(schema, DirectiveLocation.Schema, context);
foreach (var type in schema.Types)
{
PublishEvent(new TypeEvent(type), context);
- PublishDirectiveEvents(type, context);
+ PublishDirectiveEvents(type, GetTypeDirectiveLocation(type), context);
if (type is INameProvider namedMember)
{
@@ -136,10 +138,13 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(argument), context);
PublishDefaultValueNodeEvents(argument, context);
- PublishDirectiveEvents(argument, context);
+ PublishDirectiveEvents(
+ argument,
+ DirectiveLocation.ArgumentDefinition,
+ context);
}
- PublishDirectiveEvents(field, context);
+ PublishDirectiveEvents(field, DirectiveLocation.FieldDefinition, context);
}
break;
@@ -152,7 +157,7 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new EnumValueEvent(value), context);
PublishEvent(new NamedMemberEvent(value), context);
- PublishDirectiveEvents(value, context);
+ PublishDirectiveEvents(value, DirectiveLocation.EnumValue, context);
}
break;
@@ -168,7 +173,10 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(field), context);
PublishDefaultValueNodeEvents(field, context);
- PublishDirectiveEvents(field, context);
+ PublishDirectiveEvents(
+ field,
+ DirectiveLocation.InputFieldDefinition,
+ context);
}
break;
@@ -184,7 +192,10 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new DirectiveDefinitionEvent(directiveDefinition), context);
PublishEvent(new NamedMemberEvent(directiveDefinition), context);
- PublishDirectiveEvents(directiveDefinition, context);
+ PublishDirectiveEvents(
+ directiveDefinition,
+ DirectiveLocation.DirectiveDefinition,
+ context);
foreach (var argument in directiveDefinition.Arguments)
{
@@ -193,18 +204,19 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(argument), context);
PublishDefaultValueNodeEvents(argument, context);
- PublishDirectiveEvents(argument, context);
+ PublishDirectiveEvents(argument, DirectiveLocation.ArgumentDefinition, context);
}
}
}
private void PublishDirectiveEvents(
IDirectivesProvider member,
+ DirectiveLocation location,
ValidationContext context)
{
foreach (var directive in member.Directives)
{
- PublishEvent(new DirectiveEvent(directive, member), context);
+ PublishEvent(new DirectiveEvent(directive, member, location), context);
foreach (var argumentAssignment in directive.Arguments)
{
@@ -226,6 +238,21 @@ private void PublishDirectiveEvents(
}
}
+ private static DirectiveLocation GetTypeDirectiveLocation(ITypeDefinition type)
+ {
+ return type.Kind switch
+ {
+ TypeKind.Enum => DirectiveLocation.Enum,
+ TypeKind.InputObject => DirectiveLocation.InputObject,
+ TypeKind.Interface => DirectiveLocation.Interface,
+ TypeKind.Object => DirectiveLocation.Object,
+ TypeKind.Scalar => DirectiveLocation.Scalar,
+ TypeKind.Union => DirectiveLocation.Union,
+ TypeKind.Directive or TypeKind.List or TypeKind.NonNull or _ =>
+ throw new InvalidOperationException()
+ };
+ }
+
private void PublishDefaultValueNodeEvents(IInputValueDefinition inputValue, ValidationContext context)
{
if (inputValue.DefaultValue is { } defaultValue)
diff --git a/src/HotChocolate/Core/test/Types.Validation.Tests/Rules/DirectiveIsInValidLocationRuleTests.cs b/src/HotChocolate/Core/test/Types.Validation.Tests/Rules/DirectiveIsInValidLocationRuleTests.cs
new file mode 100644
index 00000000000..8863ca0b30e
--- /dev/null
+++ b/src/HotChocolate/Core/test/Types.Validation.Tests/Rules/DirectiveIsInValidLocationRuleTests.cs
@@ -0,0 +1,389 @@
+using HotChocolate.Rules;
+
+namespace HotChocolate.Types.Validation.Rules;
+
+public sealed class DirectiveIsInValidLocationRuleTests : RuleTestBase
+{
+ [Fact]
+ public void Validate_DirectiveAppliedAtDeclaredLocations_Succeeds()
+ {
+ AssertValid(
+ """
+ schema @example {
+ query: Query
+ }
+
+ type Query @example {
+ field(argument: Int @example): Int @example
+ }
+
+ scalar Scalar @example
+
+ interface Interface @example {
+ field: Int
+ }
+
+ union Union @example = Query
+
+ enum Enum @example {
+ VALUE @example
+ }
+
+ input Input @example {
+ field: Int @example
+ }
+
+ directive @other(argument: Int @example) @example on OBJECT
+
+ directive @example on ARGUMENT_DEFINITION
+ | DIRECTIVE_DEFINITION
+ | ENUM
+ | ENUM_VALUE
+ | FIELD_DEFINITION
+ | INPUT_FIELD_DEFINITION
+ | INPUT_OBJECT
+ | INTERFACE
+ | OBJECT
+ | SCALAR
+ | SCHEMA
+ | UNION
+ """);
+ }
+
+ [Fact]
+ public void Validate_RepeatableDirectiveAppliedAtDeclaredLocation_Succeeds()
+ {
+ AssertValid(
+ """
+ type Object @example @example {
+ field: Int
+ }
+
+ directive @example repeatable on OBJECT
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveDefinitionWithoutLocations_Succeeds()
+ {
+ // DirectiveDefinitionIncludesLocationRule reports the definition itself.
+ AssertValid(
+ """
+ type Object @example {
+ field: Int
+ }
+
+ directive @example on OBJECT
+ """,
+ schema => schema.DirectiveDefinitions["example"].Locations = 0);
+ }
+
+ [Fact]
+ public void Validate_UndefinedDirective_Succeeds()
+ {
+ AssertValid(
+ """
+ type Object @example {
+ field: Int
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnSchema_Fails()
+ {
+ AssertInvalid(
+ """
+ schema @example {
+ query: Query
+ }
+
+ type Query {
+ field: Int
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'schema' is not allowed on the location 'SCHEMA'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "member": "default",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnScalar_Fails()
+ {
+ AssertInvalid(
+ """
+ scalar Scalar @example
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Scalar' is not allowed on the location 'SCALAR'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Scalar",
+ "member": "Scalar",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnObject_Fails()
+ {
+ AssertInvalid(
+ """
+ type Object @example {
+ field: Int
+ }
+
+ directive @example on FIELD_DEFINITION
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Object' is not allowed on the location 'OBJECT'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Object",
+ "member": "Object",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnFieldDefinition_Fails()
+ {
+ AssertInvalid(
+ """
+ type Object {
+ field: Int @example
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Object.field' is not allowed on the location 'FIELD_DEFINITION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Object.field",
+ "member": "field",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnArgumentDefinition_Fails()
+ {
+ AssertInvalid(
+ """
+ type Object {
+ field(argument: Int @example): Int
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Object.field(argument:)' is not allowed on the location 'ARGUMENT_DEFINITION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Object.field(argument:)",
+ "member": "argument",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnInterface_Fails()
+ {
+ AssertInvalid(
+ """
+ interface Interface @example {
+ field: Int
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Interface' is not allowed on the location 'INTERFACE'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Interface",
+ "member": "Interface",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnUnion_Fails()
+ {
+ AssertInvalid(
+ """
+ type Object {
+ field: Int
+ }
+
+ union Union @example = Object
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Union' is not allowed on the location 'UNION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Union",
+ "member": "Union",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnEnum_Fails()
+ {
+ AssertInvalid(
+ """
+ enum Enum @example {
+ VALUE
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Enum' is not allowed on the location 'ENUM'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Enum",
+ "member": "Enum",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnEnumValue_Fails()
+ {
+ AssertInvalid(
+ """
+ enum Enum {
+ VALUE @example
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Enum.VALUE' is not allowed on the location 'ENUM_VALUE'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Enum.VALUE",
+ "member": "VALUE",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnInputObject_Fails()
+ {
+ AssertInvalid(
+ """
+ input Input @example {
+ field: Int
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Input' is not allowed on the location 'INPUT_OBJECT'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Input",
+ "member": "Input",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnInputFieldDefinition_Fails()
+ {
+ AssertInvalid(
+ """
+ input Input {
+ field: Int @example
+ }
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on 'Input.field' is not allowed on the location 'INPUT_FIELD_DEFINITION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "Input.field",
+ "member": "field",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnDirectiveDefinition_Fails()
+ {
+ AssertInvalid(
+ """
+ directive @other @example on OBJECT
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on '@other' is not allowed on the location 'DIRECTIVE_DEFINITION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "@other",
+ "member": "@other",
+ "extensions": {}
+ }
+ """);
+ }
+
+ [Fact]
+ public void Validate_DirectiveNotDeclaredOnDirectiveArgumentDefinition_Fails()
+ {
+ AssertInvalid(
+ """
+ directive @other(argument: Int @example) on OBJECT
+
+ directive @example on OBJECT
+ """,
+ """
+ {
+ "message": "The directive '@example' on '@other(argument:)' is not allowed on the location 'ARGUMENT_DEFINITION'.",
+ "code": "HCV0032",
+ "severity": "Error",
+ "coordinate": "@other(argument:)",
+ "member": "argument",
+ "extensions": {}
+ }
+ """);
+ }
+}
diff --git a/src/HotChocolate/Core/test/Types.Validation.Tests/SchemaValidatorTests.cs b/src/HotChocolate/Core/test/Types.Validation.Tests/SchemaValidatorTests.cs
index 2dd8a8e1dfd..7e3cc6df794 100644
--- a/src/HotChocolate/Core/test/Types.Validation.Tests/SchemaValidatorTests.cs
+++ b/src/HotChocolate/Core/test/Types.Validation.Tests/SchemaValidatorTests.cs
@@ -25,6 +25,7 @@ public void AddDefaultRules_Should_RegisterTheDefaultRuleSet()
"DirectiveDefinitionIncludesLocationRule",
"DirectiveDefinitionNoSelfReferenceRule",
"DirectiveIsDefinedRule",
+ "DirectiveIsInValidLocationRule",
"DirectiveIsUniqueRule",
"EnumValueIsDefinedRule",
"NoInputObjectCycleRule",
diff --git a/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/GenerateLookupFields.cs b/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/GenerateLookupFields.cs
index 5337aac494e..600214b28ba 100644
--- a/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/GenerateLookupFields.cs
+++ b/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/GenerateLookupFields.cs
@@ -54,10 +54,10 @@ public static void Apply(MutableSchemaDefinition schema)
synthesizedQuery = true;
}
- var internalDef = new MutableDirectiveDefinition("internal");
- var lookupDef = new MutableDirectiveDefinition("lookup");
- var isDef = new MutableDirectiveDefinition("is");
- var shareableDef = new MutableDirectiveDefinition("shareable");
+ var internalDef = FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.Internal];
+ var lookupDef = FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.Lookup];
+ var isDef = FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.Is];
+ var shareableDef = FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.Shareable];
// Generated input object types are cached by name so that repeated
// references to the same nested shape share a single input type.
diff --git a/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/TransformRequiresToRequire.cs b/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/TransformRequiresToRequire.cs
index f070d10093d..ddcbc1747b9 100644
--- a/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/TransformRequiresToRequire.cs
+++ b/src/HotChocolate/Fusion/src/Fusion.Composition/ApolloFederation/TransformRequiresToRequire.cs
@@ -35,7 +35,7 @@ internal static class TransformRequiresToRequire
///
public static void Apply(MutableSchemaDefinition schema)
{
- var requireDef = new MutableDirectiveDefinition("require");
+ var requireDef = FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.Require];
var pendingInputTypes = new List();
// Snapshot the object types to avoid modifying the collection during iteration.
@@ -605,14 +605,14 @@ private static string FormatFieldSelection(FieldNode fieldNode)
}
private static bool IsExternal(MutableOutputFieldDefinition field)
- => field.Directives.ContainsName(FederationDirectiveNames.External);
+ => field.Directives.ContainsName(WellKnownDirectiveNames.External);
private static void ApplyExternalDirective(MutableOutputFieldDefinition field)
{
if (!IsExternal(field))
{
var externalDirectiveDefinition =
- new MutableDirectiveDefinition(FederationDirectiveNames.External);
+ FusionBuiltIns.SourceSchemaDirectives[WellKnownDirectiveNames.External];
field.Directives.Add(new Directive(externalDirectiveDefinition));
}
diff --git a/src/HotChocolate/Fusion/src/Fusion.Composition/SourceSchemaParser.cs b/src/HotChocolate/Fusion/src/Fusion.Composition/SourceSchemaParser.cs
index 7e1fa6a809b..0bac5c91bcb 100644
--- a/src/HotChocolate/Fusion/src/Fusion.Composition/SourceSchemaParser.cs
+++ b/src/HotChocolate/Fusion/src/Fusion.Composition/SourceSchemaParser.cs
@@ -7,6 +7,7 @@
using HotChocolate.Fusion.Options;
using HotChocolate.Fusion.Results;
using HotChocolate.Logging;
+using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using HotChocolate.Types.Mutable.Serialization;
using FusionLogEntryBuilder = HotChocolate.Fusion.Logging.LogEntryBuilder;
@@ -39,14 +40,14 @@ public CompositionResult Parse()
FederationV1DirectiveDefinitions.Apply(schema);
}
- // Apollo Federation's @requires directive has no Fusion-native definition (the Fusion
- // equivalent @require differs in name and argument shape). Register it before parsing
- // federation source schemas so an applied @requires binds to a real definition instead
- // of a missing one; preprocessing then rewrites it to @require and removes the
- // definition. A non-federation schema does not get the definition, so an applied
- // @requires is reported as an unknown directive, steering authors to @require.
if (!isApolloFederationV1 && IsFederationSourceText(sourceSchemaText))
{
+ // Apollo Federation's @requires directive has no Fusion-native definition (the Fusion
+ // equivalent @require differs in name and argument shape). Register it before parsing
+ // federation source schemas so an applied @requires binds to a real definition instead
+ // of a missing one; preprocessing then rewrites it to @require and removes the
+ // definition. A non-federation schema does not get the definition, so an applied
+ // @requires is reported as an unknown directive, steering authors to @require.
if (schema.Types.TryGetType(
WellKnownTypeNames.FieldSelectionSet, out var fieldSelectionSetType))
{
@@ -54,6 +55,17 @@ public CompositionResult Parse()
new RequiresMutableDirectiveDefinition(fieldSelectionSetType));
}
+ // Apollo Federation's @external may also be applied to object types, which the
+ // Fusion @external definition does not allow. Replace it so federation applications
+ // bind to the federation shape; RemoveFederationInfrastructure drops the definition
+ // during preprocessing.
+ schema.DirectiveDefinitions.Remove(WellKnownDirectiveNames.External);
+ schema.DirectiveDefinitions.Add(
+ new MutableDirectiveDefinition(FederationDirectiveNames.External)
+ {
+ Locations = DirectiveLocation.FieldDefinition | DirectiveLocation.Object
+ });
+
// @link carries the federation vocabulary a subgraph imports and is applied to the
// schema itself. RemoveFederationInfrastructure drops the definition and every
// application during preprocessing.