-
-
Notifications
You must be signed in to change notification settings - Fork 749
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
base: main
Are you sure you want to change the base?
Changes from all commits
b4c4495
4c6d65b
f18e747
9f23c56
7701ff1
c85e993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Collections.Immutable; | ||
using HotChocolate.Fusion.Events; | ||
using HotChocolate.Language; | ||
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) | ||
.ToImmutableHashSet(); | ||
|
||
foreach (var argumentName in argumentNames) | ||
{ | ||
var arguments = fieldGroup | ||
.SelectMany(i => i.Field.Arguments.Where(a => a.Name == argumentName)) | ||
.ToImmutableArray(); | ||
|
||
var defaultValue = arguments[0].DefaultValue; | ||
|
||
foreach (var argument in arguments) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be iterating over foreach (var externalArgument in externalArguments)
{
var externalArgumentDefaultValue = externalArgument.DefaultValue; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
if (!SyntaxComparer.BySyntax.Equals(argument.DefaultValue, defaultValue)) | ||
{ | ||
context.Log.Write( | ||
ExternalArgumentDefaultMismatch(argumentName, fieldName, typeName)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
graphql/composite-schemas-spec#78