Skip to content

Commit 54ae298

Browse files
Add option to disable null-bubbling
1 parent 69d7955 commit 54ae298

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,22 @@ public static class WellKnownContextData
274274
/// The key to access the authorization allowed flag on the member context.
275275
/// </summary>
276276
public const string AllowAnonymous = "HotChocolate.Authorization.AllowAnonymous";
277-
277+
278278
/// <summary>
279279
/// The key to access the true nullability flag on the execution context.
280280
/// </summary>
281281
public const string EnableTrueNullability = "HotChocolate.Types.EnableTrueNullability";
282-
282+
283+
/// <summary>
284+
/// The key to access the disable null-bubbling flag on the execution context.
285+
/// </summary>
286+
public const string DisableNullBubbling = "HotChocolate.Types.DisableNullBubbling";
287+
283288
/// <summary>
284289
/// The key to access the tag options object.
285290
/// </summary>
286291
public const string TagOptions = "HotChocolate.Types.TagOptions";
287-
292+
288293
/// <summary>
289294
/// Type key to access the internal schema options.
290295
/// </summary>

src/HotChocolate/Core/src/Execution/Pipeline/OperationResolverMiddleware.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ private IOperation CompileOperation(
109109

110110
private bool IsNullBubblingEnabled(IRequestContext context, OperationDefinitionNode operationDefinition)
111111
{
112+
if (context.Schema.ContextData.ContainsKey(DisableNullBubbling))
113+
{
114+
return false;
115+
}
116+
112117
if (!context.Schema.ContextData.ContainsKey(EnableTrueNullability) ||
113118
operationDefinition.Directives.Count == 0)
114119
{

src/HotChocolate/Core/src/Types/IReadOnlySchemaOptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface IReadOnlySchemaOptions
5555
/// unreachable from the root types.
5656
/// </summary>
5757
bool RemoveUnreachableTypes { get; }
58-
58+
5959
/// <summary>
6060
/// Defines if unused type system directives shall
6161
/// be removed from the schema.
@@ -97,7 +97,7 @@ public interface IReadOnlySchemaOptions
9797
/// Defines if the order of important middleware components shall be validated.
9898
/// </summary>
9999
bool ValidatePipelineOrder { get; }
100-
100+
101101
/// <summary>
102102
/// Defines if the runtime types of types shall be validated.
103103
/// </summary>
@@ -181,6 +181,11 @@ public interface IReadOnlySchemaOptions
181181
/// </summary>
182182
bool EnableTrueNullability { get; }
183183

184+
/// <summary>
185+
/// Specifies whether null-bubbling shall be disabled.
186+
/// </summary>
187+
bool DisableNullBubbling { get; }
188+
184189
/// <summary>
185190
/// Specifies that the @tag directive shall be registered with the type system.
186191
/// </summary>

src/HotChocolate/Core/src/Types/SchemaBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public partial class SchemaBuilder : ISchemaBuilder
3939
typeof(CostTypeInterceptor),
4040
typeof(MiddlewareValidationTypeInterceptor),
4141
typeof(EnableTrueNullabilityTypeInterceptor),
42+
typeof(DisableNullBubblingTypeInterceptor),
4243
];
4344

4445
private SchemaOptions _options = new();
@@ -263,7 +264,7 @@ public ISchemaBuilder AddType(INamedTypeExtension typeExtension)
263264
_types.Add(_ => TypeReference.Create(typeExtension));
264265
return this;
265266
}
266-
267+
267268
internal void AddTypeReference(TypeReference typeReference)
268269
{
269270
if (typeReference is null)

src/HotChocolate/Core/src/Types/SchemaOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public FieldBindingFlags DefaultFieldBindingFlags
211211
/// </summary>
212212
public bool EnableTrueNullability { get; set; }
213213

214+
/// <summary>
215+
/// Specifies whether null-bubbling shall be disabled.
216+
/// </summary>
217+
public bool DisableNullBubbling { get; set; }
218+
214219
/// <summary>
215220
/// Specifies that the @tag directive shall be registered with the type system.
216221
/// </summary>
@@ -263,6 +268,7 @@ public static SchemaOptions FromOptions(IReadOnlySchemaOptions options)
263268
MaxAllowedNodeBatchSize = options.MaxAllowedNodeBatchSize,
264269
StripLeadingIFromInterface = options.StripLeadingIFromInterface,
265270
EnableTrueNullability = options.EnableTrueNullability,
271+
DisableNullBubbling = options.DisableNullBubbling,
266272
EnableTag = options.EnableTag,
267273
DefaultQueryDependencyInjectionScope = options.DefaultQueryDependencyInjectionScope,
268274
DefaultMutationDependencyInjectionScope = options.DefaultMutationDependencyInjectionScope,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#nullable enable
2+
using HotChocolate.Configuration;
3+
using HotChocolate.Types.Descriptors;
4+
using HotChocolate.Types.Descriptors.Definitions;
5+
6+
namespace HotChocolate.Types.Interceptors;
7+
8+
internal sealed class DisableNullBubblingTypeInterceptor : TypeInterceptor
9+
{
10+
internal override bool IsEnabled(IDescriptorContext context)
11+
=> context.Options.DisableNullBubbling;
12+
13+
public override void OnAfterInitialize(ITypeDiscoveryContext discoveryContext, DefinitionBase definition)
14+
{
15+
if (definition is SchemaTypeDefinition schemaDef)
16+
{
17+
schemaDef.ContextData[WellKnownContextData.DisableNullBubbling] = true;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)