Skip to content

Commit

Permalink
Migrated tests from GraphQL-JS regarding field merging (#1697)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
PascalSenn and michaelstaib committed Nov 15, 2020
1 parent 66bfb78 commit 121cbad
Show file tree
Hide file tree
Showing 35 changed files with 1,869 additions and 6 deletions.
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
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
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

0 comments on commit 121cbad

Please sign in to comment.