Skip to content

Commit 8f76d7f

Browse files
[Fusion] Support abstract lookups (#8485)
Co-authored-by: Michael Staib <[email protected]>
1 parent 8295f06 commit 8f76d7f

18 files changed

+384
-40
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Extensions/MutableObjectTypeDefinitionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public static IEnumerable<IDirective> GetFusionLookupDirectives(
3434
.ToList();
3535

3636
// To use an abstract lookup, the type must exist in the source schema.
37+
// TODO: For abstract lookups we also need to validate that the type
38+
// is part of the abstract type's possible types in the specific source schema.
3739
if (type.ExistsInSchema(schemaName))
3840
{
3941
// Interface lookups.

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Collections/SourceInterfaceTypeCollection.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33

44
namespace HotChocolate.Fusion.Types.Collections;
55

6-
public class SourceInterfaceTypeCollection
7-
: SourceMemberCollection<SourceInterfaceType>
6+
public class SourceInterfaceTypeCollection(IEnumerable<SourceInterfaceType> members)
7+
: SourceMemberCollection<SourceInterfaceType>(members)
88
, ISourceComplexTypeCollection<SourceInterfaceType>
99
, ISourceComplexTypeCollection<ISourceComplexType>
1010
{
11-
public SourceInterfaceTypeCollection(IEnumerable<SourceInterfaceType> members)
12-
: base(members)
13-
{
14-
}
15-
1611
ISourceComplexType ISourceMemberCollection<ISourceComplexType>.this[string schemaName]
1712
=> this[schemaName];
1813

@@ -22,12 +17,9 @@ public ImmutableArray<SourceInterfaceType> Types
2217
ImmutableArray<ISourceComplexType> ISourceComplexTypeCollection<ISourceComplexType>.Types
2318
=> [.. Members];
2419

25-
public bool TryGetType(string schemaName, [NotNullWhen(true)] out SourceInterfaceType? type)
26-
=> TryGetMember(schemaName, out type);
27-
28-
public bool TryGetType(string schemaName, [NotNullWhen(true)] out ISourceComplexType? type)
20+
public bool TryGetMember(string schemaName, [NotNullWhen(true)] out ISourceComplexType? type)
2921
{
30-
if (TryGetMember(schemaName, out var member))
22+
if (base.TryGetMember(schemaName, out var member))
3123
{
3224
type = member;
3325
return true;

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Collections/SourceMemberCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SourceMemberCollection<TMember>
1111
{
1212
private readonly FrozenDictionary<string, TMember> _members;
1313

14-
protected SourceMemberCollection(IEnumerable<TMember> members)
14+
public SourceMemberCollection(IEnumerable<TMember> members)
1515
{
1616
_members = members.ToFrozenDictionary(t => t.SchemaName);
1717
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Collections/SourceObjectTypeCollection.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ public ImmutableArray<SourceObjectType> Types
1717
ImmutableArray<ISourceComplexType> ISourceComplexTypeCollection<ISourceComplexType>.Types
1818
=> [.. Members];
1919

20-
public bool TryGetType(string schemaName, [NotNullWhen(true)] out SourceObjectType? type)
21-
=> TryGetMember(schemaName, out type);
22-
23-
public bool TryGetType(string schemaName, [NotNullWhen(true)] out ISourceComplexType? type)
20+
public bool TryGetMember(string schemaName, [NotNullWhen(true)] out ISourceComplexType? type)
2421
{
25-
if (TryGetMember(schemaName, out var member))
22+
if (base.TryGetMember(schemaName, out var member))
2623
{
2724
type = member;
2825
return true;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace HotChocolate.Fusion.Types.Collections;
2+
3+
public class SourceUnionTypeCollection(IEnumerable<SourceUnionType> members)
4+
: SourceMemberCollection<SourceUnionType>(members)
5+
{
6+
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Completion/CompletionTools.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ public static SourceInterfaceTypeCollection CreateSourceInterfaceTypeCollection(
137137
return new SourceInterfaceTypeCollection(temp);
138138
}
139139

140+
public static SourceUnionTypeCollection CreateSourceUnionTypeCollection(
141+
UnionTypeDefinitionNode typeDef,
142+
CompositeSchemaBuilderContext context)
143+
{
144+
var types = TypeDirectiveParser.Parse(typeDef.Directives);
145+
var lookupDirectives = LookupDirectiveParser.Parse(typeDef.Directives);
146+
var temp = new SourceUnionType[types.Length];
147+
148+
for (var i = 0; i < types.Length; i++)
149+
{
150+
var type = types[i];
151+
var lookups = GetLookupBySchema(lookupDirectives, type.SchemaName, typeDef.Name.Value);
152+
context.RegisterForCompletionRange(lookups);
153+
154+
temp[i] = new SourceUnionType(
155+
typeDef.Name.Value,
156+
type.SchemaName,
157+
lookups);
158+
}
159+
160+
return new SourceUnionTypeCollection(temp);
161+
}
162+
140163
private static ImmutableArray<Lookup> GetLookupBySchema(
141164
ImmutableArray<LookupDirective> allLookups,
142165
string schemaName,

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Completion/CompositeSchemaBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ private static void CompleteUnionType(
532532
{
533533
var directives = CompletionTools.CreateDirectiveCollection(typeDef.Directives, context);
534534
var types = CompletionTools.CreateObjectTypeCollection(typeDef.Types, context);
535-
type.Complete(new CompositeUnionTypeCompletionContext(types, directives, FeatureCollection.Empty));
535+
var sources = CompletionTools.CreateSourceUnionTypeCollection(typeDef, context);
536+
type.Complete(new CompositeUnionTypeCompletionContext(types, directives, sources, FeatureCollection.Empty));
536537
}
537538

538539
private static void CompleteOutputField(

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Completion/CompositeUnionTypeCompletionContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ namespace HotChocolate.Fusion.Types.Completion;
66
internal readonly ref struct CompositeUnionTypeCompletionContext(
77
FusionObjectTypeDefinitionCollection types,
88
FusionDirectiveCollection directives,
9+
SourceUnionTypeCollection sources,
910
IFeatureCollection features)
1011
{
1112
public FusionDirectiveCollection Directives { get; } = directives;
1213

1314
public FusionObjectTypeDefinitionCollection Types { get; } = types;
1415

16+
public SourceUnionTypeCollection Sources { get; } = sources;
17+
1518
public IFeatureCollection Features { get; } = features;
1619
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Contracts/ISourceComplexTypeCollection.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ public interface ISourceComplexTypeCollection<TType>
77
: ISourceMemberCollection<TType>
88
where TType : ISourceComplexType
99
{
10-
bool TryGetType(string schemaName, [NotNullWhen(true)] out TType? type);
11-
1210
ImmutableArray<TType> Types { get; }
1311
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Contracts/ISourceMemberCollection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System.Collections.Immutable;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace HotChocolate.Fusion.Types;
45

5-
public interface ISourceMemberCollection<out TMember>
6+
public interface ISourceMemberCollection<TMember>
67
: IEnumerable<TMember>
78
where TMember : ISourceMember
89
{
@@ -13,4 +14,6 @@ public interface ISourceMemberCollection<out TMember>
1314
bool ContainsSchema(string schemaName);
1415

1516
ImmutableArray<string> Schemas { get; }
17+
18+
bool TryGetMember(string schemaName, [NotNullWhen(true)] out TMember? type);
1619
}

0 commit comments

Comments
 (0)