Skip to content

Commit

Permalink
Fixed Middleware Source Generator (#6940)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 23, 2024
1 parent b73979b commit 0ef04a7
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ private void WriteCtorServiceResolution(List<RequestMiddlewareParameterInfo> par
{
case RequestMiddlewareParameterKind.Service when !parameter.IsNullable:
_writer.WriteIndentedLine(
"var cp{0} = core.Services.GetRequiredService<global::{1}>();",
"var cp{0} = core.Services.GetRequiredService<{1}>();",
i,
parameter.TypeName);
break;

case RequestMiddlewareParameterKind.Service when parameter.IsNullable:
_writer.WriteIndentedLine(
"var cp{0} = core.Services.GetService<global::{1}>();",
"var cp{0} = core.Services.GetService<{1}>();",
i,
parameter.TypeName);
break;
Expand Down Expand Up @@ -155,6 +155,11 @@ private void WriteFactory(string typeName, List<RequestMiddlewareParameterInfo>
for (var i = 0; i < parameters.Count; i++)
{
var parameter = parameters[i];

if(i > 0)
{
_writer.Write(", ");
}

if (parameter.Kind is RequestMiddlewareParameterKind.Next)
{
Expand All @@ -179,14 +184,14 @@ private void WriteInvokeServiceResolution(List<RequestMiddlewareParameterInfo> p
{
case RequestMiddlewareParameterKind.Service when !parameter.IsNullable:
_writer.WriteIndentedLine(
"var ip{0} = context.Services.GetRequiredService<global::{1}>();",
"var ip{0} = context.Services.GetRequiredService<{1}>();",
i,
parameter.TypeName);
break;

case RequestMiddlewareParameterKind.Service when parameter.IsNullable:
_writer.WriteIndentedLine(
"var ip{0} = context.Services.GetService<global::{1}>();",
"var ip{0} = context.Services.GetService<{1}>();",
i,
parameter.TypeName);
break;
Expand Down Expand Up @@ -217,6 +222,11 @@ private void WriteInvoke(string methodName, List<RequestMiddlewareParameterInfo>
for (var i = 0; i < parameters.Count; i++)
{
var parameter = parameters[i];

if(i > 0)
{
_writer.Write(", ");
}

if (parameter.Kind is RequestMiddlewareParameterKind.Next)
{
Expand All @@ -243,7 +253,7 @@ public void WriteInterceptMethod(

_writer.WriteIndentedLine(
"[InterceptsLocation(\"{0}\", {1}, {2})]",
location.FilePath,
location.FilePath.Replace("\\", "\\\\"),
location.LineNumber,
location.CharacterNumber);
_writer.WriteIndentedLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task SchemaSnapshot()
.AddCustomModule()
.BuildSchemaAsync();

schema.MatchSnapshot();
schema.MatchMarkdownSnapshot();
}

[Fact]
Expand All @@ -29,20 +29,23 @@ public async Task ExecuteRootField()

var result = await services.ExecuteRequestAsync("{ foo }");

result.MatchSnapshot();
result.MatchMarkdownSnapshot();
}

[Fact]
public async Task ExecuteWithMiddleware()
{
var services = new ServiceCollection()
.AddSingleton<Service1>()
.AddSingleton<Service2>()
.AddSingleton<Service3>()
.AddGraphQL()
.AddCustomModule()
.UseRequest<SomeRequestMiddleware>()
.UseDefaultPipeline();

var result = await services.ExecuteRequestAsync("{ foo }");

result.MatchSnapshot();
result.MatchMarkdownSnapshot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@

namespace HotChocolate.Types;

public class SomeRequestMiddleware(RequestDelegate next)
public class SomeRequestMiddleware(RequestDelegate next, Service1 service1, Service2 service2)
{
public async ValueTask InvokeAsync(IRequestContext context)
public async ValueTask InvokeAsync(IRequestContext context, Service3 service3)
{
await next(context);

context.Result =
QueryResultBuilder.New()
.SetData(new Dictionary<string, object?> { { "hello", true } })
.SetData(
new Dictionary<string, object?>
{
{
$"{service1.Say()} {service3.Hello()} {service2.World()}", true
}
})
.Create();
}
}

public class Service1
{
public string Say() => nameof(Say);
}

public class Service2
{
public string World() => nameof(World);
}

public class Service3
{
public string Hello() => nameof(Hello);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ExecuteRootField

```json
{
"data": {
"foo": "foo"
}
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ExecuteWithMiddleware

```json
{
"data": {
"Say Hello World": true
}
}
```

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SchemaSnapshot

```graphql
schema {
query: Query
mutation: Mutation
Expand Down Expand Up @@ -39,4 +42,5 @@ type Subscription {
enum CustomEnum {
ABC
DEF
}
}
```

0 comments on commit 0ef04a7

Please sign in to comment.