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] Added pre-merge validation rule ExternalArgumentDefaultMismatch #7844

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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,6 +3,7 @@ namespace HotChocolate.Fusion.Logging;
public static class LogEntryCodes
{
public const string DisallowedInaccessible = "DISALLOWED_INACCESSIBLE";
public const string ExternalArgumentDefaultMismatch = "EXTERNAL_ARGUMENT_DEFAULT_MISMATCH";
public const string ExternalMissingOnBase = "EXTERNAL_MISSING_ON_BASE";
public const string OutputFieldTypesNotMergeable = "OUTPUT_FIELD_TYPES_NOT_MERGEABLE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public static LogEntry DisallowedInaccessibleDirectiveArgument(
new SchemaCoordinate(directiveName, argumentName: argument.Name, ofDirective: true),
schema: schema);

public static LogEntry ExternalArgumentDefaultMismatch(
string argumentName,
string fieldName,
string typeName)
{
var coordinate = new SchemaCoordinate(typeName, fieldName, argumentName);

return new LogEntry(
string.Format(LogEntryHelper_ExternalArgumentDefaultMismatch, coordinate),
LogEntryCodes.ExternalArgumentDefaultMismatch,
LogSeverity.Error,
coordinate);
}

public static LogEntry ExternalMissingOnBase(string fieldName, string typeName)
=> new(
string.Format(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Events;
using static HotChocolate.Fusion.Logging.LogEntryHelper;

namespace HotChocolate.Fusion.PreMergeValidation.Rules;

/// <summary>
/// This rule ensures that arguments on fields marked as <c>@external</c> have default values
/// compatible with the corresponding arguments on fields from other source schemas where the field
/// is defined (non-<c>@external</c>).
/// </summary>
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-External-Argument-Default-Mismatch">
/// Specification
/// </seealso>
internal sealed class ExternalArgumentDefaultMismatchRule : IEventHandler<OutputFieldGroupEvent>
{
public void Handle(OutputFieldGroupEvent @event, CompositionContext context)
{
var (fieldName, fieldGroup, typeName) = @event;

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

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

var argumentNames = fieldGroup
.SelectMany(i => i.Field.Arguments, (_, arg) => arg.Name)
.ToHashSet();
glen-84 marked this conversation as resolved.
Show resolved Hide resolved

foreach (var argumentName in argumentNames)
{
var arguments = fieldGroup
.Select(i => i.Field.Arguments[argumentName])
danielreynolds1 marked this conversation as resolved.
Show resolved Hide resolved
.ToImmutableArray();

var defaultValue = arguments[0].DefaultValue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this, you should create externalArguments.

var externalArguments = externalFields
    .SelectMany(i => i.Field.Arguments.Where(a => a.Name == argumentName))
    .ToImmutableArray();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


foreach (var argument in arguments)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be iterating over externalArguments.

foreach (var externalArgument in externalArguments)
{
    var externalArgumentDefaultValue = externalArgument.DefaultValue;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
var currentDefaultValue = argument.DefaultValue;
var match = (currentDefaultValue, defaultValue) switch
{
(null, null) => true,
(not null, null) => false,
(null, not null) => false,
_ => currentDefaultValue.Value!.Equals(defaultValue.Value)
};

if (!match)
{
context.Log.Write(
ExternalArgumentDefaultMismatch(argumentName, fieldName, typeName));
}
}
}
}
}

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
glen-84 marked this conversation as resolved.
Show resolved Hide resolved
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
Expand All @@ -13,10 +14,14 @@
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="ErrorHelper_PreMergeValidationFailed" xml:space="preserve">
<value>Pre-merge validation failed. View the composition log for details.</value>
Expand All @@ -36,6 +41,9 @@
<data name="LogEntryHelper_DisallowedInaccessibleDirectiveArgument" xml:space="preserve">
<value>The argument '{0}' on built-in directive type '{1}' is not accessible.</value>
</data>
<data name="LogEntryHelper_ExternalArgumentDefaultMismatch" xml:space="preserve">
<value>The argument with schema coordinate '{0}' has inconsistent default values.</value>
</data>
<data name="LogEntryHelper_ExternalMissingOnBase" xml:space="preserve">
<value>Field '{0}' on type '{1}' is only declared as external.</value>
</data>
Expand Down
Loading
Loading