Skip to content

Commit 69e564d

Browse files
Add AllowDisablingNullBubbling option
1 parent def180c commit 69e564d

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/HotChocolate/Core/src/Execution/Options/IRequestExecutorOptionsAccessor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ namespace HotChocolate.Execution.Options;
88
/// </summary>
99
public interface IRequestExecutorOptionsAccessor
1010
: IErrorHandlerOptionsAccessor
11-
, IRequestTimeoutOptionsAccessor
12-
, IComplexityAnalyzerOptionsAccessor
13-
, IPersistedQueryOptionsAccessor;
11+
, IRequestTimeoutOptionsAccessor
12+
, IComplexityAnalyzerOptionsAccessor
13+
, IPersistedQueryOptionsAccessor
14+
{
15+
/// <summary>
16+
/// Determine whether null-bubbling can be disabled on a per-request basis.
17+
/// </summary>
18+
bool AllowDisablingNullBubbling { get; }
19+
}

src/HotChocolate/Core/src/Execution/Options/RequestExecutorOptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ public IError OnlyPersistedQueriesAreAllowedError
7272
get => _onlyPersistedQueriesAreAllowedError;
7373
set
7474
{
75-
_onlyPersistedQueriesAreAllowedError = value
76-
?? throw new ArgumentNullException(
75+
_onlyPersistedQueriesAreAllowedError = value ??
76+
throw new ArgumentNullException(
7777
nameof(OnlyPersistedQueriesAreAllowedError));
7878
}
7979
}
80+
81+
/// <summary>
82+
/// Determine whether null-bubbling can be disabled on a per-request basis.
83+
/// </summary>
84+
public bool AllowDisablingNullBubbling { get; set; } = false;
8085
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using HotChocolate.Execution.Options;
56
using HotChocolate.Execution.Processing;
67
using HotChocolate.Language;
78
using HotChocolate.Types;
@@ -19,13 +20,15 @@ internal sealed class OperationResolverMiddleware
1920
{
2021
private readonly RequestDelegate _next;
2122
private readonly ObjectPool<OperationCompiler> _operationCompilerPool;
23+
private readonly IRequestExecutorOptionsAccessor _options;
2224
private readonly VariableCoercionHelper _coercionHelper;
2325
private readonly IReadOnlyList<IOperationCompilerOptimizer>? _optimizers;
2426

2527
private OperationResolverMiddleware(
2628
RequestDelegate next,
2729
ObjectPool<OperationCompiler> operationCompilerPool,
2830
IEnumerable<IOperationCompilerOptimizer> optimizers,
31+
IRequestExecutorOptionsAccessor options,
2932
VariableCoercionHelper coercionHelper)
3033
{
3134
if (optimizers is null)
@@ -37,6 +40,8 @@ private OperationResolverMiddleware(
3740
throw new ArgumentNullException(nameof(next));
3841
_operationCompilerPool = operationCompilerPool ??
3942
throw new ArgumentNullException(nameof(operationCompilerPool));
43+
_options = options ??
44+
throw new ArgumentNullException(nameof(options));
4045
_coercionHelper = coercionHelper ??
4146
throw new ArgumentNullException(nameof(coercionHelper));
4247
_optimizers = optimizers.ToArray();
@@ -109,8 +114,7 @@ private IOperation CompileOperation(
109114

110115
private bool IsNullBubblingEnabled(IRequestContext context, OperationDefinitionNode operationDefinition)
111116
{
112-
if (context.ContextData.TryGetValue(DisableNullBubbling, out var disableNullBubbling)
113-
&& disableNullBubbling is true)
117+
if (_options.AllowDisablingNullBubbling && context.ContextData.ContainsKey(DisableNullBubbling))
114118
{
115119
return false;
116120
}
@@ -175,12 +179,14 @@ public static RequestCoreMiddleware Create()
175179
var operationCompilerPool = core.Services.GetRequiredService<ObjectPool<OperationCompiler>>();
176180
var optimizers1 = core.Services.GetRequiredService<IEnumerable<IOperationCompilerOptimizer>>();
177181
var optimizers2 = core.SchemaServices.GetRequiredService<IEnumerable<IOperationCompilerOptimizer>>();
182+
var options = core.SchemaServices.GetRequiredService<IRequestExecutorOptionsAccessor>();
178183
var coercionHelper = core.Services.GetRequiredService<VariableCoercionHelper>();
179184
var middleware = new OperationResolverMiddleware(
180185
next,
181186
operationCompilerPool,
182187
optimizers1.Concat(optimizers2),
188+
options,
183189
coercionHelper);
184190
return context => middleware.InvokeAsync(context);
185191
};
186-
}
192+
}

src/HotChocolate/Core/test/Execution.Tests/TrueNullabilityTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public async Task Error_Query_With_NullBubbling_Disabled()
148148
var response =
149149
await new ServiceCollection()
150150
.AddGraphQLServer()
151+
.ModifyRequestOptions(options => options.AllowDisablingNullBubbling = true)
151152
.AddQueryType<Query>()
152153
.ExecuteRequestAsync(request);
153154

0 commit comments

Comments
 (0)