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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;

namespace HotChocolate.Fusion.Definitions;

internal sealed class CostMutableDirectiveDefinition : MutableDirectiveDefinition
{
public CostMutableDirectiveDefinition(MutableScalarTypeDefinition stringType)
: base(DirectiveNames.Cost.Name)
{
Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.Cost.Arguments.Weight,
new NonNullType(stringType)));

Locations =
DirectiveLocation.ArgumentDefinition
| DirectiveLocation.Enum
| DirectiveLocation.FieldDefinition
| DirectiveLocation.InputFieldDefinition
| DirectiveLocation.Object
| DirectiveLocation.Scalar;
}

public static CostMutableDirectiveDefinition Create(ISchemaDefinition schema)
{
if (!schema.Types.TryGetType<MutableScalarTypeDefinition>(SpecScalarNames.String.Name, out var stringType))
{
stringType = BuiltIns.String.Create();
}

return new CostMutableDirectiveDefinition(stringType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using static HotChocolate.Fusion.Properties.CompositionResources;
using static HotChocolate.Fusion.WellKnownArgumentNames;
using static HotChocolate.Fusion.WellKnownDirectiveNames;

namespace HotChocolate.Fusion.Definitions;

/// <summary>
/// The <c>@fusion__cost</c> directive specifies cost metadata for each source schema.
/// </summary>
internal sealed class FusionCostMutableDirectiveDefinition : MutableDirectiveDefinition
{
public FusionCostMutableDirectiveDefinition(
MutableEnumTypeDefinition schemaMutableEnumType,
MutableScalarTypeDefinition stringType)
: base(FusionCost)
{
Description = FusionCostMutableDirectiveDefinition_Description;

Arguments.Add(
new MutableInputFieldDefinition(Schema, new NonNullType(schemaMutableEnumType))
{
Description = FusionCostMutableDirectiveDefinition_Argument_Schema_Description
});

Arguments.Add(
new MutableInputFieldDefinition(Weight, new NonNullType(stringType))
{
Description = FusionCostMutableDirectiveDefinition_Argument_Weight_Description
});

IsRepeatable = true;

Locations =
DirectiveLocation.ArgumentDefinition
| DirectiveLocation.Enum
| DirectiveLocation.FieldDefinition
| DirectiveLocation.InputFieldDefinition
| DirectiveLocation.Object
| DirectiveLocation.Scalar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using static HotChocolate.Fusion.Properties.CompositionResources;
using static HotChocolate.Fusion.WellKnownArgumentNames;
using static HotChocolate.Fusion.WellKnownDirectiveNames;

namespace HotChocolate.Fusion.Definitions;

/// <summary>
/// The <c>@fusion__listSize</c> directive specifies list size metadata for each source schema.
/// </summary>
internal sealed class FusionListSizeMutableDirectiveDefinition : MutableDirectiveDefinition
{
public FusionListSizeMutableDirectiveDefinition(
MutableEnumTypeDefinition schemaMutableEnumType,
MutableScalarTypeDefinition intType,
MutableScalarTypeDefinition stringType,
MutableScalarTypeDefinition booleanType)
: base(FusionListSize)
{
Description = FusionListSizeMutableDirectiveDefinition_Description;

Arguments.Add(
new MutableInputFieldDefinition(Schema, new NonNullType(schemaMutableEnumType))
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_Schema_Description
});

Arguments.Add(
new MutableInputFieldDefinition(AssumedSize, intType)
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_AssumedSize_Description
});

Arguments.Add(
new MutableInputFieldDefinition(SlicingArguments, new ListType(new NonNullType(stringType)))
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_SlicingArguments_Description
});

Arguments.Add(
new MutableInputFieldDefinition(SlicingArgumentDefaultValue, intType)
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_SlicingArgumentDefaultValue_Description
});

Arguments.Add(
new MutableInputFieldDefinition(SizedFields, new ListType(new NonNullType(stringType)))
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_SizedFields_Description
});

Arguments.Add(
new MutableInputFieldDefinition(RequireOneSlicingArgument, booleanType)
{
Description = FusionListSizeMutableDirectiveDefinition_Argument_RequireOneSlicingArgument_Description
});

IsRepeatable = true;

Locations = DirectiveLocation.FieldDefinition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using HotChocolate.Types;
using HotChocolate.Types.Mutable;

namespace HotChocolate.Fusion.Definitions;

internal sealed class ListSizeMutableDirectiveDefinition : MutableDirectiveDefinition
{
public ListSizeMutableDirectiveDefinition(
MutableScalarTypeDefinition intType,
MutableScalarTypeDefinition stringType,
MutableScalarTypeDefinition booleanType)
: base(DirectiveNames.ListSize.Name)
{
Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.ListSize.Arguments.AssumedSize,
intType));

Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.ListSize.Arguments.SlicingArguments,
new ListType(new NonNullType(stringType))));

Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.ListSize.Arguments.SlicingArgumentDefaultValue,
intType));

Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.ListSize.Arguments.SizedFields,
new ListType(new NonNullType(stringType))));

