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

Fixed issue with variable forwarding in nested fields. #6348

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.Diagnostics;
using HotChocolate.Execution.Processing;
using HotChocolate.Fusion.Metadata;
Expand Down Expand Up @@ -55,6 +56,17 @@ internal RequestDocument CreateRequestDocument(

rootSelectionSetNode = new SelectionSetNode(new[] { rootResolver });
path = p;

var variables =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why an immutable dictionary. Those have bad lookup perf.

context.Operation.Definition.VariableDefinitions.ToImmutableDictionary(v => v.Variable.Name.Value);
var variablesToForward = ExtractVariablesToForward(variables, rootSelectionSetNode.Selections)
.Distinct()
.Where(v => !context.ForwardedVariables.Contains(v))
.ToArray();
foreach (var variable in variablesToForward)
{
context.ForwardedVariables.Add(variable);
}
}

var operationDefinitionNode = new OperationDefinitionNode(
Expand All @@ -73,6 +85,42 @@ internal RequestDocument CreateRequestDocument(
path);
}

protected static IEnumerable<VariableDefinitionNode> ExtractVariablesToForward(
IReadOnlyDictionary<string, VariableDefinitionNode> globalVariables,
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
IEnumerable<ISelectionNode> selections)
{
foreach (var selection in selections)
{
if (selection is FieldNode fieldNode)
{
foreach (var toForward in fieldNode.Arguments.Where(a => globalVariables.ContainsKey(a.Name.Value)))
{
var definition = globalVariables[toForward.Name.Value];
yield return definition;
}

if (fieldNode.SelectionSet == null) continue;

foreach (var toForward in ExtractVariablesToForward(globalVariables,
fieldNode.SelectionSet.Selections))
{
yield return toForward;
}
}

if (selection is InlineFragmentNode inlineFragment)
{
if (inlineFragment.SelectionSet == null) continue;

foreach (var toForward in ExtractVariablesToForward(globalVariables,
inlineFragment.SelectionSet.Selections))
{
yield return toForward;
}
}
}
}

protected virtual SelectionSetNode CreateRootSelectionSetNode(
QueryPlanContext context,
SelectionExecutionStep executionStep)
Expand Down
40 changes: 39 additions & 1 deletion src/HotChocolate/Fusion/test/Core.Tests/RequestPlannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ ... on Patient1 {
snapshot.Add(result.QueryPlan, nameof(result.QueryPlan));
await snapshot.MatchAsync();
}

[Fact]
public async Task Query_Plan_24_Field_Requirement_And_Fields_In_Context()
{
Expand Down Expand Up @@ -978,6 +978,44 @@ query Requires {
await snapshot.MatchAsync();
}


[Fact]
public async Task Query_Plan_25_Variables_Are_Passed_Through()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();

var fusionGraph = await new FusionGraphComposer().ComposeAsync(
new[]
{
demoProject.Appointment.ToConfiguration(),
demoProject.Patient1.ToConfiguration(),
},
new FusionFeatureCollection(FusionFeatures.NodeField));

// act
var result = await CreateQueryPlanAsync(
fusionGraph,
"""
query Appointments($first: Int!) {
patientById(patientId: 1) {
name
appointments(first: $first) {
nodes {
id
}
}
}
}
""");

// assert
var snapshot = new Snapshot();
snapshot.Add(result.UserRequest, nameof(result.UserRequest));
snapshot.Add(result.QueryPlan, nameof(result.QueryPlan));
await snapshot.MatchAsync();
}

private static async Task<(DocumentNode UserRequest, QueryPlan QueryPlan)> CreateQueryPlanAsync(
Skimmed.Schema fusionGraph,
[StringSyntax("graphql")] string query)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
UserRequest
---------------
query Appointments($first: Int!) {
patientById(patientId: 1) {
name
appointments(first: $first) {
nodes {
id
}
}
}
}
---------------

QueryPlan
---------------
{
"document": "query Appointments($first: Int!) { patientById(patientId: 1) { name appointments(first: $first) { nodes { id } } } }",
"operation": "Appointments",
"rootNode": {
"type": "Sequence",
"nodes": [
{
"type": "Resolve",
"subgraph": "Patient1",
"document": "query Appointments_1 { patientById(patientId: 1) { name __fusion_exports__1: id } }",
"selectionSetId": 0
},
{
"type": "Compose",
"selectionSetIds": [
0
]
},
{
"type": "Resolve",
"subgraph": "Appointment",
"document": "query Appointments_2($__fusion_exports__1: ID!, $first: Int!) { node(id: $__fusion_exports__1) { ... on Patient1 { appointments(first: $first) { nodes { id } } } } }",
"selectionSetId": 1,
"path": [
"node"
],
"requires": [
{
"variable": "__fusion_exports__1"
}
],
"forwardedVariables": [
{
"variable": "first"
}
]
},
{
"type": "Compose",
"selectionSetIds": [
1
]
}
]
}
}
---------------
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,26 @@ public IEnumerable<Appointment> Appointments()
return null;
}
}

[NodeResolver]
public Patient1? GetPatient(int id)
{
if (id == 1)
{
return new Patient1()
{
Id = 1
};
}
if (id == 2)
{
return new Patient1()
{
Id = 2
};
}

return null;

}
}
9 changes: 9 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Appointments/Patient1.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
using HotChocolate.Types;
using HotChocolate.Types.Relay;

namespace HotChocolate.Fusion.Shared.Appointments;

public class Patient1 : IPatient
{
[ID<Patient1>] public int Id { get; set;}

[UsePaging]
public IEnumerable<Appointment> Appointments => new List<Appointment>()
{
new Appointment(),
new Appointment(),
new Appointment(),
};
}