Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fusion] Miscellaneous changes to composition (part 2) #7920

Merged
merged 14 commits into from
Jan 10, 2025
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,37 @@
using HotChocolate.Skimmed;
using static HotChocolate.Fusion.WellKnownDirectiveNames;

namespace HotChocolate.Fusion.Extensions;

internal static class DirectivesProviderExtensions
{
public static bool HasExternalDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(External);
}

public static bool HasInaccessibleDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(Inaccessible);
}

public static bool HasInternalDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(Internal);
}

public static bool HasLookupDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(Lookup);
}

public static bool HasOverrideDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(Override);
}

public static bool HasProvidesDirective(this IDirectivesProvider type)
{
return type.Directives.ContainsName(Provides);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public static class LogEntryCodes
public const string KeyInvalidFields = "KEY_INVALID_FIELDS";
public const string KeyInvalidFieldsType = "KEY_INVALID_FIELDS_TYPE";
public const string KeyInvalidSyntax = "KEY_INVALID_SYNTAX";
public const string LookupMustNotReturnList = "LOOKUP_MUST_NOT_RETURN_LIST";
public const string LookupShouldHaveNullableReturnType = "LOOKUP_SHOULD_HAVE_NULLABLE_RETURN_TYPE";
public const string LookupReturnsList = "LOOKUP_RETURNS_LIST";
public const string LookupReturnsNonNullableType = "LOOKUP_RETURNS_NON_NULLABLE_TYPE";
public const string OutputFieldTypesNotMergeable = "OUTPUT_FIELD_TYPES_NOT_MERGEABLE";
public const string OverrideFromSelf = "OVERRIDE_FROM_SELF";
public const string OverrideOnInterface = "OVERRIDE_ON_INTERFACE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public static LogEntry KeyInvalidSyntax(
schema);
}

