Skip to content

Commit

Permalink
Fixed requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Dec 9, 2024
1 parent 3121252 commit 814b2f1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,26 @@ private bool TryHandleUnresolvedSelections(
schemasInContext[requiredSchema].AddDependantOperation(lookupOperation);
}

// TODO: we need to include the entity path in here.
// actually ... we need to redo the whole path thingy.
// only the first one is path - entity path.
// second one is operation + entity path.
var currentSelectionPath = CreateFieldPath(context.Path);

// add requirements to the operation
for (var i = 0; i < lookup.Fields.Length; i++)
{
var requirementName = GetNextRequirementName();
var requiredField = lookup.Fields[i];
var argument = lookup.Arguments[i];

// should we store on the requirement the operation from which data is required?
var requiredFromSchema = fieldSchemaDependencies[requiredField];
var requiredFromOperation = schemasInContext[requiredFromSchema];
var requiredFromSelectionSet = requiredFromOperation != context.Operation
? requiredFromOperation.Selections.Single()
: context.Parent;
var requiredFromPathStack = requiredFromOperation != context.Operation
? ImmutableStack<SelectionPathSegment>.Empty.Push(new SelectionPathSegment(lookupField))
: context.Path;
var requiredFromPath = CreateFieldPath(requiredFromPathStack);
var requiredFromContext = new PlaningContext(
requiredFromOperation,
requiredFromSelectionSet,
// path is wrong ... we must check if this is a lookup context and build new path for it
context.Path);
requiredFromPathStack);

if (!TryPlanSelection(
requiredFromContext,
Expand All @@ -323,7 +319,7 @@ private bool TryHandleUnresolvedSelections(
var requirement = new FieldRequirementPlanNode(
requirementName,
requiredFromOperation,
currentSelectionPath,
requiredFromPath,
requiredField,
argument.Type);

Expand Down Expand Up @@ -582,7 +578,7 @@ private static FieldPath CreateFieldPath(ImmutableStack<SelectionPathSegment> pa
{
var current = FieldPath.Root;

foreach (var segment in path)
foreach (var segment in path.Reverse())
{
if (segment.PlanNode is FieldPlanNode field)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void WriteOperationNode(
writer.WriteString("name", requirement.Name);
writer.WriteNumber("dependsOn", nodeIdLookup[requirement.From]);

writer.WritePropertyName("field");
writer.WritePropertyName("selectionSet");
writer.WriteStartArray();

foreach (var segment in requirement.SelectionSet.Reverse())
Expand All @@ -76,6 +76,16 @@ private static void WriteOperationNode(

writer.WriteEndArray();

writer.WritePropertyName("field");
writer.WriteStartArray();

foreach (var segment in requirement.RequiredField.Reverse())
{
writer.WriteStringValue(segment.Name);
}

writer.WriteEndArray();

writer.WriteString("type", requirement.Type.ToString(false));
writer.WriteEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private static void WriteOperationNode(
{
writer.WriteLine(" - name: \"{0}\"", requirement.Name);
writer.WriteLine(" dependsOn: \"{0}\"", nodeIdLookup[requirement.From]);
writer.WriteLine(" field: \"{0}\"", requirement.SelectionSet);
writer.WriteLine(" selectionSet: \"{0}\"", requirement.SelectionSet);
writer.WriteLine(" field: \"{0}\"", requirement.RequiredField);
writer.WriteLine(" type: \"{0}\"", requirement.Type.ToString(false));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,21 @@ public override string ToString()

do
{
if(ReferenceEquals(current, Root))
{
break;
}

if (first)
{
path.Insert(0, current.Name);
path.Insert(0, '.');
first = false;
}
else
{
path.Insert(0, ".");
}

path.Insert(0, current.Name);

current = current.Parent;
} while (current != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ query GetProduct($id: ID!, $skip: Boolean!) {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -317,7 +318,8 @@ query GetProduct($id: ID!) {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -370,7 +372,8 @@ query GetProduct($id: ID!) {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -427,7 +430,8 @@ query GetProduct($id: ID!, $skip: Boolean!) {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -483,7 +487,8 @@ query GetProduct($id: ID!) {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HotChocolate.Fusion.Planning;
using HotChocolate.Fusion.Planning.Nodes;
using HotChocolate.Fusion.Types.Completion;
using HotChocolate.Language;

Expand Down Expand Up @@ -94,7 +93,8 @@ fragment Product on Product {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -170,7 +170,8 @@ fragment AuthorCard on UserProfile {
requirements:
- name: "__fusion_requirement_2"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
- id: 3
schema: "ACCOUNTS"
Expand All @@ -183,7 +184,8 @@ fragment AuthorCard on UserProfile {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "2"
field: ".productById"
selectionSet: "productById.reviews.nodes.author"
field: "id"
type: "ID!"
""");
Expand Down Expand Up @@ -259,7 +261,8 @@ fragment AuthorCard on UserProfile {
requirements:
- name: "__fusion_requirement_2"
dependsOn: "1"
field: ".productById"
selectionSet: "productById"
field: "id"
type: "ID!"
- id: 3
schema: "ACCOUNTS"
Expand All @@ -272,7 +275,8 @@ fragment AuthorCard on UserProfile {
requirements:
- name: "__fusion_requirement_1"
dependsOn: "2"
field: ".productById"
selectionSet: "productById.reviews.nodes.author"
field: "id"
type: "ID!"
""");
Expand Down

0 comments on commit 814b2f1

Please sign in to comment.