Skip to content

Commit

Permalink
Add more tests and fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Dec 9, 2024
1 parent c443c83 commit fdd6a5b
Show file tree
Hide file tree
Showing 98 changed files with 5,277 additions and 1,046 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void InlineFragmentDefinition(
{
var fragmentContext = context.Branch(typeCondition);

RewriteFields(fragmentDefinition.SelectionSet, context);
RewriteFields(fragmentDefinition.SelectionSet, fragmentContext);

var selectionSet = new SelectionSetNode(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public bool RemoveDirective(CompositeDirective directive)

private void InitializeConditions()
{
if(_isConditional.HasValue)
if (_isConditional.HasValue)
{
return;
}
Expand All @@ -170,7 +170,7 @@ private void InitializeConditions()
continue;
}

if (directive.Name.Value.Equals("skip"))
if (directive.Name.Value.Equals("include"))
{
_includeVariable = GetVariableName(directive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public RootPlanNode CreatePlan(DocumentNode document, string? operationName)
var context = new PlaningContext(operation, operation, ImmutableStack<SelectionPathSegment>.Empty);
if (TryPlanSelectionSet(context))
{
PlanConditionNode(operation, operation.Selections);
TryMakeOperationConditional(operation, operation.Selections);
operationPlan.AddOperation(operation);
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ private bool TryHandleUnresolvedSelections(
}

schemasInContext.Add(schemaName, lookupOperation);
PlanConditionNode(lookupOperation, lookupField.Selections);
TryMakeOperationConditional(lookupOperation, lookupField.Selections);

// we add the lookup operation to all the schemas that we have requirements with.
foreach (var requiredSchema in fieldSchemaDependencies.Values.Distinct())
Expand Down Expand Up @@ -607,7 +607,7 @@ private static FieldNode CreateFieldNodeFromPath(FieldPath path)
return current!;
}

private void PlanConditionNode(
private void TryMakeOperationConditional(
OperationPlanNode operation,
IReadOnlyList<SelectionPlanNode> selections)
{
Expand Down Expand Up @@ -655,9 +655,8 @@ private void PlanConditionNode(
}
}

private bool IsSelectionAlwaysSkipped(ISelectionNode selectionNode)
private static bool IsSelectionAlwaysSkipped(ISelectionNode selectionNode)
{
var selectionIsSkipped = false;
foreach (var directive in selectionNode.Directives)
{
var isSkipDirective = directive.Name.Value == "skip";
Expand All @@ -673,26 +672,19 @@ private bool IsSelectionAlwaysSkipped(ISelectionNode selectionNode)
{
if (booleanValueNode.Value && isSkipDirective)
{
selectionIsSkipped = true;
return true;
}
else if (!booleanValueNode.Value && isIncludedDirective)
{
selectionIsSkipped = true;
}
else

if (!booleanValueNode.Value && isIncludedDirective)
{
selectionIsSkipped = false;
return true;
}
}
else
{
selectionIsSkipped = false;
}
}
}
}

return selectionIsSkipped;
return false;
}

private string GetNextRequirementName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void BindOperationVariables(
OperationDefinitionNode operationDefinition,
RootPlanNode operationPlan)
{
var operationBacklog = new Stack<OperationPlanNode>(operationPlan.Operations.OfType<OperationPlanNode>());
var operationBacklog = new Stack<OperationPlanNode>(operationPlan.Operations);
var selectionBacklog = new Stack<SelectionPlanNode>();
var variableDefinitions = operationDefinition.VariableDefinitions.ToDictionary(t => t.Variable.Name.Value);
var usedVariables = new HashSet<string>();
Expand All @@ -18,7 +18,7 @@ public static void BindOperationVariables(
{
CollectAndBindUsedVariables(operation, variableDefinitions, usedVariables, selectionBacklog);

foreach (var child in operation.Dependants.OfType<OperationPlanNode>())
foreach (var child in operation.Dependants)
{
operationBacklog.Push(child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace HotChocolate.Fusion.Planning;

public static class PlanNodeJsonFormatter
{
public static void FormatToJson(this RootPlanNode root, IBufferWriter<byte> writer)
{
var jsonWriter = new Utf8JsonWriter(writer, new JsonWriterOptions { Indented = true });
jsonWriter.Write(root);
jsonWriter.Flush();
}

public static string ToJson(this RootPlanNode root)
{
var buffer = new ArrayBufferWriter<byte>();
Expand All @@ -16,7 +23,7 @@ public static string ToJson(this RootPlanNode root)
return Encoding.UTF8.GetString(buffer.WrittenSpan);
}

public static void Write(this Utf8JsonWriter writer, RootPlanNode root)
private static void Write(this Utf8JsonWriter writer, RootPlanNode root)
{
var nodeIdLookup = CollectNodeIds(root);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Buffers.Text;
using System.Text;
using System.Text.Json;
using HotChocolate.Fusion.Planning.Nodes;
Expand All @@ -7,6 +8,14 @@ namespace HotChocolate.Fusion.Planning;

public static class PlanNodeYamlFormatter
{
public static void FormatToYaml(this RootPlanNode root, IBufferWriter<byte> writer)
{
var yaml = root.ToYaml();

// Performance go brrr
writer.Write(Encoding.UTF8.GetBytes(yaml));
}

public static string ToYaml(this RootPlanNode root)
{
var sb = new StringBuilder();
Expand All @@ -16,7 +25,7 @@ public static string ToYaml(this RootPlanNode root)
return sb.ToString();
}

public static void Write(this StringWriter writer, RootPlanNode root)
private static void Write(this StringWriter writer, RootPlanNode root)
{
var nodeIdLookup = CollectNodeIds(root);

Expand Down Expand Up @@ -57,7 +66,7 @@ private static void WriteOperationNode(

if (operation.IncludeVariable is not null)
{
writer.WriteLine(" includeIf: \"{0}\"", operation.SkipVariable);
writer.WriteLine(" includeIf: \"{0}\"", operation.IncludeVariable);
}

if (operation.Requirements.Count > 0)
Expand Down
Loading

0 comments on commit fdd6a5b

Please sign in to comment.