Skip to content

Commit 6080fe6

Browse files
authored
[Fusion] Adds Introspection Support (#8439)
1 parent 0746ff0 commit 6080fe6

File tree

65 files changed

+2993
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2993
-232
lines changed

src/HotChocolate/Core/src/Types.Abstractions/Types/IScalarTypeDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public interface IScalarTypeDefinition
77
, IInputTypeDefinition
88
, ISyntaxNodeProvider<ScalarTypeDefinitionNode>
99
{
10+
Uri? SpecifiedBy { get; }
11+
1012
/// <summary>
1113
/// Checks if the value is an instance of this type.
1214
/// </summary>

src/HotChocolate/Core/src/Types/Types/Introspection/__Field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace HotChocolate.Types.Introspection;
1313

14-
[Introspection]
1514
// ReSharper disable once InconsistentNaming
15+
[Introspection]
1616
internal sealed class __Field : ObjectType<IOutputFieldDefinition>
1717
{
1818
protected override ObjectTypeConfiguration CreateConfiguration(ITypeDiscoveryContext context)

src/HotChocolate/Core/src/Validation/Rules/VariableVisitor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ protected override ISyntaxVisitorAction Enter(
121121
{
122122
if (IntrospectionFieldNames.TypeName.Equals(node.Name.Value, StringComparison.Ordinal))
123123
{
124+
if (node.Directives.Count > 0)
125+
{
126+
foreach (var directive in node.Directives)
127+
{
128+
var result = Visit(directive, context);
129+
if (result.IsBreak())
130+
{
131+
return result;
132+
}
133+
}
134+
}
135+
124136
return Skip;
125137
}
126138

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed class FusionDirectiveCollection
1212

1313
public FusionDirectiveCollection(FusionDirective[] directives)
1414
{
15+
ArgumentNullException.ThrowIfNull(directives);
1516
_directives = directives;
1617
}
1718

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections;
2+
using System.Collections.Frozen;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Runtime.CompilerServices;
5+
using HotChocolate.Types;
6+
7+
namespace HotChocolate.Fusion.Types.Collections;
8+
9+
public sealed class FusionEnumValueCollection
10+
: IReadOnlyList<FusionEnumValue>
11+
, IReadOnlyEnumValueCollection
12+
{
13+
private readonly FusionEnumValue[] _values;
14+
private readonly FrozenDictionary<string, FusionEnumValue> _map;
15+
16+
public FusionEnumValueCollection(FusionEnumValue[] values)
17+
{
18+
ArgumentNullException.ThrowIfNull(values);
19+
_map = values.ToFrozenDictionary(t => t.Name);
20+
_values = values;
21+
}
22+
23+
public int Count => _values.Length;
24+
25+
/// <summary>
26+
/// Gets the enum value with the specified name.
27+
/// </summary>
28+
public FusionEnumValue this[string name] => _map[name];
29+
30+
IEnumValue IReadOnlyEnumValueCollection.this[string name] => _map[name];
31+
32+
public FusionEnumValue this[int index] => _values[index];
33+
34+
IEnumValue IReadOnlyList<IEnumValue>.this[int index] => this[index];
35+
36+
/// <summary>
37+
/// Tries to get the <paramref name="value"/> for
38+
/// the specified <paramref name="name"/>.
39+
/// </summary>
40+
/// <param name="name">
41+
/// The GraphQL enum value name.
42+
/// </param>
43+
/// <param name="value">
44+
/// The GraphQL enum value.
45+
/// </param>
46+
/// <returns>
47+
/// <c>true</c> if the <paramref name="name"/> represents a value of this enum type;
48+
/// otherwise, <c>false</c>.
49+
/// </returns>
50+
public bool TryGetValue(string name, [NotNullWhen(true)] out FusionEnumValue? value)
51+
=> _map.TryGetValue(name, out value);
52+
53+
bool IReadOnlyEnumValueCollection.TryGetValue(string name, [NotNullWhen(true)] out IEnumValue? value)
54+
{
55+
if(_map.TryGetValue(name, out var enumValue))
56+
{
57+
value = enumValue;
58+
return true;
59+
}
60+
61+
value = null;
62+
return false;
63+
}
64+
65+
/// <summary>
66+
/// Determines whether the collection contains an enum value with the specified name.
67+
/// </summary>
68+
/// <param name="name">
69+
/// The GraphQL enum value name.
70+
/// </param>
71+
/// <returns>
72+
/// <c>true</c> if the collection contains an enum value with the specified name;
73+
/// otherwise, <c>false</c>.
74+
/// </returns>
75+
public bool ContainsName(string name)
76+
=> _map.ContainsKey(name);
77+
78+
public IEnumerable<FusionEnumValue> AsEnumerable()
79+
=> _values;
80+
81+
public IEnumerator<FusionEnumValue> GetEnumerator()
82+
=> Unsafe.As<IEnumerable<FusionEnumValue>>(_values).GetEnumerator();
83+
84+
IEnumerator<IEnumValue> IEnumerable<IEnumValue>.GetEnumerator()
85+
=> GetEnumerator();
86+
87+
IEnumerator IEnumerable.GetEnumerator()
88+
=> GetEnumerator();
89+
90+
public static FusionEnumValueCollection Empty { get; } = new([]);
91+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using HotChocolate.Features;
2+
using HotChocolate.Language;
3+
using HotChocolate.Types;
4+
5+
namespace HotChocolate.Fusion.Types.Completion;
6+
7+
internal sealed class AggregateCompositeTypeInterceptor : CompositeTypeInterceptor
8+
{
9+
private readonly CompositeTypeInterceptor[] _interceptors;
10+
11+
public AggregateCompositeTypeInterceptor(CompositeTypeInterceptor[] interceptors)
12+
{
13+
ArgumentNullException.ThrowIfNull(interceptors);
14+
_interceptors = interceptors;
15+
}
16+
17+
public override void OnCompleteSchema(
18+
ICompositeSchemaBuilderContext context,
19+
ref IFeatureCollection features)
20+
{
21+
foreach (var interceptor in _interceptors)
22+
{
23+
interceptor.OnCompleteSchema(context, ref features);
24+
}
25+
}
26+
27+
public override void OnCompleteOutputField(
28+
ICompositeSchemaBuilderContext context,
29+
IComplexTypeDefinition type,
30+
IOutputFieldDefinition field,
31+
OperationType? operationType,
32+
ref IFeatureCollection features)
33+
{
34+
foreach (var interceptor in _interceptors)
35+
{
36+
interceptor.OnCompleteOutputField(context, type, field, operationType, ref features);
37+
}
38+
}
39+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static class CompletionTools
1212
{
1313
public static FusionDirectiveCollection CreateDirectiveCollection(
1414
IReadOnlyList<DirectiveNode> directives,
15-
CompositeSchemaContext context)
15+
CompositeSchemaBuilderContext context)
1616
{
1717
directives = DirectiveTools.GetUserDirectives(directives);
1818

@@ -60,7 +60,7 @@ private static ArgumentAssignment CreateArgumentAssignment(
6060

6161
public static FusionInterfaceTypeDefinitionCollection CreateInterfaceTypeCollection(
6262
IReadOnlyList<NamedTypeNode> interfaceTypes,
63-
CompositeSchemaContext context)
63+
CompositeSchemaBuilderContext context)
6464
{
6565
if (interfaceTypes.Count == 0)
6666
{
@@ -79,7 +79,7 @@ public static FusionInterfaceTypeDefinitionCollection CreateInterfaceTypeCollect
7979

8080
public static FusionObjectTypeDefinitionCollection CreateObjectTypeCollection(
8181
IReadOnlyList<NamedTypeNode> types,
82-
CompositeSchemaContext context)
82+
CompositeSchemaBuilderContext context)
8383
{
8484
var temp = new FusionObjectTypeDefinition[types.Count];
8585

@@ -93,7 +93,7 @@ public static FusionObjectTypeDefinitionCollection CreateObjectTypeCollection(
9393

9494
public static SourceObjectTypeCollection CreateSourceObjectTypeCollection(
9595
ObjectTypeDefinitionNode typeDef,
96-
CompositeSchemaContext context)
96+
CompositeSchemaBuilderContext context)
9797
{
9898
var types = TypeDirectiveParser.Parse(typeDef.Directives);
9999
var lookups = LookupDirectiveParser.Parse(typeDef.Directives);
@@ -113,7 +113,7 @@ public static SourceObjectTypeCollection CreateSourceObjectTypeCollection(
113113

114114
public static SourceInterfaceTypeCollection CreateSourceInterfaceTypeCollection(
115115
InterfaceTypeDefinitionNode typeDef,
116-
CompositeSchemaContext context)
116+
CompositeSchemaBuilderContext context)
117117
{
118118
var types = TypeDirectiveParser.Parse(typeDef.Directives);
119119
var lookups = LookupDirectiveParser.Parse(typeDef.Directives);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using HotChocolate.Features;
2+
using HotChocolate.Fusion.Types.Collections;
3+
4+
namespace HotChocolate.Fusion.Types.Completion;
5+
6+
internal readonly ref struct CompositeEnumTypeCompletionContext(
7+
FusionDirectiveCollection directives,
8+
IFeatureCollection features)
9+
{
10+
public FusionDirectiveCollection Directives { get; } = directives;
11+
12+
public IFeatureCollection Features { get; } = features;
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using HotChocolate.Features;
2+
using HotChocolate.Fusion.Types.Collections;
3+
using HotChocolate.Types;
4+
5+
namespace HotChocolate.Fusion.Types.Completion;
6+
7+
internal readonly ref struct CompositeEnumValueCompletionContext(
8+
IEnumTypeDefinition declaringType,
9+
FusionDirectiveCollection directives,
10+
IFeatureCollection features)
11+
{
12+
public IEnumTypeDefinition DeclaringType { get; } = declaringType;
13+
14+
public FusionDirectiveCollection Directives { get; } = directives;
15+
16+
public IFeatureCollection Features { get; } = features;
17+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ namespace HotChocolate.Fusion.Types.Completion;
44

55
internal readonly ref struct CompositeScalarTypeCompletionContext(
66
ScalarValueKind valueKind,
7-
FusionDirectiveCollection directives)
7+
FusionDirectiveCollection directives,
8+
Uri? specifiedBy)
89
{
910
public ScalarValueKind ValueKind { get; } = valueKind;
1011

1112
public FusionDirectiveCollection Directives { get; } = directives;
13+
14+
public Uri? SpecifiedBy { get; } = specifiedBy;
1215
}

0 commit comments

Comments
 (0)