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

fix: IUnitOfWork injection #52

Merged
merged 3 commits into from
Oct 7, 2024
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
6 changes: 3 additions & 3 deletions src/Fluss.HotChocolate/AddExtensionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
await _next.Invoke(context);

if (!context.ContextData.TryGetValue(nameof(UnitOfWork), out var unitOfWork))
if (!context.ContextData.TryGetValue(nameof(IUnitOfWork), out var unitOfWork))
{
return;
}
Expand All @@ -38,7 +38,7 @@
{
if (context.Result is QueryResult subsequentQueryResult)
{
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(UnitOfWork),
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(IUnitOfWork),

Check warning on line 41 in src/Fluss.HotChocolate/AddExtensionMiddleware.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.HotChocolate/AddExtensionMiddleware.cs#L41

Added line #L41 was not covered by tests
unitOfWork).Create();
}

Expand Down Expand Up @@ -84,7 +84,7 @@

while (true)
{
if (contextData == null || !contextData.TryGetValue(nameof(UnitOfWork), out var value))
if (contextData == null || !contextData.TryGetValue(nameof(IUnitOfWork), out var value))
{
break;
}
Expand Down
27 changes: 14 additions & 13 deletions src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
private static readonly MethodInfo GetOrSetGlobalStateUnitOfWorkMethod =
typeof(ResolverContextExtensions).GetMethods()
.First(m => m.Name == nameof(ResolverContextExtensions.GetOrSetGlobalState))
.MakeGenericMethod(typeof(UnitOfWork));
.MakeGenericMethod(typeof(IUnitOfWork));

Check warning on line 15 in src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs#L15

Added line #L15 was not covered by tests

private static readonly MethodInfo GetGlobalStateOrDefaultLongMethod =
typeof(ResolverContextExtensions).GetMethods()
Expand All @@ -21,22 +21,22 @@

private static readonly MethodInfo ServiceUnitOfWorkMethod =
typeof(IPureResolverContext).GetMethods().First(
method => method is { Name: nameof(IPureResolverContext.Service), IsGenericMethod: true })
.MakeGenericMethod(typeof(UnitOfWork));
method => method is { Name: nameof(IPureResolverContext.Service), IsGenericMethod: true })
.MakeGenericMethod(typeof(IUnitOfWork));

Check warning on line 25 in src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs#L25

Added line #L25 was not covered by tests

private static readonly MethodInfo WithPrefilledVersionMethod =
typeof(UnitOfWork).GetMethods(BindingFlags.Instance | BindingFlags.Public)
.First(m => m.Name == nameof(UnitOfWork.WithPrefilledVersion));
typeof(IUnitOfWork).GetMethods(BindingFlags.Instance | BindingFlags.Public)
.First(m => m.Name == nameof(IUnitOfWork.WithPrefilledVersion));

Check warning on line 29 in src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs#L28-L29

Added lines #L28 - L29 were not covered by tests

public bool CanHandle(ParameterInfo parameter) => typeof(UnitOfWork) == parameter.ParameterType
|| typeof(IUnitOfWork) == parameter.ParameterType;
public bool CanHandle(ParameterInfo parameter) =>
typeof(IUnitOfWork) == parameter.ParameterType;

/*
* Produces something like this: context.GetOrSetGlobalState(
* nameof(UnitOfWork.UnitOfWork),
* nameof(IUnitOfWork),
* _ =>
* context
* .Service<UnitOfWork.UnitOfWork>()
* .Service<IUnitOfWork>()
* .WithPrefilledVersion(
* context.GetGlobalState<long>(PrefillUnitOfWorkVersion)
* ))!;
Expand All @@ -53,13 +53,14 @@
context,
Expression.Constant(PrefillUnitOfWorkVersion)));

return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context, Expression.Constant(nameof(UnitOfWork)),
Expression.Lambda<Func<string, UnitOfWork>>(
return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context,
Expression.Constant(nameof(IUnitOfWork)),
Expression.Lambda<Func<string, IUnitOfWork>>(

Check warning on line 58 in src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs#L56-L58

Added lines #L56 - L58 were not covered by tests
getNewUnitOfWork,
Expression.Parameter(typeof(string))));
}

public ArgumentKind Kind => ArgumentKind.Custom;
public bool IsPure => true;
public bool IsPure => false;
public bool IsDefaultHandler => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class UnitOfWorkParameterExpressionBuilderTest
private readonly UnitOfWorkParameterExpressionBuilder _builder = new();

[Fact]
public void CanHandle_ShouldReturnTrueForUnitOfWorkParameter()
public void CanHandle_ShouldReturnFalseForUnitOfWorkParameter()
{
var parameter = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithUnitOfWork))!.GetParameters()[0];
Assert.True(_builder.CanHandle(parameter));
Assert.False(_builder.CanHandle(parameter));
}

[Fact]
Expand All @@ -39,9 +39,9 @@ public void Kind_ShouldReturnCustom()
}

[Fact]
public void IsPure_ShouldReturnTrue()
public void IsPure_ShouldReturnFalse()
{
Assert.True(_builder.IsPure);
Assert.False(_builder.IsPure);
}

[Fact]
Expand Down