Skip to content

Commit ff57b88

Browse files
Insert type refinement for abstract lookups
1 parent 0e0f380 commit ff57b88

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Metadata/Lookup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public Lookup(
6565
/// </summary>
6666
public string Name { get; }
6767

68+
/// <summary>
69+
/// Get the name of the declaring type.
70+
/// </summary>
71+
public string DeclaringTypeName => _declaringTypeName;
72+
6873
/// <summary>
6974
/// Gets the arguments that represent field requirements.
7075
/// </summary>

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/OperationPlanner.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ lookup is null
295295
requirements = requirements.Add(argumentRequirementKey, operationRequirement);
296296
}
297297

298+
// If the lookup returns an abstract type, we might need to insert a type refinement
299+
// around our selection set.
300+
if (_schema.Types.TryGetType(lookup.DeclaringTypeName, out var lookupDeclaringType)
301+
&& lookupDeclaringType != workItem.SelectionSet.Type)
302+
{
303+
var typeRefinement =
304+
new InlineFragmentNode(
305+
null,
306+
new NamedTypeNode(workItem.SelectionSet.Type.Name),
307+
[],
308+
resolvable);
309+
var selectionSetWithTypeRefinement = new SelectionSetNode(null, [typeRefinement]);
310+
311+
var indexBuilder = index.ToBuilder();
312+
313+
indexBuilder.Register(resolvable, selectionSetWithTypeRefinement);
314+
315+
index = indexBuilder;
316+
317+
operationBuilder.SetSelectionSet(selectionSetWithTypeRefinement);
318+
}
319+
298320
operationBuilder.SetLookup(lookup, requirementKey);
299321
}
300322

src/HotChocolate/Fusion-vnext/test/Fusion.Execution.Tests/Planning/__snapshots__/OperationPlannerTests.Plan_Simple_Interface_Lookup.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
operation:
22
- document: >-
3-
query GetTopProducts {
4-
topProduct {
5-
id
6-
name
7-
price
8-
id @fusion__requirement
9-
}
3+
query GetTopProducts {
4+
topProduct {
5+
id
6+
name
7+
price
8+
id @fusion__requirement
109
}
10+
}
1111
name: GetTopProducts
1212
hash: 123
1313
nodes:
@@ -27,7 +27,9 @@ nodes:
2727
$__fusion_1_id: ID!
2828
) {
2929
node(id: $__fusion_1_id) {
30-
price
30+
... on Product {
31+
price
32+
}
3133
}
3234
}
3335
source: $.node

src/HotChocolate/Fusion-vnext/test/Fusion.Execution.Tests/Planning/__snapshots__/OperationPlannerTests.Plan_Simple_Union_Lookup.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
operation:
22
- document: >-
3-
query GetTopProducts {
4-
topProduct {
5-
id
6-
name
7-
price
8-
id @fusion__requirement
9-
}
3+
query GetTopProducts {
4+
topProduct {
5+
id
6+
name
7+
price
8+
id @fusion__requirement
109
}
10+
}
1111
name: GetTopProducts
1212
hash: 123
1313
nodes:
@@ -27,7 +27,9 @@ nodes:
2727
$__fusion_1_id: ID!
2828
) {
2929
lookupUnionById(id: $__fusion_1_id) {
30-
price
30+
... on Product {
31+
price
32+
}
3133
}
3234
}
3335
source: $.lookupUnionById

0 commit comments

Comments
 (0)