Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/All.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@
<Project Path="HotChocolate/Fusion/src/Fusion.Aspire/HotChocolate.Fusion.Aspire.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.AspNetCore/HotChocolate.Fusion.AspNetCore.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.Composition/HotChocolate.Fusion.Composition.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.Composition.ApolloFederation/HotChocolate.Fusion.Composition.ApolloFederation.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.Caching/HotChocolate.Fusion.Caching.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.Diagnostics/HotChocolate.Fusion.Diagnostics.csproj" />
<Project Path="HotChocolate/Fusion/src/Fusion.Execution.Types/HotChocolate.Fusion.Execution.Types.csproj" />
Expand Down
1 change: 0 additions & 1 deletion src/HotChocolate/Fusion/HotChocolate.Fusion.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Project Path="src/Fusion.Aspire/HotChocolate.Fusion.Aspire.csproj" />
<Project Path="src/Fusion.AspNetCore/HotChocolate.Fusion.AspNetCore.csproj" />
<Project Path="src/Fusion.Composition/HotChocolate.Fusion.Composition.csproj" />
<Project Path="src/Fusion.Composition.ApolloFederation/HotChocolate.Fusion.Composition.ApolloFederation.csproj" />
<Project Path="src/Fusion.Connectors.ApolloFederation/HotChocolate.Fusion.Connectors.ApolloFederation.csproj" />
<Project Path="src/Fusion.Caching/HotChocolate.Fusion.Caching.csproj" />
<Project Path="src/Fusion.Diagnostics/HotChocolate.Fusion.Diagnostics.csproj" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HotChocolate.Fusion.Errors;
using HotChocolate.Fusion.Logging;
using HotChocolate.Fusion.Logging.Contracts;
using HotChocolate.Language;
using HotChocolate.Types.Mutable;
using static HotChocolate.Fusion.ApolloFederation.Properties.FederationResources;
Expand All @@ -11,7 +12,7 @@ namespace HotChocolate.Fusion.ApolloFederation;
/// </summary>
internal static class FederationSchemaAnalyzer
{
private const string FederationUrlPrefix = "specs.apollo.dev/federation";
internal const string FederationUrlPrefix = "specs.apollo.dev/federation";

private static readonly HashSet<string> s_unsupportedDirectives =
[
Expand All @@ -23,33 +24,45 @@ internal static class FederationSchemaAnalyzer
];

/// <summary>
/// Validates the given federation schema and returns any composition errors.
/// Validates the given federation schema and writes any composition errors to the
/// provided <paramref name="log"/>.
/// </summary>
/// <param name="schema">
/// The mutable schema definition to validate.
/// </param>
/// <param name="log">
/// The composition log that receives validation errors.
/// </param>
/// <returns>
/// A list of <see cref="CompositionError"/> instances. An empty list indicates success.
/// <c>true</c> when validation passed (no errors were written); otherwise, <c>false</c>.
/// </returns>
public static List<CompositionError> Validate(MutableSchemaDefinition schema)
public static bool Validate(MutableSchemaDefinition schema, ICompositionLog log)
{
var errors = new List<CompositionError>();
var entryCountBefore = log.Count();

ValidateFederationVersion(schema, errors);
ValidateUnsupportedDirectives(schema, errors);
ValidateFederationVersion(schema, log);
ValidateUnsupportedDirectives(schema, log);

return errors;
// Pre-existing entries on the log are not ours to report on; this run reports
// failure only when it wrote new entries (all of which are errors) of its own.
return log.Count() == entryCountBefore;
}

private static void ValidateFederationVersion(
MutableSchemaDefinition schema,
List<CompositionError> errors)
ICompositionLog log)
{
var federationVersion = FindFederationVersion(schema);

if (federationVersion is null)
{
errors.Add(new CompositionError(FederationSchemaAnalyzer_FederationV1NotSupported));
log.Write(
LogEntryBuilder.New()
.SetMessage(FederationSchemaAnalyzer_FederationV1NotSupported)
.SetCode(LogEntryCodes.FederationV1NotSupported)
.SetSeverity(LogSeverity.Error)
.SetSchema(schema)
.Build());
}
}