Arguments.Add(
new MutableInputFieldDefinition(
DirectiveNames.ListSize.Arguments.RequireOneSlicingArgument,
booleanType));

Locations = DirectiveLocation.FieldDefinition;
}

public static ListSizeMutableDirectiveDefinition Create(ISchemaDefinition schema)
{
if (!schema.Types.TryGetType<MutableScalarTypeDefinition>(SpecScalarNames.Int.Name, out var intType))
{
intType = BuiltIns.Int.Create();
}

if (!schema.Types.TryGetType<MutableScalarTypeDefinition>(SpecScalarNames.String.Name, out var stringType))
{
stringType = BuiltIns.String.Create();
}

if (!schema.Types.TryGetType<MutableScalarTypeDefinition>(SpecScalarNames.Boolean.Name, out var booleanType))
{
booleanType = BuiltIns.Boolean.Create();
}

return new ListSizeMutableDirectiveDefinition(intType, stringType, booleanType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HotChocolate.Fusion.Definitions;
using HotChocolate.Fusion.Directives;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Fusion.Info;
using HotChocolate.Fusion.Options;
using HotChocolate.Language;
using HotChocolate.Types;
Expand Down Expand Up @@ -40,7 +41,7 @@ public override void MergeDirectiveDefinition(

public override void MergeDirectives(
IDirectivesProvider mergedMember,
ImmutableArray<IDirectivesProvider> memberDefinitions,
ImmutableArray<DirectivesProviderInfo> memberDefinitions,
MutableSchemaDefinition mergedSchema)
{
if (MergeBehavior is DirectiveMergeBehavior.Ignore)
Expand All @@ -57,7 +58,7 @@ public override void MergeDirectives(
var cacheControlDirectives =
memberDefinitions
.SelectMany(
d => d.Directives.Where(dir => dir.Name == DirectiveNames.CacheControl))
d => d.Member.Directives.Where(dir => dir.Name == DirectiveNames.CacheControl))
.Select(CacheControlDirective.From)
.ToArray();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Immutable;
using System.Globalization;
using HotChocolate.Fusion.Definitions;
using HotChocolate.Fusion.Directives;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Fusion.Info;
using HotChocolate.Fusion.Options;
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
using ArgumentNames = HotChocolate.Fusion.WellKnownArgumentNames;
using DirectiveNames = HotChocolate.Fusion.WellKnownDirectiveNames;

namespace HotChocolate.Fusion.DirectiveMergers;

internal class CostDirectiveMerger(DirectiveMergeBehavior mergeBehavior)
: DirectiveMergerBase(mergeBehavior)
{
public override string DirectiveName => DirectiveNames.Cost;

public override MutableDirectiveDefinition GetCanonicalDirectiveDefinition(ISchemaDefinition schema)
{
return CostMutableDirectiveDefinition.Create(schema);
}

public override void MergeDirectives(
IDirectivesProvider mergedMember,
ImmutableArray<DirectivesProviderInfo> memberDefinitions,
MutableSchemaDefinition mergedSchema)
{
if (!mergedSchema.DirectiveDefinitions.TryGetDirective(DirectiveName, out var directiveDefinition))
{
// Merged definition not found.
return;
}

var costDirectives =
memberDefinitions
.SelectMany(d => d.Member.Directives.Where(dir => dir.Name == DirectiveNames.Cost))
.Select(CostDirective.From)
.ToArray();

if (costDirectives.Length == 0)
{
return;
}

var maxWeight = costDirectives.Max(d => d.Weight);
var weightArgument =
new ArgumentAssignment(ArgumentNames.Weight, maxWeight.ToString(CultureInfo.InvariantCulture));
var costDirective = new Directive(directiveDefinition, weightArgument);

mergedMember.AddDirective(costDirective);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Info;
using HotChocolate.Fusion.Options;
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
Expand Down Expand Up @@ -27,6 +28,6 @@ public virtual void MergeDirectiveDefinition(

public abstract void MergeDirectives(
IDirectivesProvider mergedMember,
ImmutableArray<IDirectivesProvider> memberDefinitions,
ImmutableArray<DirectivesProviderInfo> memberDefinitions,
MutableSchemaDefinition mergedSchema);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Info;
using HotChocolate.Fusion.Options;
using HotChocolate.Types;
using HotChocolate.Types.Mutable;
Expand All @@ -19,6 +20,6 @@ void MergeDirectiveDefinition(

void MergeDirectives(
IDirectivesProvider mergedMember,
ImmutableArray<IDirectivesProvider> memberDefinitions,
ImmutableArray<DirectivesProviderInfo> memberDefinitions,
MutableSchemaDefinition mergedSchema);
}
Loading
Loading