Skip to content

Commit

Permalink
Added Fusion tests for @skip and fixed some (#7353)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler authored Aug 12, 2024
1 parent ba0d021 commit 3cff2e1
Show file tree
Hide file tree
Showing 88 changed files with 9,461 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ private sealed class FetchRewriterContext(
IReadOnlyDictionary<string, IValueNode> variables,
SelectionSetNode? selectionSet,
string? responseName,
IReadOnlyList<string>? unspecifiedArguments)
IReadOnlyList<string>? unspecifiedArguments,
IReadOnlyList<DirectiveNode>? directives)
{
public string? ResponseName { get; } = responseName;

Expand All @@ -26,6 +27,8 @@ private sealed class FetchRewriterContext(
/// </summary>
public IReadOnlyList<string>? UnspecifiedArguments { get; } = unspecifiedArguments;

public IReadOnlyList<DirectiveNode>? Directives { get; } = directives;

public SelectionSetNode? SelectionSet { get; } = selectionSet;

public IReadOnlyList<string> SelectionPath { get; set; } = Array.Empty<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ private class ResolverRewriter : SyntaxRewriter<FetchRewriterContext>
return null;
}

if (context.Directives?.Count > 0)
{
result = result.WithDirectives(context.Directives);
}

if (context.UnspecifiedArguments?.Count > 0)
{
var explicitlyDefinedArguments = result.Arguments
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using static HotChocolate.Fusion.FusionResources;

Expand Down Expand Up @@ -48,15 +49,17 @@ public ResolverDefinition(
/// <summary>
/// Gets the argument target types of this resolver.
/// </summary>
public IReadOnlyDictionary<string, ITypeNode> ArgumentTypes { get; }
public IReadOnlyDictionary<string, ITypeNode> ArgumentTypes { get; }

public (ISelectionNode selectionNode, IReadOnlyList<string> Path) CreateSelection(
IReadOnlyDictionary<string, IValueNode> variables,
SelectionSetNode? selectionSet,
string? responseName,
IReadOnlyList<string>? unspecifiedArguments)
IReadOnlyList<string>? unspecifiedArguments,
IReadOnlyList<DirectiveNode>? directives)
{
var context = new FetchRewriterContext(Placeholder, variables, selectionSet, responseName, unspecifiedArguments);
var context = new FetchRewriterContext(Placeholder, variables, selectionSet, responseName, unspecifiedArguments,
directives);
var selection = _rewriter.Rewrite(_field ?? (ISyntaxNode)Select, context);

if (Placeholder is null && selectionSet is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private SelectionSetNode CreateRootSelectionSetNode(
context.VariableValues,
selectionSetNode,
nodeSelection.Selection.ResponseName,
null);
null,
nodeSelection.Selection.SyntaxNode.Directives);

if (selectionNode is FieldNode fieldNode &&
!nodeSelection.Selection.ResponseName.EqualsOrdinal(fieldNode.Name.Value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ internal RequestDocument CreateRequestDocument(
context.VariableValues,
rootSelectionSetNode,
null,
unspecifiedArguments);
unspecifiedArguments,
null);

rootSelectionSetNode = new SelectionSetNode(new[] { rootResolver, });
path = p;
Expand All @@ -62,6 +63,7 @@ internal RequestDocument CreateRequestDocument(
executionStep.ParentSelectionPath is not null)
{
rootSelectionSetNode = CreateRootLevelQuery(
context,
executionStep.ParentSelectionPath,
rootSelectionSetNode);
}
Expand All @@ -83,15 +85,37 @@ internal RequestDocument CreateRequestDocument(
}

private SelectionSetNode CreateRootLevelQuery(
QueryPlanContext context,
SelectionPath path,
SelectionSetNode selectionSet)
{
var current = path;

while (current is not null)
{
selectionSet = new SelectionSetNode(
new[] { current.Selection.SyntaxNode.WithSelectionSet(selectionSet), });
var selectionNode = current.Selection.SyntaxNode.WithSelectionSet(selectionSet);

// TODO: this is not good but will fix the include issue ...
// we need to rework the operation compiler for a proper fix.
if (selectionNode.Directives.Count > 0)
{
foreach (var directive in selectionNode.Directives)
{
foreach (var argument in directive.Arguments)
{
if (argument.Value is not VariableNode variable)
{
continue;
}

var originalVarDef = context.Operation.Definition.VariableDefinitions
.First(t => t.Variable.Equals(variable, SyntaxComparison.Syntax));
context.ForwardedVariables.Add(originalVarDef);
}
}
}

selectionSet = new SelectionSetNode(new[] { selectionNode, });

current = current.Parent;
}
Expand Down Expand Up @@ -164,7 +188,8 @@ protected virtual SelectionSetNode CreateRootSelectionSetNode(
context.VariableValues,
selectionSetNode,
rootSelection.Selection.ResponseName,
unspecifiedArguments);
unspecifiedArguments,
rootSelection.Selection.SyntaxNode.Directives);
selectionNode = s;
}

Expand All @@ -175,6 +200,26 @@ protected virtual SelectionSetNode CreateRootSelectionSetNode(
new NameNode(rootSelection.Selection.ResponseName));
}

// TODO: this is not good but will fix the include issue ...
// we need to rework the operation compiler for a proper fix.
if (selectionNode.Directives.Count > 0)
{
foreach (var directive in selectionNode.Directives)
{
foreach (var argument in directive.Arguments)
{
if (argument.Value is not VariableNode variable)
{
continue;
}

var originalVarDef = context.Operation.Definition.VariableDefinitions
.First(t => t.Variable.Equals(variable, SyntaxComparison.Syntax));
context.ForwardedVariables.Add(originalVarDef);
}
}
}

selectionNodes.Add(selectionNode);
}

Expand Down
Loading

0 comments on commit 3cff2e1

Please sign in to comment.