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] Improved composition errors #7856

Merged
merged 2 commits into from
Dec 23, 2024
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
Expand Up @@ -3,7 +3,7 @@

namespace HotChocolate.Fusion.Logging;

public sealed class CompositionLog : ICompositionLog, IEnumerable<LogEntry>
public sealed class CompositionLog : ICompositionLog
{
public bool HasErrors { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace HotChocolate.Fusion.Logging.Contracts;
/// <summary>
/// Defines an interface for logging composition information, warnings, and errors.
/// </summary>
public interface ICompositionLog
public interface ICompositionLog : IEnumerable<LogEntry>
{
/// <summary>
/// Gets a value indicating whether the log contains errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,53 @@ internal static class LogEntryHelper
public static LogEntry DisallowedInaccessibleBuiltInScalar(
ScalarTypeDefinition scalar,
SchemaDefinition schema)
=> new(
string.Format(LogEntryHelper_DisallowedInaccessibleBuiltInScalar, scalar.Name),
{
return new LogEntry(
string.Format(
LogEntryHelper_DisallowedInaccessibleBuiltInScalar,
scalar.Name,
schema.Name),
LogEntryCodes.DisallowedInaccessible,
LogSeverity.Error,
new SchemaCoordinate(scalar.Name),
scalar,
schema);
}

public static LogEntry DisallowedInaccessibleIntrospectionType(
INamedTypeDefinition type,
SchemaDefinition schema)
=> new(
string.Format(LogEntryHelper_DisallowedInaccessibleIntrospectionType, type.Name),
{
return new LogEntry(
string.Format(
LogEntryHelper_DisallowedInaccessibleIntrospectionType,
type.Name,
schema.Name),
LogEntryCodes.DisallowedInaccessible,
LogSeverity.Error,
new SchemaCoordinate(type.Name),
type,
schema);
}

public static LogEntry DisallowedInaccessibleIntrospectionField(
OutputFieldDefinition field,
string typeName,
SchemaDefinition schema)
=> new(
{
var coordinate = new SchemaCoordinate(typeName, field.Name);

return new LogEntry(
string.Format(
LogEntryHelper_DisallowedInaccessibleIntrospectionField,
field.Name,
typeName),
coordinate,
schema.Name),
LogEntryCodes.DisallowedInaccessible,
LogSeverity.Error,
new SchemaCoordinate(typeName, field.Name),
coordinate,
field,
schema);
}

public static LogEntry DisallowedInaccessibleIntrospectionArgument(
InputFieldDefinition argument,
Expand All @@ -53,8 +67,8 @@ public static LogEntry DisallowedInaccessibleIntrospectionArgument(
return new LogEntry(
string.Format(
LogEntryHelper_DisallowedInaccessibleIntrospectionArgument,
argument.Name,
coordinate),
coordinate,
schema.Name),
LogEntryCodes.DisallowedInaccessible,
LogSeverity.Error,
coordinate,
Expand All @@ -66,15 +80,23 @@ public static LogEntry DisallowedInaccessibleDirectiveArgument(
InputFieldDefinition argument,
string directiveName,
SchemaDefinition schema)
=> new(
{
var coordinate = new SchemaCoordinate(
directiveName,
argumentName: argument.Name,
ofDirective: true);

return new LogEntry(
string.Format(
LogEntryHelper_DisallowedInaccessibleDirectiveArgument,
argument.Name,
directiveName),
coordinate,
schema.Name),
LogEntryCodes.DisallowedInaccessible,
LogSeverity.Error,
new SchemaCoordinate(directiveName, argumentName: argument.Name, ofDirective: true),
schema: schema);
coordinate,
argument,
schema);
}

public static LogEntry ExternalArgumentDefaultMismatch(
string argumentName,
Expand All @@ -90,23 +112,40 @@ public static LogEntry ExternalArgumentDefaultMismatch(
coordinate);
}

public static LogEntry ExternalMissingOnBase(string fieldName, string typeName)
=> new(
string.Format(
LogEntryHelper_ExternalMissingOnBase,
fieldName,
typeName),
public static LogEntry ExternalMissingOnBase(
OutputFieldDefinition externalField,
INamedTypeDefinition type,
SchemaDefinition schema)
{
var coordinate = new SchemaCoordinate(type.Name, externalField.Name);

return new LogEntry(
string.Format(LogEntryHelper_ExternalMissingOnBase, coordinate, schema.Name),
LogEntryCodes.ExternalMissingOnBase,
LogSeverity.Error,
new SchemaCoordinate(typeName, fieldName));
coordinate,
externalField,
schema);
}

public static LogEntry OutputFieldTypesNotMergeable(
OutputFieldDefinition field,
string typeName,
SchemaDefinition schemaA,
SchemaDefinition schemaB)
{
var coordinate = new SchemaCoordinate(typeName, field.Name);

public static LogEntry OutputFieldTypesNotMergeable(string fieldName, string typeName)
=> new(
return new LogEntry(
string.Format(
LogEntryHelper_OutputFieldTypesNotMergeable,
fieldName,
typeName),
coordinate,
schemaA.Name,
schemaB.Name),
LogEntryCodes.OutputFieldTypesNotMergeable,
LogSeverity.Error,
new SchemaCoordinate(typeName, fieldName));
coordinate,
field,
schemaA);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 @@ -17,14 +17,20 @@ internal sealed class ExternalMissingOnBaseRule : IEventHandler<OutputFieldGroup
{
public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
{
var (fieldName, fieldGroup, typeName) = @event;
var fieldGroup = @event.FieldGroup;

var externalFieldCount = fieldGroup.Count(i => ValidationHelper.IsExternal(i.Field));
var nonExternalFieldCount = fieldGroup.Length - externalFieldCount;
var externalFields = fieldGroup
.Where(i => ValidationHelper.IsExternal(i.Field))
.ToImmutableArray();

if (externalFieldCount != 0 && nonExternalFieldCount == 0)
var nonExternalFieldCount = fieldGroup.Length - externalFields.Length;

foreach (var (field, type, schema) in externalFields)
{
context.Log.Write(ExternalMissingOnBase(fieldName, typeName));
if (nonExternalFieldCount == 0)
{
context.Log.Write(ExternalMissingOnBase(field, type, schema));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@ internal sealed class OutputFieldTypesMergeableRule : IEventHandler<OutputFieldG
{
public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
{
var (fieldName, fieldGroup, typeName) = @event;
var (_, fieldGroup, typeName) = @event;

if (!ValidationHelper.FieldsAreMergeable([.. fieldGroup.Select(i => i.Field)]))
for (var i = 0; i < fieldGroup.Length - 1; i++)
{
context.Log.Write(OutputFieldTypesNotMergeable(fieldName, typeName));
var fieldInfoA = fieldGroup[i];
var fieldInfoB = fieldGroup[i + 1];
var typeA = fieldInfoA.Field.Type;
var typeB = fieldInfoB.Field.Type;

if (!ValidationHelper.SameTypeShape(typeA, typeB))
{
context.Log.Write(
OutputFieldTypesNotMergeable(
fieldInfoA.Field,
typeName,
fieldInfoA.Schema,
fieldInfoB.Schema));
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading