Skip to content

Commit

Permalink
Optimized Resolver Context Initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Nov 7, 2022
1 parent baaabd7 commit 159d34d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Expand Up @@ -6,6 +6,9 @@ namespace HotChocolate.Execution.Processing;

internal partial class MiddlewareContext
{
private static readonly ImmutableDictionary<string, object?> _emptyLocalContextData =
ImmutableDictionary<string, object?>.Empty;

public MiddlewareContext()
{
_childContext = new PureResolverContext(this);
Expand All @@ -26,10 +29,10 @@ public MiddlewareContext()
ParentResult = parentResult;
ResponseIndex = responseIndex;
_parent = parent;
_parser = _operationContext.Services.GetRequiredService<InputParser>();
_parser = operationContext.InputParser;
Path = path;
ScopedContextData = scopedContextData;
LocalContextData = ImmutableDictionary<string, object?>.Empty;
LocalContextData = _emptyLocalContextData;
Arguments = _selection.Arguments;
RequestAborted = _operationContext.RequestAborted;
}
Expand Down
Expand Up @@ -6,7 +6,9 @@
using HotChocolate.Execution.Instrumentation;
using HotChocolate.Execution.Processing.Tasks;
using HotChocolate.Fetching;
using HotChocolate.Types;
using HotChocolate.Utilities;
using Microsoft.Extensions.DependencyInjection;
using static HotChocolate.Execution.ThrowHelper;

namespace HotChocolate.Execution.Processing;
Expand All @@ -29,6 +31,7 @@ internal sealed partial class OperationContext
private IServiceProvider _services = default!;
private Func<object?> _resolveQueryRootValue = default!;
private IBatchDispatcher _batchDispatcher = default!;
private InputParser _inputParser = default!;
private object? _rootValue;
private bool _isInitialized;

Expand Down Expand Up @@ -66,6 +69,7 @@ internal sealed partial class OperationContext
_operation = operation;
_variables = variables;
_services = scopedServices;
_inputParser = scopedServices.GetRequiredService<InputParser>();
_rootValue = rootValue;
_resolveQueryRootValue = resolveQueryRootValue;
_batchDispatcher = batchDispatcher;
Expand All @@ -88,6 +92,7 @@ public void InitializeFrom(OperationContext context)
_operation = context._operation;
_variables = context._variables;
_services = context._services;
_inputParser = context._inputParser;
_rootValue = context._rootValue;
_resolveQueryRootValue = context._resolveQueryRootValue;
_batchDispatcher = context._batchDispatcher;
Expand Down
@@ -1,30 +1,25 @@
using System;
using HotChocolate.Types;

namespace HotChocolate.Execution.Processing;

// note: we are not asserting the properties here for performance reasons.
// These properties are used by the middleware context which means that potentially
// all fields will access them.
internal sealed partial class OperationContext
{
/// <summary>
/// Gets the request scoped services
/// </summary>
public IServiceProvider Services
{
get
{
AssertInitialized();
return _services;
}
}
public IServiceProvider Services => _services;

/// <summary>
/// Gets the activator helper class.
/// </summary>
public IActivator Activator
{
get
{
AssertInitialized();
return _activator;
}
}
public IActivator Activator => _activator;

/// <summary>
/// Gets access to the input parser.
/// </summary>
public InputParser InputParser => _inputParser;
}

0 comments on commit 159d34d

Please sign in to comment.