Skip to content

Commit

Permalink
Added support for variables to new operation planner. (#7780)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Nov 29, 2024
1 parent 133a3c0 commit 9cf6ea1
Show file tree
Hide file tree
Showing 17 changed files with 688 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Immutable;
using System.Diagnostics;
using HotChocolate.Fusion.Types;
using HotChocolate.Fusion.Types.Collections;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning.Nodes;
Expand All @@ -11,19 +11,31 @@ public sealed class FieldPlanNode : SelectionPlanNode

public FieldPlanNode(
FieldNode fieldNode,
CompositeOutputField field)
OutputFieldInfo field)
: base(field.Type.NamedType(), fieldNode.SelectionSet?.Selections)
{
FieldNode = fieldNode;
Field = field;
ResponseName = FieldNode.Alias?.Value ?? field.Name;

foreach (var argument in fieldNode.Arguments)
{
AddArgument(new ArgumentAssignment(argument.Name.Value, argument.Value));
}
}

public FieldPlanNode(
FieldNode fieldNode,
CompositeOutputField field)
: this(fieldNode, new OutputFieldInfo(field))
{
}

public string ResponseName { get; }

public FieldNode FieldNode { get; }

public CompositeOutputField Field { get; }
public OutputFieldInfo Field { get; }

public IReadOnlyList<ArgumentAssignment> Arguments
=> _arguments ?? (IReadOnlyList<ArgumentAssignment>)Array.Empty<ArgumentAssignment>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace HotChocolate.Fusion.Planning.Nodes;
/// </summary>
public sealed class OperationPlanNode : SelectionPlanNode, IOperationPlanNodeProvider
{
private static readonly IReadOnlyDictionary<string, VariableDefinitionNode> _emptyVariableMap =
new Dictionary<string, VariableDefinitionNode>();
private List<OperationPlanNode>? _operations;
private Dictionary<string, VariableDefinitionNode>? _variables;

public OperationPlanNode(
string schemaName,
Expand Down Expand Up @@ -37,9 +40,18 @@ public OperationPlanNode(
// todo: variable representations are missing.
// todo: how to we represent state?

public IReadOnlyDictionary<string, VariableDefinitionNode> VariableDefinitions
=> _variables ?? _emptyVariableMap;

public IReadOnlyList<OperationPlanNode> Operations
=> _operations ?? (IReadOnlyList<OperationPlanNode>)Array.Empty<OperationPlanNode>();

public void AddVariableDefinition(VariableDefinitionNode variable)
{
ArgumentNullException.ThrowIfNull(variable);
(_variables ??= new Dictionary<string, VariableDefinitionNode>()).Add(variable.Variable.Name.Value, variable);
}

public void AddOperation(OperationPlanNode operation)
{
ArgumentNullException.ThrowIfNull(operation);
Expand All @@ -53,7 +65,7 @@ public OperationDefinitionNode ToSyntaxNode()
null,
null,
OperationType.Query,
Array.Empty<VariableDefinitionNode>(),
_variables?.Values.OrderBy(t => t.Variable.Name.Value).ToArray() ?? [],
Directives.ToSyntaxNode(),
Selections.ToSyntaxNode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Types;

namespace HotChocolate.Fusion.Planning.Nodes;

public class OutputFieldInfo(string name, ICompositeType type, ImmutableArray<string> sources)
{
public OutputFieldInfo(CompositeOutputField field)
: this(field.Name, field.Type, field.Sources.Schemas)
{

}

public string Name => name;

public ICompositeType Type => type;

public ImmutableArray<string> Sources => sources;
}
Loading

0 comments on commit 9cf6ea1

Please sign in to comment.