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

Restructured Fusion Projects #7755

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Collections.Immutable;
using System.Diagnostics;
using HotChocolate.Fusion.Types;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning;
namespace HotChocolate.Fusion.Planning.Nodes;

public class FieldPlanNode : SelectionPlanNode
public sealed class FieldPlanNode : SelectionPlanNode
{
private List<ArgumentAssignment>? _arguments;

public FieldPlanNode(
FieldNode fieldNode,
CompositeOutputField field)
Expand All @@ -20,4 +24,24 @@ public FieldPlanNode(
public FieldNode FieldNode { get; }

public CompositeOutputField Field { get; }

public IReadOnlyList<ArgumentAssignment> Arguments
=> _arguments ?? (IReadOnlyList<ArgumentAssignment>)Array.Empty<ArgumentAssignment>();

public void AddArgument(ArgumentAssignment argument)
{
ArgumentNullException.ThrowIfNull(argument);
_arguments ??= [];
_arguments.Add(argument);
}

public FieldNode ToSyntaxNode()
{
return new FieldNode(
new NameNode(Field.Name),
Field.Name.Equals(ResponseName) ? null : new NameNode(ResponseName),
Directives.ToSyntaxNode(),
Arguments.ToSyntaxNode(),
Selections.Count == 0 ? null : Selections.ToSyntaxNode());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HotChocolate.Fusion.Planning;
namespace HotChocolate.Fusion.Planning.Nodes;

public interface IOperationPlanNodeProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HotChocolate.Fusion.Types;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning.Nodes;

public sealed class InlineFragmentPlanNode : SelectionPlanNode
{
public InlineFragmentPlanNode(
ICompositeNamedType declaringType,
IReadOnlyList<ISelectionNode> selectionNodes)
: base(declaringType, selectionNodes)
{
}

public InlineFragmentNode ToSyntaxNode()
{
return new InlineFragmentNode(
null,
new NamedTypeNode(DeclaringType.Name),
Directives.ToSyntaxNode(),
Selections.ToSyntaxNode());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using HotChocolate.Fusion.Types;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning;
namespace HotChocolate.Fusion.Planning.Nodes;

/// <summary>
/// Represents an operation to resolve data from a specific source schema.
Expand Down Expand Up @@ -34,6 +34,9 @@ public OperationPlanNode(

public string SchemaName { get; }

// todo: variable representations are missing.
// todo: how to we represent state?

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

Expand All @@ -43,4 +46,15 @@ public void AddOperation(OperationPlanNode operation)
(_operations ??= []).Add(operation);
operation.Parent = this;
}

public OperationDefinitionNode ToSyntaxNode()
{
return new OperationDefinitionNode(
null,
null,
OperationType.Query,
Array.Empty<VariableDefinitionNode>(),
Directives.ToSyntaxNode(),
Selections.ToSyntaxNode());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HotChocolate.Fusion.Planning;
namespace HotChocolate.Fusion.Planning.Nodes;

public abstract class PlanNode
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace HotChocolate.Fusion.Planning;
using System.Collections.Immutable;
using HotChocolate.Language;

public class RootPlanNode : PlanNode, IOperationPlanNodeProvider
namespace HotChocolate.Fusion.Planning.Nodes;

public sealed class RootPlanNode : PlanNode, IOperationPlanNodeProvider
{
private readonly List<OperationPlanNode> _operations = new();

Expand All @@ -13,4 +16,27 @@ public void AddOperation(OperationPlanNode operation)
_operations.Add(operation);
operation.Parent = this;
}

public DocumentNode ToSyntaxNode()
{
var backlog = new Queue<OperationPlanNode>();
var definitions = ImmutableArray.CreateBuilder<IDefinitionNode>();

foreach(var operation in _operations)
{
backlog.Enqueue(operation);
}

while(backlog.TryDequeue(out var operation))
{
definitions.Add(operation.ToSyntaxNode());

foreach(var child in operation.Operations)
{
backlog.Enqueue(child);
}
}

return new DocumentNode(definitions.ToImmutable());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using HotChocolate.Fusion.Types;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning;
namespace HotChocolate.Fusion.Planning.Nodes;

/// <summary>
/// The base class for plan nodes that can have child selections.
Expand Down Expand Up @@ -65,6 +65,13 @@ public IReadOnlyList<SelectionPlanNode> Selections
public void AddSelection(SelectionPlanNode selection)
{
ArgumentNullException.ThrowIfNull(selection);

if(selection is OperationPlanNode)
{
throw new NotSupportedException(
"An operation cannot be a child of a selection.");
}

(_selections ??= []).Add(selection);
selection.Parent = this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Immutable;
using HotChocolate.Fusion.Types;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Planning.Nodes;

internal static class SyntaxNodeHelpers
{
public static SelectionSetNode ToSyntaxNode(this IReadOnlyList<SelectionPlanNode> selections)
{
var selectionSet = ImmutableArray.CreateBuilder<ISelectionNode>();

foreach (var selection in selections)
{
selectionSet.Add(selection switch
{
FieldPlanNode field => field.ToSyntaxNode(),
InlineFragmentPlanNode inlineFragment => inlineFragment.ToSyntaxNode(),
_ => throw new InvalidOperationException()
});
}

return new SelectionSetNode(null, selectionSet.ToImmutable());
}

public static IReadOnlyList<DirectiveNode> ToSyntaxNode(this IReadOnlyList<CompositeDirective> directives)
{
var directiveNodes = ImmutableArray.CreateBuilder<DirectiveNode>();

foreach (var directive in directives)
{
directiveNodes.Add(directive.ToSyntaxNode());
}

return directiveNodes.ToImmutable();
}

public static IReadOnlyList<ArgumentNode> ToSyntaxNode(this IReadOnlyList<ArgumentAssignment> arguments)
{
var argumentNodes = ImmutableArray.CreateBuilder<ArgumentNode>();

foreach (var argument in arguments)
{
argumentNodes.Add(argument.ToSyntaxNode());
}

return argumentNodes.ToImmutable();
}
}

This file was deleted.

Loading
Loading