Skip to content

Commit

Permalink
Add AllowDisablingNullBubbling option
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Mar 22, 2024
1 parent def180c commit 69e564d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace HotChocolate.Execution.Options;
/// </summary>
public interface IRequestExecutorOptionsAccessor
: IErrorHandlerOptionsAccessor
, IRequestTimeoutOptionsAccessor
, IComplexityAnalyzerOptionsAccessor
, IPersistedQueryOptionsAccessor;
, IRequestTimeoutOptionsAccessor
, IComplexityAnalyzerOptionsAccessor
, IPersistedQueryOptionsAccessor
{
/// <summary>
/// Determine whether null-bubbling can be disabled on a per-request basis.
/// </summary>
bool AllowDisablingNullBubbling { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ public IError OnlyPersistedQueriesAreAllowedError
get => _onlyPersistedQueriesAreAllowedError;
set
{
_onlyPersistedQueriesAreAllowedError = value
?? throw new ArgumentNullException(
_onlyPersistedQueriesAreAllowedError = value ??
throw new ArgumentNullException(
nameof(OnlyPersistedQueriesAreAllowedError));
}
}

/// <summary>
/// Determine whether null-bubbling can be disabled on a per-request basis.
/// </summary>
public bool AllowDisablingNullBubbling { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate.Execution.Options;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using HotChocolate.Types;
Expand All @@ -19,13 +20,15 @@ internal sealed class OperationResolverMiddleware
{
private readonly RequestDelegate _next;
private readonly ObjectPool<OperationCompiler> _operationCompilerPool;
private readonly IRequestExecutorOptionsAccessor _options;
private readonly VariableCoercionHelper _coercionHelper;
private readonly IReadOnlyList<IOperationCompilerOptimizer>? _optimizers;

private OperationResolverMiddleware(
RequestDelegate next,
ObjectPool<OperationCompiler> operationCompilerPool,
IEnumerable<IOperationCompilerOptimizer> optimizers,
IRequestExecutorOptionsAccessor options,
VariableCoercionHelper coercionHelper)
{
if (optimizers is null)
Expand All @@ -37,6 +40,8 @@ private OperationResolverMiddleware(
throw new ArgumentNullException(nameof(next));
_operationCompilerPool = operationCompilerPool ??
throw new ArgumentNullException(nameof(operationCompilerPool));
_options = options ??
throw new ArgumentNullException(nameof(options));
_coercionHelper = coercionHelper ??
throw new ArgumentNullException(nameof(coercionHelper));
_optimizers = optimizers.ToArray();
Expand Down Expand Up @@ -109,8 +114,7 @@ private IOperation CompileOperation(

private bool IsNullBubblingEnabled(IRequestContext context, OperationDefinitionNode operationDefinition)
{
if (context.ContextData.TryGetValue(DisableNullBubbling, out var disableNullBubbling)
&& disableNullBubbling is true)
if (_options.AllowDisablingNullBubbling && context.ContextData.ContainsKey(DisableNullBubbling))
{

Check warning on line 118 in src/HotChocolate/Core/src/Execution/Pipeline/OperationResolverMiddleware.cs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L118 was not covered by tests
return false;
}
Expand Down Expand Up @@ -175,12 +179,14 @@ public static RequestCoreMiddleware Create()
var operationCompilerPool = core.Services.GetRequiredService<ObjectPool<OperationCompiler>>();
var optimizers1 = core.Services.GetRequiredService<IEnumerable<IOperationCompilerOptimizer>>();
var optimizers2 = core.SchemaServices.GetRequiredService<IEnumerable<IOperationCompilerOptimizer>>();
var options = core.SchemaServices.GetRequiredService<IRequestExecutorOptionsAccessor>();
var coercionHelper = core.Services.GetRequiredService<VariableCoercionHelper>();
var middleware = new OperationResolverMiddleware(
next,
operationCompilerPool,
optimizers1.Concat(optimizers2),
options,
coercionHelper);
return context => middleware.InvokeAsync(context);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public async Task Error_Query_With_NullBubbling_Disabled()
var response =
await new ServiceCollection()
.AddGraphQLServer()
.ModifyRequestOptions(options => options.AllowDisablingNullBubbling = true)
.AddQueryType<Query>()
.ExecuteRequestAsync(request);

Expand Down

0 comments on commit 69e564d

Please sign in to comment.