Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce RequestContextAccessor #6454

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "8.0.100-preview.6.23330.14"
"version": "8.0.100-preview.7.23376.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading;

namespace HotChocolate.Execution;

internal sealed class DefaultRequestContextAccessor : IRequestContextAccessor
{
private static readonly AsyncLocal<RequestContextHolder> _requestContextCurrent = new();

public IRequestContext RequestContext
{
get
{
return _requestContextCurrent.Value?.Context ??
throw new InvalidCastException("Can only be accessed in a request context.");
}
set
{
RequestContextHolder? holder = _requestContextCurrent.Value;

if (holder is null)
{
holder = new RequestContextHolder();
_requestContextCurrent.Value = holder;
}

holder.Context = value;
}
}

private class RequestContextHolder
{
public IRequestContext Context = default!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static IServiceCollection AddGraphQLCore(this IServiceCollection services
services.AddOptions();

services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAddSingleton<DefaultRequestContextAccessor>();
services.TryAddSingleton<IRequestContextAccessor>(sp => sp.GetRequiredService<DefaultRequestContextAccessor>());

services.TryAddSingleton<ObjectPool<StringBuilder>>(sp =>
{
Expand Down
16 changes: 16 additions & 0 deletions src/HotChocolate/Core/src/Execution/IRequestContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace HotChocolate.Execution;

/// <summary>
/// The <see cref="IRequestContextAccessor"/> allows access to the
/// <see cref="IRequestContext"/> during request execution.
///
/// Be aware that the <see cref="IRequestContext"/> is not thread-safe and should
/// not be mutated within resolvers.
/// </summary>
public interface IRequestContextAccessor
{
/// <summary>
/// Gets the <see cref="IRequestContext"/>.
/// </summary>
IRequestContext RequestContext { get; }
}
6 changes: 6 additions & 0 deletions src/HotChocolate/Core/src/Execution/RequestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal sealed class RequestExecutor : IRequestExecutor
private readonly RequestDelegate _requestDelegate;
private readonly BatchExecutor _batchExecutor;
private readonly ObjectPool<RequestContext> _contextPool;
private readonly DefaultRequestContextAccessor _contextAccessor;
private readonly IRequestContextEnricher[] _enricher;

public RequestExecutor(
Expand All @@ -26,6 +27,7 @@ public RequestExecutor(
RequestDelegate requestDelegate,
BatchExecutor batchExecutor,
ObjectPool<RequestContext> contextPool,
DefaultRequestContextAccessor contextAccessor,
ulong version)
{
Schema = schema ??
Expand All @@ -40,6 +42,8 @@ public RequestExecutor(
throw new ArgumentNullException(nameof(batchExecutor));
_contextPool = contextPool ??
throw new ArgumentNullException(nameof(contextPool));
_contextAccessor = contextAccessor ??
throw new ArgumentNullException(nameof(contextAccessor));
Version = version;

var list = new List<IRequestContextEnricher>();
Expand Down Expand Up @@ -88,6 +92,8 @@ public async Task<IExecutionResult> ExecuteAsync(
context.RequestAborted = cancellationToken;
context.Initialize(request, services);
EnrichContext(context);

_contextAccessor.RequestContext = context;

await _requestDelegate(context).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ await typeModuleChangeMonitor.ConfigureAsync(context, cancellationToken)
sp.GetRequiredService<RequestDelegate>(),
sp.GetRequiredService<BatchExecutor>(),
sp.GetRequiredService<ObjectPool<RequestContext>>(),
sp.GetApplicationService<DefaultRequestContextAccessor>(),
version));

OnConfigureSchemaServices(context, serviceCollection, setup);
Expand Down