Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Fusion.Types;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Types;

namespace HotChocolate.Fusion.Planning.Partitioners;
Expand Down Expand Up @@ -145,42 +144,52 @@ private void AddSelectionsForConcreteType(

if (cloneSelectionSets)
{
var rewrittenSelections = new List<ISelectionNode>(selections.Count);

foreach (var selection in selections)
{
var rewrittenSelection = SyntaxRewriter.Create(
node =>
{
if (node is SelectionSetNode selectionSetNode)
{
var newSelectionSet = new SelectionSetNode(selectionSetNode.Selections);

// Since we're cloning the selection set,
// we also need to keep track of the original
// selection set the cloned one belongs to,
// so we can later insert requirements in the original one.
context.SelectionSetIndexBuilder.RegisterCloned(
selectionSetNode,
newSelectionSet);

return newSelectionSet;
}

return node;
}).Rewrite(selection)!;

rewrittenSelections.Add(rewrittenSelection);
typeSelections.Add(CloneSelection(selection, context.SelectionSetIndexBuilder));
}

typeSelections.AddRange(rewrittenSelections);
}
else
{
typeSelections.AddRange(selections);
}
}

// Walks the selection top-down, cloning every nested SelectionSetNode so that
// each clone can be registered against its original in the index. We can't use
// the bottom-up SyntaxRewriter for this, because by the time it reaches an outer
// SelectionSetNode its children have already been replaced, leaving the rewriter
// with a freshly allocated node that isn't tracked in the index.
private static ISelectionNode CloneSelection(
ISelectionNode selection,
SelectionSetIndexBuilder indexBuilder)
{
return selection switch
{
FieldNode field when field.SelectionSet is not null
=> field.WithSelectionSet(CloneSelectionSet(field.SelectionSet, indexBuilder)),
InlineFragmentNode fragment
=> fragment.WithSelectionSet(CloneSelectionSet(fragment.SelectionSet, indexBuilder)),
_ => selection
};
}

private static SelectionSetNode CloneSelectionSet(
SelectionSetNode original,
SelectionSetIndexBuilder indexBuilder)
{
var clonedSelections = new ISelectionNode[original.Selections.Count];

for (var i = 0; i < original.Selections.Count; i++)
{
clonedSelections[i] = CloneSelection(original.Selections[i], indexBuilder);
}

var cloned = new SelectionSetNode(clonedSelections);
indexBuilder.RegisterCloned(original, cloned);
return cloned;
}

