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

[Fusion] Fix QueryPlan bug #8020

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ private void CreateExecutionSteps(
}

executionStep.AllSelections.Add(selection);
executionStep.RootSelections.Add(new RootSelection(selection, resolver));
var rootSelection = new RootSelection(selection, resolver);
executionStep.RootSelections.Add(rootSelection);

if (resolver is not null)
{
Expand All @@ -195,7 +196,7 @@ private void CreateExecutionSteps(

if (selection.SelectionSet is not null)
{
CollectNestedSelections(
var couldPlanSelections = CollectNestedSelections(
backlog,
operation,
selection,
Expand All @@ -204,6 +205,15 @@ private void CreateExecutionSteps(
executionStep,
preferBatching,
context.ParentSelections);

if (!couldPlanSelections)
{
executionStep.AllSelections.Remove(selection);
executionStep.RootSelections.Remove(rootSelection);
executionStep.SelectionResolvers.Remove(selection);

(leftovers ??=[]).Add(selection);
}
}

path.RemoveAt(pathIndex);
Expand Down Expand Up @@ -281,7 +291,7 @@ private void HandleSpecialQuerySelections(
}
}

private void CollectNestedSelections(
private bool CollectNestedSelections(
Queue<BacklogItem> backlog,
IOperation operation,
ISelection parentSelection,
Expand All @@ -296,9 +306,10 @@ private void CollectNestedSelections(
preferBatching = parentSelection.Type.IsListType();
}

var overallCouldPlanSelections = false;
foreach (var possibleType in operation.GetPossibleTypes(parentSelection))
{
CollectNestedSelections(
var couldPlanSelections = CollectNestedSelections(
backlog,
operation,
parentSelection,
Expand All @@ -308,10 +319,17 @@ private void CollectNestedSelections(
possibleType,
preferBatching,
parentSelectionLookup);

if (couldPlanSelections)
{
overallCouldPlanSelections = true;
}
}

return overallCouldPlanSelections;
}

private void CollectNestedSelections(
private bool CollectNestedSelections(
Queue<BacklogItem> backlog,
IOperation operation,
ISelection parentSelection,
Expand Down Expand Up @@ -378,7 +396,7 @@ private void CollectNestedSelections(

if (selection.SelectionSet is not null)
{
CollectNestedSelections(
var couldPlanSelections = CollectNestedSelections(
backlog,
operation,
selection,
Expand All @@ -387,6 +405,14 @@ private void CollectNestedSelections(
executionStep,
preferBatching,
parentSelectionLookup);

if (!couldPlanSelections)
{
executionStep.AllSelections.Remove(selection);
executionStep.SelectionResolvers.Remove(selection);

(leftovers ??=[]).Add(selection);
}
}
}
else
Expand All @@ -399,6 +425,11 @@ private void CollectNestedSelections(

if (leftovers is not null)
{
if (leftovers.Count == selectionSet.Selections.Count)
{
return false;
}

backlog.Enqueue(
new BacklogItem(
parentSelection,
Expand All @@ -407,6 +438,8 @@ private void CollectNestedSelections(
leftovers,
preferBatching));
}

return true;
}

private static SelectionPath? CreateSelectionPath(SelectionPath? rootPath, List<ISelection> pathSegments)
Expand Down
127 changes: 127 additions & 0 deletions src/HotChocolate/Fusion/test/Core.Tests/DemoIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,133 @@ type ResaleSurveyFeedback {
await snapshot.MatchMarkdownAsync();
}

[Fact]
public async Task Viewer_Bug_1()
{
// arrange
var subgraphA = await TestSubgraph.CreateAsync(
"""
type Query {
recentTestimonial: Testimonial
viewer: Viewer
}

type Testimonial {
id: ID!
}

type Viewer {
name: String
}
""");
var subgraphB = await TestSubgraph.CreateAsync(
"""
type Query {
viewer: Viewer
}

type Viewer {
exclusiveSubgraphB: String
}
""");

using var subgraphs = new TestSubgraphCollection(output, [subgraphA, subgraphB]);
var executor = await subgraphs.GetExecutorAsync();

var request = Parse(
"""
query {
recentTestimonial {
__typename
}
viewer {
exclusiveSubgraphB
}
}
""");

// act
await using var result = await executor.ExecuteAsync(
OperationRequestBuilder
.New()
.SetDocument(request)
.Build());

// assert
var snapshot = new Snapshot();
CollectSnapshotData(snapshot, request, result);
await snapshot.MatchMarkdownAsync();
}


[Fact]
public async Task Viewer_Bug_2()
{
// arrange
var subgraphA = await TestSubgraph.CreateAsync(
"""
type Query {
recentTestimonial: Testimonial
viewer: Viewer
}

type Testimonial {
id: ID!
}

type Viewer {
subType: SubType
}

type SubType {
subgraphA: String
}
""");
var subgraphB = await TestSubgraph.CreateAsync(
"""
type Query {
viewer: Viewer
}

type Viewer {
subType: SubType
}

type SubType {
subgraphB: String
}
""");

using var subgraphs = new TestSubgraphCollection(output, [subgraphA, subgraphB]);
var executor = await subgraphs.GetExecutorAsync();

var request = Parse(
"""
query {
recentTestimonial {
__typename
}
viewer {
subType {
subgraphB
}
}
}
""");

// act
await using var result = await executor.ExecuteAsync(
OperationRequestBuilder
.New()
.SetDocument(request)
.Build());

// assert
var snapshot = new Snapshot();
CollectSnapshotData(snapshot, request, result);
await snapshot.MatchMarkdownAsync();
}

public sealed class HotReloadConfiguration : IObservable<GatewayConfiguration>
{
private GatewayConfiguration _configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Viewer_Bug_1

## Result

```json
{
"data": {
"recentTestimonial": {
"__typename": "Testimonial"
},
"viewer": {
"exclusiveSubgraphB": "string"
}
}
}
```

## Request

```graphql
{
recentTestimonial {
__typename
}
viewer {
exclusiveSubgraphB
}
}
```

## QueryPlan Hash

```text
4A67F4C0933C907EC1B605E7F1DB3F91C019E5A6
```

## QueryPlan

```json
{
"document": "{ recentTestimonial { __typename } viewer { exclusiveSubgraphB } }",
"rootNode": {
"type": "Sequence",
"nodes": [
{
"type": "Parallel",
"nodes": [
{
"type": "Resolve",
"subgraph": "Subgraph_1",
"document": "query fetch_recentTestimonial_viewer_1 { recentTestimonial { __typename } }",
"selectionSetId": 0
},
{
"type": "Resolve",
"subgraph": "Subgraph_2",
"document": "query fetch_recentTestimonial_viewer_2 { viewer { exclusiveSubgraphB } }",
"selectionSetId": 0
}
]
},
{
"type": "Compose",
"selectionSetIds": [
0
]
}
]
}
}
```

Loading
Loading