Skip to content

Commit 217fcee

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

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class Lookup : INeedsCompletion
1717
/// </summary>
1818
/// <param name="schemaName">The name of the source schema.</param>
1919
/// <param name="declaringTypeName">The name of the type that declares the field.</param>
20-
/// <param name="name">The name of the lookup field.</param>
20+
/// <param name="fieldName">The name of the lookup field.</param>
2121
/// <param name="arguments">The arguments that represent field requirements.</param>
2222
/// <param name="fields">The paths to the field that are required.</param>
2323
/// <exception cref="ArgumentException">
@@ -30,13 +30,13 @@ public sealed class Lookup : INeedsCompletion
3030
public Lookup(
3131
string schemaName,
3232
string declaringTypeName,
33-
string name,
33+
string fieldName,
3434
ImmutableArray<LookupArgument> arguments,
3535
ImmutableArray<IValueSelectionNode> fields)
3636
{
3737
ArgumentException.ThrowIfNullOrEmpty(schemaName);
3838
ArgumentException.ThrowIfNullOrEmpty(declaringTypeName);
39-
ArgumentException.ThrowIfNullOrEmpty(name);
39+
ArgumentException.ThrowIfNullOrEmpty(fieldName);
4040

4141
if (arguments.Length == 0)
4242
{
@@ -50,7 +50,7 @@ public Lookup(
5050

5151
_declaringTypeName = declaringTypeName;
5252
SchemaName = schemaName;
53-
Name = name;
53+
FieldName = fieldName;
5454
Arguments = arguments;
5555
Fields = fields;
5656
}
@@ -63,7 +63,12 @@ public Lookup(
6363
/// <summary>
6464
/// Gets the name of the lookup field.
6565
/// </summary>
66-
public string Name { get; }
66+
public string FieldName { get; }
67+
68+
/// <summary>
69+
/// Get the name of lookup field type.
70+
/// </summary>
71+
public string FieldType => _declaringTypeName;
6772

6873
/// <summary>
6974
/// Gets the arguments that represent field requirements.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public OperationDefinitionBuilder SetSelectionSet(SelectionSetNode selectionSet)
7676
}
7777

7878
var lookupField = new FieldNode(
79-
new NameNode(_lookup.Name),
79+
new NameNode(_lookup.FieldName),
8080
null,
8181
[],
8282
arguments,
@@ -87,7 +87,7 @@ public OperationDefinitionBuilder SetSelectionSet(SelectionSetNode selectionSet)
8787
var indexBuilder = index.ToBuilder();
8888
indexBuilder.Register(selectionSet);
8989
index = indexBuilder;
90-
selectionPath = selectionPath.AppendField(_lookup.Name);
90+
selectionPath = selectionPath.AppendField(_lookup.FieldName);
9191
}
9292

9393
var definition = new OperationDefinitionNode(

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.FieldType, out var lookupFieldType)
301+
&& lookupFieldType != 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)