private static List<ISelectionNode> GetSelectionsWithPath(
Stack<InlineFragmentNode> fragmentPath,
List<ISelectionNode> selections,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace HotChocolate.Fusion;

// TODO:
// - Selections on interface, all types of interfaces are on same subgraph and there's only node lookup
public class GlobalObjectIdentificationTests : FusionTestBase
{
[Fact]
Expand Down Expand Up @@ -1475,4 +1473,163 @@ ... on Discussion {
// assert
await MatchSnapshotAsync(gateway, request, result);
}

[Fact]
public async Task Node_Field_Interface_Selection_All_Implementations_In_Same_Subgraph_Only_Node_Lookup()
{
// arrange
using var serverA = CreateSourceSchema(
"A",
"""
type Query {
node(id: ID!): Node @lookup
}

interface Node {
id: ID!
}

interface IOrder {
id: ID!
name: String!
}

type Order1 implements Node & IOrder {
id: ID!
name: String!
}

type Order2 implements Node & IOrder {
id: ID!
name: String!
}
""");

using var gateway = await CreateCompositeSchemaAsync(
[
("A", serverA)
]);

// act
using var client = GraphQLHttpClient.Create(gateway.CreateClient());

var request = new OperationRequest(
"""
{
# Order1:1
node(id: "T3JkZXIxOjE=") {
... on IOrder {
name
}
}
}
""");

using var result = await client.PostAsync(
request,
new Uri("http://localhost:5000/graphql"));

// assert
await MatchSnapshotAsync(gateway, request, result);
}

[Fact]
public async Task Node_Field_Interface_Selection_With_Nested_Field_From_Other_Subgraph()
{
// arrange
using var serverA = CreateSourceSchema(
"A",
"""
type Query {
node(id: ID!): Node @lookup @shareable
order1ById(id: ID!): Order1 @lookup
order2ById(id: ID!): Order2 @lookup
orders: [IOrder!]!
}

interface Node {
id: ID!
}

interface IOrder {
id: ID!
name: String!
product: Product!
}

type Order1 implements Node & IOrder {
id: ID!
name: String!
product: Product!
}

type Order2 implements Node & IOrder {
id: ID!
name: String!
product: Product!
}

type Product {
id: ID! @shareable
}
""");

using var serverB = CreateSourceSchema(
"B",
"""
type Query {
node(id: ID!): Node @lookup @shareable
products: [Product!]!
productById(id: ID!): Product @lookup @internal
}

interface Node {
id: ID!
}

type Product implements Node {
id: ID! @shareable
name: String!
comments: [Comment!]!
}

type Comment {
content: String!
}
""");

using var gateway = await CreateCompositeSchemaAsync(
[
("A", serverA),
("B", serverB)
]);

// act
using var client = GraphQLHttpClient.Create(gateway.CreateClient());

var request = new OperationRequest(
"""
{
# Order1:1
node(id: "T3JkZXIxOjE=") {
... on IOrder {
name
product {
name
comments {
content
}
}
}
}
}
""");

using var result = await client.PostAsync(
request,
new Uri("http://localhost:5000/graphql"));

// assert
await MatchSnapshotAsync(gateway, request, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
title: Node_Field_Interface_Selection_All_Implementations_In_Same_Subgraph_Only_Node_Lookup
request:
document: |
{
node(id: "T3JkZXIxOjE=") {
... on IOrder {
name
}
}
}
response:
body: |
{
"data": {
"node": {
"name": "Order1: T3JkZXIxOjE="
}
}
}
sourceSchemas:
- name: A
schema: |
schema {
query: Query
}

type Query {
node(id: ID!): Node @lookup
}

type Order1 implements Node & IOrder {
id: ID!
name: String!
}

type Order2 implements Node & IOrder {
id: ID!
name: String!
}

interface IOrder {
id: ID!
name: String!
}

interface Node {
id: ID!
}
interactions:
- request:
accept: application/graphql-response+json; charset=utf-8, application/json; charset=utf-8, application/jsonl; charset=utf-8, text/event-stream; charset=utf-8
document: |
query Op_a088b140_4 {
node(id: "T3JkZXIxOjE=") {
__typename
... on Order1 {
__typename
name
}
}
}
response:
results:
- |
{
"data": {
"node": {
"__typename": "Order1",
"name": "Order1: T3JkZXIxOjE="
}
}
}
operationPlan:
operation:
- document: |
{
node(id: "T3JkZXIxOjE=") {
__typename @fusion__requirement
... on IOrder {
name
}
}
}
hash: a088b140315a0c889324bbc829bbc277
searchSpace: 1
expandedNodes: 1
nodes:
- id: 1
type: Node
idValue: "T3JkZXIxOjE="
responseName: node
branches:
- Order1: 4
- Order2: 3
fallback: 2
- id: 2
type: Operation
operation: |
query Op_a088b140_2 {
node(id: "T3JkZXIxOjE=") {
__typename
}
}
dependencies:
- id: 1
- id: 3
type: Operation
schema: A
operation: |
query Op_a088b140_3 {
node(id: "T3JkZXIxOjE=") {
__typename
... on Order2 {
__typename
name
}
}
}
dependencies:
- id: 1
- id: 4
type: Operation
schema: A
operation: |
query Op_a088b140_4 {
node(id: "T3JkZXIxOjE=") {
__typename
... on Order1 {
__typename
name
}
}
}
dependencies:
- id: 1
Loading
Loading