Expand Down Expand Up @@ -90,15 +103,21 @@ private static void ValidateFederationVersion(

private static void ValidateUnsupportedDirectives(
MutableSchemaDefinition schema,
List<CompositionError> errors)
ICompositionLog log)
{
foreach (var name in s_unsupportedDirectives)
{
if (schema.DirectiveDefinitions.ContainsName(name))
{
errors.Add(new CompositionError(string.Format(
FederationSchemaAnalyzer_DirectiveNotSupported,
name)));
log.Write(
LogEntryBuilder.New()
.SetMessage(
FederationSchemaAnalyzer_DirectiveNotSupported,
name)
.SetCode(LogEntryCodes.FederationDirectiveNotSupported)
.SetSeverity(LogSeverity.Error)
.SetSchema(schema)
.Build());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Errors;
using HotChocolate.Fusion.Logging;
using HotChocolate.Fusion.Results;
using HotChocolate.Language;
using HotChocolate.Serialization;
Expand All @@ -15,6 +16,47 @@ namespace HotChocolate.Fusion.ApolloFederation;
/// </summary>
public static class FederationSchemaTransformer
{
/// <summary>
/// Determines whether the given schema is an Apollo Federation v2 subgraph
/// based on the presence of an <c>@link</c> directive that imports the
/// Apollo Federation specification.
/// </summary>
/// <param name="schema">
/// The mutable schema definition to inspect.
/// </param>
/// <returns>
/// <c>true</c> when the schema declares an <c>@link</c> directive whose
/// <c>url</c> argument references the Apollo Federation specification;
/// otherwise, <c>false</c>.
/// </returns>
public static bool IsFederationSchema(MutableSchemaDefinition schema)
{
ArgumentNullException.ThrowIfNull(schema);

foreach (var directive in schema.Directives)
{
if (!directive.Name.Equals(FederationDirectiveNames.Link, StringComparison.Ordinal))
{
continue;
}

if (!directive.Arguments.TryGetValue("url", out var urlValue)
|| urlValue is not StringValueNode urlString)
{
continue;
}

if (urlString.Value.Contains(
FederationSchemaAnalyzer.FederationUrlPrefix,
StringComparison.Ordinal))
{
return true;
}
}

return false;
}

/// <summary>
/// Transforms the given Apollo Federation v2 subgraph SDL.
/// </summary>
Expand All @@ -41,18 +83,21 @@ public static CompositionResult<string> Transform(string federationSdl)
string.Format(FederationSchemaTransformer_ParseFailed, ex.Message));
}

var errors = FederationSchemaAnalyzer.Validate(schema);
var log = new CompositionLog();

if (errors.Count > 0)
if (!FederationSchemaAnalyzer.Validate(schema, log))
{
return errors.ToImmutableArray();
return log
.Select(entry => new CompositionError(entry.Message))
.ToImmutableArray();
}

RemoveFederationInfrastructure.Apply(schema);
GenerateLookupFields.Apply(schema);
RewriteKeyDirectives.Apply(schema);
TransformRequiresToRequire.Apply(schema);
RemoveExternalFields.Apply(schema);
StampConnectorKind.Apply(schema);

return SchemaFormatter.FormatAsString(schema);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using HotChocolate.Fusion.Definitions;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Types.Mutable;

namespace HotChocolate.Fusion.ApolloFederation;

/// <summary>
/// Stamps the <c>@fusion__connector(kind: "Apollo")</c> directive onto the source schema
/// so the composer can lift the kind onto the corresponding <c>fusion__Schema</c> enum value
/// in the merged execution schema.
/// </summary>
internal static class StampConnectorKind
{
private const string ApolloKind = "Apollo";

/// <summary>
/// Applies the connector kind stamp to the schema. Idempotent: the directive
/// definition is registered only when missing, and a duplicate
/// <c>@fusion__connector(kind: "Apollo")</c> instance is not added when one
/// is already present.
/// </summary>
/// <param name="schema">
/// The mutable schema definition to stamp in place.
/// </param>
public static void Apply(MutableSchemaDefinition schema)
{
if (!schema.DirectiveDefinitions.TryGetDirective(
WellKnownDirectiveNames.FusionConnector,
out var connectorDefinition))
{
connectorDefinition = new ConnectorMutableDirectiveDefinition(BuiltIns.String.Create());
schema.DirectiveDefinitions.Add(connectorDefinition);
}

foreach (var existing in schema.Directives[WellKnownDirectiveNames.FusionConnector])
{
if (existing.Arguments.TryGetValue(WellKnownArgumentNames.Kind, out var kindValue)
&& kindValue is StringValueNode { Value: ApolloKind })
{
return;
}
}

schema.Directives.Add(
new Directive(
connectorDefinition,
new ArgumentAssignment(WellKnownArgumentNames.Kind, ApolloKind)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using static HotChocolate.Fusion.Properties.CompositionResources;
using static HotChocolate.Fusion.WellKnownDirectiveNames;
using argNames = HotChocolate.Fusion.WellKnownArgumentNames;

namespace HotChocolate.Fusion.Definitions;

/// <summary>
/// The <c>@fusion__connector</c> directive is applied to a source schema definition to declare
/// which connector kind handles that source schema. The composer lifts the directive onto the
/// corresponding <c>fusion__Schema</c> enum value in the merged execution schema.
/// </summary>
internal sealed class ConnectorMutableDirectiveDefinition : MutableDirectiveDefinition
{
public ConnectorMutableDirectiveDefinition(MutableScalarTypeDefinition stringType)
: base(FusionConnector)
{
Description = ConnectorMutableDirectiveDefinition_Description;

Arguments.Add(new MutableInputFieldDefinition(argNames.Kind, new NonNullType(stringType))
{
Description = ConnectorMutableDirectiveDefinition_Argument_Kind_Description
});

Locations = DirectiveLocation.Schema;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using static HotChocolate.Fusion.Properties.CompositionResources;
using static HotChocolate.Fusion.WellKnownDirectiveNames;
using argNames = HotChocolate.Fusion.WellKnownArgumentNames;

namespace HotChocolate.Fusion.Definitions;

/// <summary>
/// The <c>@fusion__connector</c> directive is applied to a <c>fusion__Schema</c> enum value to
/// declare which connector kind handles the corresponding source schema. The default GraphQL
/// connector treats absent kinds as <c>"GraphQL"</c>.
/// </summary>
internal sealed class FusionConnectorMutableDirectiveDefinition : MutableDirectiveDefinition
{
public FusionConnectorMutableDirectiveDefinition(MutableScalarTypeDefinition stringType)
: base(FusionConnector)
{
Description = FusionConnectorMutableDirectiveDefinition_Description;

Arguments.Add(new MutableInputFieldDefinition(argNames.Kind, new NonNullType(stringType))
{
Description = FusionConnectorMutableDirectiveDefinition_Argument_Kind_Description
});

Locations = DirectiveLocation.EnumValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal static class FusionBuiltIns
public static FrozenDictionary<string, MutableDirectiveDefinition> SourceSchemaDirectives { get; } =
new HashSet<MutableDirectiveDefinition>(
[
new ConnectorMutableDirectiveDefinition(s_stringType),
new ExternalMutableDirectiveDefinition(),
new InaccessibleMutableDirectiveDefinition(),
new InternalMutableDirectiveDefinition(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<InternalsVisibleTo Include="ChilliCream.Nitro.CommandLine.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Aspire" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Composition.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Composition.ApolloFederation.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Execution.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Utilities.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.AspNetCore.Tests" />
Expand All @@ -28,6 +29,10 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CompositionResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="ApolloFederation\Properties\FederationResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>FederationResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand All @@ -36,6 +41,11 @@
<AutoGen>True</AutoGen>
<DependentUpon>CompositionResources.resx</DependentUpon>
</Compile>
<Compile Update="ApolloFederation\Properties\FederationResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>FederationResources.resx</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static class LogEntryCodes
public const string ExternalRequireCollision = "EXTERNAL_REQUIRE_COLLISION";
public const string ExternalTypeMismatch = "EXTERNAL_TYPE_MISMATCH";
public const string ExternalUnused = "EXTERNAL_UNUSED";
public const string FederationDirectiveNotSupported = "FEDERATION_DIRECTIVE_NOT_SUPPORTED";
public const string FederationV1NotSupported = "FEDERATION_V1_NOT_SUPPORTED";
public const string FieldArgumentTypesNotMergeable = "FIELD_ARGUMENT_TYPES_NOT_MERGEABLE";
public const string FieldWithMissingRequiredArgument = "FIELD_WITH_MISSING_REQUIRED_ARGUMENT";
public const string ImplementedByInaccessible = "IMPLEMENTED_BY_INACCESSIBLE";
Expand Down
Loading
Loading