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

Migrated tests from GraphQL-JS regarding field merging #1697

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public IOutputType NonNullString
public IDictionary<SelectionSetNode, IList<FieldInfo>> FieldSets { get; } =
new Dictionary<SelectionSetNode, IList<FieldInfo>>();

public ISet<(FieldNode, FieldNode)> FieldTuples { get; } =
new HashSet<(FieldNode, FieldNode)>();

public ISet<string> VisitedFragments { get; } = new HashSet<string>();

public IDictionary<string, object> VariableValues { get; } =
Expand Down Expand Up @@ -130,6 +133,7 @@ public void Clear()
Path.Clear();
SelectionSets.Clear();
FieldSets.Clear();
FieldTuples.Clear();
VisitedFragments.Clear();
VariableValues.Clear();
Variables.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public interface IDocumentValidatorContext : ISyntaxVisitorContext
/// A dictionary to store field infos per selection set.
/// </summary>
IDictionary<SelectionSetNode, IList<FieldInfo>> FieldSets { get; }

/// <summary>
/// A set of field tuples.
/// </summary>
ISet<(FieldNode, FieldNode)> FieldTuples { get; }

/// <summary>
/// Gets a set of already visited fragment names.
Expand Down
12 changes: 8 additions & 4 deletions src/HotChocolate/Core/src/Validation/Rules/FieldVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ internal sealed class FieldVisitor : TypeDocumentValidatorVisitor
{
if (context.FieldSets.Count > 0)
{
TryMergeFieldsInSet(context, context.FieldSets[node.SelectionSet]);
foreach (SelectionSetNode selectionSet in context.FieldSets.Keys)
{
TryMergeFieldsInSet(context, context.FieldSets[selectionSet]);
}
}

if (node.SelectionSet.Selections.Count == 0)
Expand Down Expand Up @@ -75,7 +78,7 @@ internal sealed class FieldVisitor : TypeDocumentValidatorVisitor
if (context.Types.TryPeek(out IType type) &&
type.NamedType() is IComplexOutputType ct)
{
if (ct.Fields.TryGetField(node.Name.Value, out IOutputField of))
if (ct.Fields.TryGetField(node.Name.Value, out IOutputField? of))
{
fields.Add(new FieldInfo(context.Types.Peek(), of.Type, node));

Expand Down Expand Up @@ -155,6 +158,7 @@ internal sealed class FieldVisitor : TypeDocumentValidatorVisitor
context.SelectionSets.Pop();
}
}

return Continue;
}

Expand Down Expand Up @@ -235,14 +239,14 @@ private static bool IsTypeNameField(NameString fieldName)
{
TryMergeFieldsInSet(context, fieldA, fieldB);
}
else
else if(context.FieldTuples.Add((fieldA.Field, fieldB.Field)))
{
context.Errors.Add(
context.FieldsAreNotMergable(fieldA, fieldB));
}
}
}
else
else if(context.FieldTuples.Add((fieldA.Field, fieldB.Field)))
{
context.Errors.Add(context.FieldsAreNotMergable(fieldA, fieldB));
}
Expand Down