Skip to content

Commit

Permalink
Remove Pure Resolver Context (#7186)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jun 21, 2024
1 parent 336ffcd commit 0423ade
Show file tree
Hide file tree
Showing 71 changed files with 553 additions and 558 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static ObjectFieldDefinition CreateServiceField(IDescriptorContext cont
descriptor.Type<NonNullType<ObjectType<_Service>>>().Resolve(_service);
descriptor.Definition.PureResolver = Resolve;

static _Service Resolve(IPureResolverContext ctx)
static _Service Resolve(IResolverContext ctx)
=> _service;

return descriptor.CreateDefinition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate.AspNetCore;

internal static class GlobalStateHelpers
{
public static HttpContext GetHttpContext(IPureResolverContext context)
public static HttpContext GetHttpContext(IResolverContext context)
{
if (context.ContextData.TryGetValue(nameof(HttpContext), out var value) &&
value is HttpContext httpContext)
Expand All @@ -16,9 +16,9 @@ public static HttpContext GetHttpContext(IPureResolverContext context)
throw new MissingStateException("Resolver", nameof(HttpContext), StateKind.Global);
}

public static HttpRequest GetHttpRequest(IPureResolverContext context)
public static HttpRequest GetHttpRequest(IResolverContext context)
=> GetHttpContext(context).Request;

public static HttpResponse GetHttpResponse(IPureResolverContext context)
public static HttpResponse GetHttpResponse(IResolverContext context)
=> GetHttpContext(context).Response;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System.Reflection;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
using HotChocolate.Resolvers.Expressions.Parameters;
using Microsoft.AspNetCore.Http;

namespace HotChocolate.AspNetCore.ParameterExpressionBuilders;

internal sealed class HttpContextParameterExpressionBuilder
: LambdaParameterExpressionBuilder<IPureResolverContext, HttpContext>
internal sealed class HttpContextParameterExpressionBuilder()
: LambdaParameterExpressionBuilder<HttpContext>(ctx => GlobalStateHelpers.GetHttpContext(ctx), isPure: true)
{
public HttpContextParameterExpressionBuilder()
: base(ctx => GlobalStateHelpers.GetHttpContext(ctx)) { }

public override ArgumentKind Kind => ArgumentKind.GlobalState;

public override bool CanHandle(ParameterInfo parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

namespace HotChocolate.AspNetCore.ParameterExpressionBuilders;

internal sealed class HttpRequestParameterExpressionBuilder
: LambdaParameterExpressionBuilder<IPureResolverContext, HttpRequest>
internal sealed class HttpRequestParameterExpressionBuilder()
: LambdaParameterExpressionBuilder<HttpRequest>(ctx => GlobalStateHelpers.GetHttpRequest(ctx), isPure: true)
{
public HttpRequestParameterExpressionBuilder()
: base(ctx => GlobalStateHelpers.GetHttpRequest(ctx)) { }

public override ArgumentKind Kind => ArgumentKind.GlobalState;

public override bool CanHandle(ParameterInfo parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

namespace HotChocolate.AspNetCore.ParameterExpressionBuilders;

internal sealed class HttpResponseParameterExpressionBuilder
: LambdaParameterExpressionBuilder<IPureResolverContext, HttpResponse>
internal sealed class HttpResponseParameterExpressionBuilder()
: LambdaParameterExpressionBuilder<HttpResponse>(ctx => GlobalStateHelpers.GetHttpResponse(ctx), isPure: true)
{
public HttpResponseParameterExpressionBuilder()
: base(ctx => GlobalStateHelpers.GetHttpResponse(ctx)) { }

public override ArgumentKind Kind => ArgumentKind.GlobalState;

public override bool CanHandle(ParameterInfo parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public IServiceProvider Services

public IVariableValueCollection Variables => _operationContext.Variables;

IReadOnlyDictionary<string, object?> IPureResolverContext.ScopedContextData
=> ScopedContextData;

public CancellationToken RequestAborted { get; private set; }

public bool HasCleanupTasks => _cleanupTasks.Count > 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using HotChocolate.Execution.Internal;
using HotChocolate.Language;
using HotChocolate.Resolvers;
Expand All @@ -12,21 +14,15 @@ namespace HotChocolate.Execution.Processing;

internal partial class MiddlewareContext
{
private sealed class PureResolverContext : IPureResolverContext
private sealed class PureResolverContext(MiddlewareContext parentContext) : IResolverContext
{
private readonly MiddlewareContext _parentContext;
private ITypeConverter? _typeConverter;
private IReadOnlyDictionary<string, ArgumentValue> _argumentValues = default!;
private ISelection _selection = default!;
private ObjectType _parentType = default!;
private ObjectResult _parentResult = default!;
private object? _parent;

public PureResolverContext(MiddlewareContext parentContext)
{
_parentContext = parentContext;
}

public bool Initialize(
ISelection selection,
ObjectType parentType,
Expand All @@ -44,9 +40,7 @@ public bool Initialize(
return true;
}

if (selection.Arguments.TryCoerceArguments(
_parentContext,
out var coercedArgs))
if (selection.Arguments.TryCoerceArguments(parentContext, out var coercedArgs))
{
_argumentValues = coercedArgs;
return true;
Expand All @@ -64,23 +58,66 @@ public void Clear()
_argumentValues = default!;
}

public ISchema Schema => _parentContext.Schema;
public ISchema Schema => parentContext.Schema;

public IObjectType ObjectType => _parentType;

public IOperation Operation => _parentContext.Operation;
public IOperation Operation => parentContext.Operation;

public ISelection Selection => _selection;

public Path Path => PathHelper.CreatePathFromContext(_selection, _parentResult, -1);

public IReadOnlyDictionary<string, object?> ScopedContextData
=> _parentContext.ScopedContextData;
public CancellationToken RequestAborted => parentContext.RequestAborted;

public void ReportError(string errorMessage)
=> throw new NotSupportedException();

public void ReportError(IError error)
=> throw new NotSupportedException();

public void ReportError(Exception exception, Action<IErrorBuilder>? configure = null)
=> throw new NotSupportedException();

public IReadOnlyList<ISelection> GetSelections(
IObjectType typeContext,
ISelection? selection = null,
bool allowInternals = false)
=> throw new NotSupportedException();

public ISelectionCollection Select()
=> throw new NotSupportedException();

public ISelectionCollection Select(string fieldName)
=> throw new NotSupportedException();

public T GetQueryRoot<T>()
=> throw new NotSupportedException();

public IResolverContext Clone()
=> throw new NotSupportedException();

public string ResponseName => _selection.ResponseName;

public bool HasErrors
=> throw new NotSupportedException();

public IVariableValueCollection Variables => _parentContext.Variables;
public IImmutableDictionary<string, object?> ScopedContextData
{
get => parentContext.ScopedContextData;
set => throw new NotSupportedException();
}

public IImmutableDictionary<string, object?> LocalContextData
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}

public IVariableValueCollection Variables => parentContext.Variables;

public IDictionary<string, object?> ContextData
=> _parentContext.ContextData;
=> parentContext.ContextData;

public T Parent<T>()
=> _parent switch
Expand Down Expand Up @@ -162,13 +199,27 @@ public ValueKind ArgumentKind(string name)
return argument.Kind ?? ValueKind.Unknown;
}

public T Service<T>() where T: notnull => _parentContext.Service<T>();
public IServiceProvider Services
{
get => parentContext.Services;
set => throw new NotSupportedException();
}

public IServiceProvider RequestServices
{
get => parentContext.RequestServices;
}

public object Service(Type service)
=> parentContext.Service(service);

public T Service<T>() where T : notnull => parentContext.Service<T>();

#if NET8_0_OR_GREATER
public T? Service<T>(object key) where T : notnull => _parentContext.Service<T>(key);
public T? Service<T>(object key) where T : notnull => parentContext.Service<T>(key);
#endif

public T Resolver<T>() => _parentContext.Resolver<T>();
public T Resolver<T>() => parentContext.Resolver<T>();

private T CoerceArgumentValue<T>(ArgumentValue argument)
{
Expand All @@ -178,7 +229,7 @@ private T CoerceArgumentValue<T>(ArgumentValue argument)
// runtime version we can skip over parsing it.
if (!argument.IsFullyCoerced)
{
value = _parentContext._parser.ParseLiteral(
value = parentContext._parser.ParseLiteral(
argument.ValueLiteral!,
argument,
typeof(T));
Expand All @@ -191,8 +242,8 @@ private T CoerceArgumentValue<T>(ArgumentValue argument)
}

_typeConverter ??=
_parentContext.Services.GetService<ITypeConverter>() ??
DefaultTypeConverter.Default;
parentContext.Services.GetService<ITypeConverter>() ??
DefaultTypeConverter.Default;

if (value is T castedValue ||
_typeConverter.TryConvert(value, out castedValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public bool TryCreatePureContext(
ObjectType parentType,
ObjectResult parentResult,
object? parent,
[NotNullWhen(true)] out IPureResolverContext? context)
[NotNullWhen(true)] out IResolverContext? context)
{
if (_childContext.Initialize(selection, parentType, parentResult, parent))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
}
}

_writer.WriteLine();

_writer.WriteIndentedLine(
"var thisType = typeof({0});",
objectTypeExtension.Type.ToFullyQualified());
_writer.WriteIndentedLine(
"var bindingResolver = descriptor.Extend().Context.ParameterBindingResolver;");
_writer.WriteIndentedLine(
"global::{0}Resolvers.InitializeBindings(bindingResolver);",
objectTypeExtension.Type.ToDisplayString());
}

if (objectTypeExtension.NodeResolver is not null)
Expand All @@ -78,7 +85,7 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
{
_writer.WriteIndentedLine(".ImplementsNode()");
_writer.WriteIndentedLine(
".ResolveNode({0}Resolvers.{1}_{2});",
".ResolveNode({0}Resolvers.{1}_{2}().Resolver);",
objectTypeExtension.Type.ToDisplayString(),
objectTypeExtension.Type.Name,
objectTypeExtension.NodeResolver.Member.Name);
Expand All @@ -99,9 +106,7 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)
resolver.Member.Name);

_writer.WriteIndentedLine(
resolver.IsPure
? ".Extend().Definition.PureResolver = {0}Resolvers.{1}_{2};"
: ".Extend().Definition.Resolver = {0}Resolvers.{1}_{2};",
".Extend().Definition.Resolvers = {0}Resolvers.{1}_{2}();",
objectTypeExtension.Type.ToDisplayString(),
objectTypeExtension.Type.Name,
resolver.Member.Name);
Expand All @@ -111,28 +116,6 @@ public void WriteInitializeMethod(ObjectTypeExtensionInfo objectTypeExtension)

_writer.WriteLine();
_writer.WriteIndentedLine("Configure(descriptor);");

if (objectTypeExtension.Resolvers.Length > 0)
{
_writer.WriteLine();
_writer.WriteIndentedLine("descriptor.Extend().Context.OnSchemaCreated(");
using (_writer.IncreaseIndent())
{
_writer.WriteIndentedLine("schema =>");
_writer.WriteIndentedLine("{");
using (_writer.IncreaseIndent())
{
_writer.WriteIndentedLine("var services = schema.Services.GetApplicationServices();");
_writer.WriteIndentedLine(
"var bindingResolver = services.GetRequiredService<global::{0}>();",
WellKnownTypes.ParameterBindingResolver);
_writer.WriteIndentedLine(
"global::{0}Resolvers.InitializeBindings(bindingResolver);",
objectTypeExtension.Type.ToDisplayString());
}
_writer.WriteIndentedLine("});");
}
}
}

_writer.WriteIndentedLine("}");
Expand Down
Loading

0 comments on commit 0423ade

Please sign in to comment.