Skip to content

Commit

Permalink
Fix parallel access to session from GQL fields
Browse files Browse the repository at this point in the history
Fixes #17282
  • Loading branch information
AndreySurkov committed Jan 10, 2025
1 parent a2f45e5 commit 06f2e86
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@

namespace OrchardCore.Apis.GraphQL.Resolvers;

public class LockedAsyncFieldResolver<TReturnType> : FuncFieldResolver<TReturnType>
public class LockedAsyncFieldResolver<TReturnType> : IFieldResolver
{
public LockedAsyncFieldResolver(Func<IResolveFieldContext, ValueTask<TReturnType>> resolver) : base(resolver)
{
private readonly Func<IResolveFieldContext, ValueTask<TReturnType>> _resolver;

public LockedAsyncFieldResolver(Func<IResolveFieldContext, ValueTask<TReturnType>> resolver)
{
ArgumentNullException.ThrowIfNull(resolver);
_resolver = resolver;
}

public new async ValueTask<object> ResolveAsync(IResolveFieldContext context)
public async ValueTask<object> ResolveAsync(IResolveFieldContext context)
{
var graphContext = (GraphQLUserContext)context.UserContext;
await graphContext.ExecutionContextLock.WaitAsync();

try
{
return await base.ResolveAsync(context);
return await _resolver(context).ConfigureAwait(false);
}
finally
{
Expand All @@ -26,20 +29,24 @@ public LockedAsyncFieldResolver(Func<IResolveFieldContext, ValueTask<TReturnType
}
}

public class LockedAsyncFieldResolver<TSourceType, TReturnType> : FuncFieldResolver<TSourceType, TReturnType>
public class LockedAsyncFieldResolver<TSourceType, TReturnType> : IFieldResolver
{
public LockedAsyncFieldResolver(Func<IResolveFieldContext<TSourceType>, ValueTask<TReturnType>> resolver) : base(resolver)
private readonly Func<IResolveFieldContext<TSourceType>, ValueTask<TReturnType>> _resolver;

public LockedAsyncFieldResolver(Func<IResolveFieldContext<TSourceType>, ValueTask<TReturnType>> resolver)
{
ArgumentNullException.ThrowIfNull(resolver);
_resolver = resolver;
}

public new async Task<object> ResolveAsync(IResolveFieldContext context)
public async ValueTask<object> ResolveAsync(IResolveFieldContext context)
{
var graphContext = (GraphQLUserContext)context.UserContext;
await graphContext.ExecutionContextLock.WaitAsync();

try
{
return await base.ResolveAsync(context);
return await _resolver(context.As<TSourceType>()).ConfigureAwait(false);
}
finally
{
Expand Down

0 comments on commit 06f2e86

Please sign in to comment.