Skip to content

Commit 5d0b836

Browse files
committed
Added integration of root types
1 parent 7728ab3 commit 5d0b836

30 files changed

+792
-160
lines changed

src/HotChocolate/Core/src/Execution/Configuration/ConfigurationContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using HotChocolate.Types.Descriptors;
2+
13
namespace HotChocolate.Execution.Configuration;
24

35
/// <summary>
46
/// The configuration context is used during the setup of the schema and request executor.
57
/// </summary>
68
public sealed class ConfigurationContext : IHasContextData
79
{
10+
private IDescriptorContext? _descriptorContext;
11+
812
/// <summary>
913
/// Initializes a new instance of <see cref="ConfigurationContext"/>.
1014
/// </summary>
@@ -52,4 +56,20 @@ public ConfigurationContext(
5256
/// Gets the configuration context data which can be used by hooks to store arbitrary state.
5357
/// </summary>
5458
public IDictionary<string, object?> ContextData { get; } = new Dictionary<string, object?>();
59+
60+
/// <summary>
61+
/// Gets the descriptor context.
62+
/// </summary>
63+
public IDescriptorContext DescriptorContext
64+
{
65+
get
66+
{
67+
if(_descriptorContext is null)
68+
{
69+
_descriptorContext = SchemaBuilder.CreateContext();
70+
}
71+
72+
return _descriptorContext;
73+
}
74+
}
5575
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using HotChocolate.Execution;
33
using HotChocolate.Execution.Configuration;
44
using HotChocolate.Execution.Options;
5+
using HotChocolate.Types.Descriptors;
56
using Microsoft.Extensions.Options;
67

78
// ReSharper disable once CheckNamespace
@@ -118,6 +119,30 @@ public static IRequestExecutorBuilder ConfigureSchema(
118119
(ctx, sp) => configureSchema(sp, ctx.SchemaBuilder))));
119120
}
120121

122+
/// <summary>
123+
/// Adds a delegate that will be used to configure the descriptor context.
124+
/// </summary>
125+
public static IRequestExecutorBuilder ConfigureDescriptorContext(
126+
this IRequestExecutorBuilder builder,
127+
Action<IDescriptorContext> configure)
128+
{
129+
if (builder is null)
130+
{
131+
throw new ArgumentNullException(nameof(builder));
132+
}
133+
134+
if (configure is null)
135+
{
136+
throw new ArgumentNullException(nameof(configure));
137+
}
138+
139+
return Configure(
140+
builder,
141+
options => options.OnConfigureSchemaBuilderHooks.Add(
142+
new OnConfigureSchemaBuilderAction(
143+
(ctx, _) => configure(ctx.DescriptorContext))));
144+
}
145+
121146
/// <summary>
122147
/// Adds a delegate that will be used to configure a named <see cref="ISchema"/>.
123148
/// </summary>

src/HotChocolate/Core/src/Execution/RequestExecutorResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private static async ValueTask<ISchema> CreateSchemaAsync(
377377
.AddServices(schemaServices)
378378
.SetContextData(typeof(RequestExecutorOptions).FullName!, executorOptions);
379379

380-
var descriptorContext = context.SchemaBuilder.CreateContext();
380+
var descriptorContext = context.DescriptorContext;
381381

382382
await foreach (var member in
383383
typeModuleChangeMonitor.CreateTypesAsync(descriptorContext)

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/IOutputTypeFileBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ public interface IOutputTypeFileBuilder
77
void WriteHeader();
88

99
void WriteBeginNamespace();
10+
1011
void WriteEndNamespace();
1112

1213
string WriteBeginClass(string typeName);
14+
1315
void WriteEndClass();
1416

1517
void WriteInitializeMethod(IOutputTypeInfo typeInfo);

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/InterfaceTypeExtensionFileBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void WriteInitializeMethod(IOutputTypeInfo typeInfo)
4747
{
4848
_writer.WriteIndentedLine(
4949
"internal static void Initialize(global::HotChocolate.Types.IInterfaceTypeDescriptor<{0}> descriptor)",
50-
typeInfo.RuntimeType.ToFullyQualified());
50+
typeInfo.RuntimeType!.ToFullyQualified());
5151
_writer.WriteIndentedLine("{");
5252

5353
using (_writer.IncreaseIndent())
@@ -127,6 +127,6 @@ public void WriteConfigureMethod(IOutputTypeInfo typeInfo)
127127
{
128128
_writer.WriteIndentedLine(
129129
"static partial void Configure(global::HotChocolate.Types.IInterfaceTypeDescriptor<{0}> descriptor);",
130-
typeInfo.RuntimeType.ToFullyQualified());
130+
typeInfo.RuntimeType!.ToFullyQualified());
131131
}
132132
}

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/ModuleFileBuilder.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text;
22
using HotChocolate.Types.Analyzers.Helpers;
33
using HotChocolate.Types.Analyzers.Models;
4+
using Microsoft.CodeAnalysis;
45
using Microsoft.CodeAnalysis.Text;
56

67
namespace HotChocolate.Types.Analyzers.FileBuilders;
@@ -85,23 +86,27 @@ public void WriteEnsureObjectTypeExtensionIsRegistered(string runtimeTypeName)
8586
public void WriteEnsureInterfaceTypeExtensionIsRegistered(string runtimeTypeName)
8687
=> _writer.WriteIndentedLine("builder.AddType<InterfaceType<{0}>>();", runtimeTypeName);
8788

88-
public void WriteRegisterObjectTypeExtension(string runtimeTypeName, string extensionType)
89+
public void WriteRegisterTypeExtension(string key, string runtimeTypeName, string extensionType)
8990
{
90-
_writer.WriteIndentedLine("global::{0}.TryAdd<{1}>(", WellKnownTypes.TypeConfiguration, runtimeTypeName);
91+
_writer.WriteIndentedLine(
92+
"builder.ConfigureDescriptorContext(ctx => ctx.TypeConfiguration.TryAdd<{0}>(",
93+
runtimeTypeName);
9194
using (_writer.IncreaseIndent())
9295
{
93-
_writer.WriteIndentedLine("\"{0}\",", extensionType);
94-
_writer.WriteIndentedLine("() => {0}.Initialize);", extensionType);
96+
_writer.WriteIndentedLine("\"{0}\",", key);
97+
_writer.WriteIndentedLine("() => {0}.Initialize));", extensionType);
9598
}
9699
}
97100

98-
public void WriteRegisterInterfaceTypeExtension(string runtimeTypeName, string extensionType)
101+
public void WriteRegisterRootTypeExtension(string key, OperationType operation, string extensionType)
99102
{
100-
_writer.WriteIndentedLine("global::{0}.TryAdd<{1}>(", WellKnownTypes.TypeConfiguration, runtimeTypeName);
103+
_writer.WriteIndentedLine("builder.ConfigureDescriptorContext(ctx => ctx.TypeConfiguration.TryAdd(");
104+
101105
using (_writer.IncreaseIndent())
102106
{
103-
_writer.WriteIndentedLine("\"{0}\",", extensionType);
104-
_writer.WriteIndentedLine("() => {0}.Initialize);", extensionType);
107+
_writer.WriteIndentedLine("\"{0}\",", key);
108+
_writer.WriteIndentedLine("global::HotChocolate.Types.OperationType.{0},", operation);
109+
_writer.WriteIndentedLine("() => {0}.Initialize));", extensionType);
105110
}
106111
}
107112

0 commit comments

Comments
 (0)