public static LogEntry LookupMustNotReturnList(
public static LogEntry LookupReturnsList(
OutputFieldDefinition field,
INamedTypeDefinition type,
SchemaDefinition schema)
Expand All @@ -387,17 +387,17 @@ public static LogEntry LookupMustNotReturnList(

return new LogEntry(
string.Format(
LogEntryHelper_LookupMustNotReturnList,
LogEntryHelper_LookupReturnsList,
coordinate,
schema.Name),
LogEntryCodes.LookupMustNotReturnList,
LogEntryCodes.LookupReturnsList,
LogSeverity.Error,
coordinate,
field,
schema);
}

public static LogEntry LookupShouldHaveNullableReturnType(
public static LogEntry LookupReturnsNonNullableType(
OutputFieldDefinition field,
INamedTypeDefinition type,
SchemaDefinition schema)
Expand All @@ -406,10 +406,10 @@ public static LogEntry LookupShouldHaveNullableReturnType(

return new LogEntry(
string.Format(
LogEntryHelper_LookupShouldHaveNullableReturnType,
LogEntryHelper_LookupReturnsNonNullableType,
coordinate,
schema.Name),
LogEntryCodes.LookupShouldHaveNullableReturnType,
LogEntryCodes.LookupReturnsNonNullableType,
LogSeverity.Warning,
coordinate,
field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ internal record InputTypeGroupEvent(
ImmutableArray<InputTypeInfo> InputTypeGroup) : IEvent;

internal record KeyFieldEvent(
ComplexTypeDefinition EntityType,
Directive KeyDirective,
ComplexTypeDefinition EntityType,
OutputFieldDefinition Field,
ComplexTypeDefinition Type,
SchemaDefinition Schema) : IEvent;

internal record KeyFieldNodeEvent(
ComplexTypeDefinition EntityType,
Directive KeyDirective,
FieldNode FieldNode,
ImmutableArray<string> FieldNamePath,
Directive KeyDirective,
ComplexTypeDefinition EntityType,
SchemaDefinition Schema) : IEvent;

internal record KeyFieldsInvalidReferenceEvent(
ComplexTypeDefinition EntityType,
Directive KeyDirective,
FieldNode FieldNode,
ComplexTypeDefinition Type,
Directive KeyDirective,
ComplexTypeDefinition EntityType,
SchemaDefinition Schema) : IEvent;

internal record KeyFieldsInvalidSyntaxEvent(
ComplexTypeDefinition EntityType,
Directive KeyDirective,
ComplexTypeDefinition EntityType,
SchemaDefinition Schema) : IEvent;

internal record KeyFieldsInvalidTypeEvent(
Expand Down Expand Up @@ -129,6 +129,13 @@ internal record RequireFieldsInvalidSyntaxEvent(
ComplexTypeDefinition Type,
SchemaDefinition Schema) : IEvent;

internal record RequireFieldsInvalidTypeEvent(
Directive RequireDirective,
InputFieldDefinition Argument,
OutputFieldDefinition Field,
ComplexTypeDefinition Type,
SchemaDefinition Schema) : IEvent;

internal record SchemaEvent(SchemaDefinition Schema) : IEvent;

internal record TypeEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ private void PublishEntityEvents(

foreach (var keyDirective in keyDirectives)
{
if (
!keyDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
if (!keyDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
|| f is not StringValueNode fields)
{
PublishEvent(
Expand All @@ -211,7 +210,7 @@ private void PublishEntityEvents(
catch (SyntaxException)
{
PublishEvent(
new KeyFieldsInvalidSyntaxEvent(entityType, keyDirective, schema),
new KeyFieldsInvalidSyntaxEvent(keyDirective, entityType, schema),
context);
}
}
Expand All @@ -236,10 +235,10 @@ private void PublishKeyFieldEvents(

PublishEvent(
new KeyFieldNodeEvent(
entityType,
keyDirective,
fieldNode,
[.. fieldNamePath],
keyDirective,
entityType,
schema),
context);

Expand All @@ -249,8 +248,8 @@ private void PublishKeyFieldEvents(
{
PublishEvent(
new KeyFieldEvent(
entityType,
keyDirective,
entityType,
field,
parentType,
schema),
Expand All @@ -265,10 +264,10 @@ private void PublishKeyFieldEvents(
{
PublishEvent(
new KeyFieldsInvalidReferenceEvent(
entityType,
keyDirective,
fieldNode,
parentType,
keyDirective,
entityType,
schema),
context);

Expand Down Expand Up @@ -302,8 +301,7 @@ private void PublishProvidesEvents(
var providesDirective =
field.Directives.First(d => d.Name == WellKnownDirectiveNames.Provides);

if (
!providesDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
if (!providesDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
|| f is not StringValueNode fields)
{
PublishEvent(
Expand Down Expand Up @@ -365,8 +363,7 @@ private void PublishProvidesFieldEvents(

if (parentType?.NullableType() is ComplexTypeDefinition providedType)
{
if (
providedType.Fields.TryGetField(
if (providedType.Fields.TryGetField(
fieldNode.Name.Value,
out var providedField))
{
Expand Down Expand Up @@ -419,10 +416,13 @@ private void PublishRequireEvents(
var requireDirective =
argument.Directives.First(d => d.Name == WellKnownDirectiveNames.Require);

if (
!requireDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
if (!requireDirective.Arguments.TryGetValue(WellKnownArgumentNames.Fields, out var f)
|| f is not StringValueNode fields)
{
PublishEvent(
new RequireFieldsInvalidTypeEvent(requireDirective, argument, field, type, schema),
context);

return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Skimmed;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

Expand All @@ -25,13 +26,13 @@ public void Handle(TypeEvent @event, CompositionContext context)

// Built-in scalar types must be accessible.
if (type is ScalarTypeDefinition { IsSpecScalar: true } scalar
&& !ValidationHelper.IsAccessible(scalar))
&& scalar.HasInaccessibleDirective())
{
context.Log.Write(DisallowedInaccessibleBuiltInScalar(scalar, schema));
}

// Introspection types must be accessible.
if (type.IsIntrospectionType && !ValidationHelper.IsAccessible(type))
if (type.IsIntrospectionType && type.HasInaccessibleDirective())
{
context.Log.Write(DisallowedInaccessibleIntrospectionType(type, schema));
}
Expand All @@ -42,7 +43,7 @@ public void Handle(OutputFieldEvent @event, CompositionContext context)
var (field, type, schema) = @event;

// Introspection fields must be accessible.
if (type.IsIntrospectionType && !ValidationHelper.IsAccessible(field))
if (type.IsIntrospectionType && field.HasInaccessibleDirective())
{
context.Log.Write(
DisallowedInaccessibleIntrospectionField(
Expand All @@ -57,7 +58,7 @@ public void Handle(FieldArgumentEvent @event, CompositionContext context)
var (argument, field, type, schema) = @event;

// Introspection arguments must be accessible.
if (type.IsIntrospectionType && !ValidationHelper.IsAccessible(argument))
if (type.IsIntrospectionType && argument.HasInaccessibleDirective())
{
context.Log.Write(
DisallowedInaccessibleIntrospectionArgument(
Expand All @@ -73,7 +74,7 @@ public void Handle(DirectiveArgumentEvent @event, CompositionContext context)
var (argument, directive, schema) = @event;

// Built-in directive arguments must be accessible.
if (BuiltIns.IsBuiltInDirective(directive.Name) && !ValidationHelper.IsAccessible(argument))
if (BuiltIns.IsBuiltInDirective(directive.Name) && argument.HasInaccessibleDirective())
{
context.Log.Write(
DisallowedInaccessibleDirectiveArgument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

namespace HotChocolate.Fusion.PreMergeValidation.Rules;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void Handle(EnumTypeGroupEvent @event, CompositionContext context)

var enumValues = enumGroup
.SelectMany(e => e.Type.Values)
.Where(ValidationHelper.IsAccessible)
.Where(v => !v.HasInaccessibleDirective())
.Select(v => v.Name)
.ToImmutableHashSet();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Language;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

Expand All @@ -20,7 +21,7 @@ public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
var (fieldName, fieldGroup, typeName) = @event;

var externalFields = fieldGroup
.Where(i => ValidationHelper.IsExternal(i.Field))
.Where(i => i.Field.HasExternalDirective())
.ToImmutableArray();

if (externalFields.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

namespace HotChocolate.Fusion.PreMergeValidation.Rules;
Expand All @@ -20,7 +21,7 @@ public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
var fieldGroup = @event.FieldGroup;

var externalFields = fieldGroup
.Where(i => ValidationHelper.IsExternal(i.Field))
.Where(i => i.Field.HasExternalDirective())
.ToImmutableArray();

var nonExternalFieldCount = fieldGroup.Length - externalFields.Length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Skimmed;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

Expand All @@ -21,7 +22,7 @@ public void Handle(OutputFieldEvent @event, CompositionContext context)
{
var (field, type, schema) = @event;

if (type is InterfaceTypeDefinition && ValidationHelper.IsExternal(field))
if (type is InterfaceTypeDefinition && field.HasExternalDirective())
{
context.Log.Write(ExternalOnInterface(field, type, schema));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using HotChocolate.Skimmed;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

Expand All @@ -18,7 +19,7 @@ public void Handle(OutputFieldEvent @event, CompositionContext context)
{
var (field, type, schema) = @event;

if (ValidationHelper.IsExternal(field))
if (field.HasExternalDirective())
{
var referencingFields =
schema.Types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using HotChocolate.Fusion.Extensions;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

namespace HotChocolate.Fusion.PreMergeValidation.Rules;
Expand All @@ -24,10 +25,10 @@ public void Handle(FieldArgumentGroupEvent @event, CompositionContext context)
var argumentGroupVisible = argumentGroup
.Where(
i =>
ValidationHelper.IsAccessible(i.Type)
&& !ValidationHelper.IsInternal(i.Type)
&& ValidationHelper.IsAccessible(i.Field)
&& !ValidationHelper.IsInternal(i.Field))
!i.Type.HasInaccessibleDirective()
&& !i.Type.HasInternalDirective()
&& !i.Field.HasInaccessibleDirective()
&& !i.Field.HasInternalDirective())
.ToImmutableArray();

for (var i = 0; i < argumentGroupVisible.Length - 1; i++)
Expand Down
Loading
Loading