Skip to content

Commit 0ef04a7

Browse files
authored
Fixed Middleware Source Generator (#6940)
1 parent b73979b commit 0ef04a7

File tree

7 files changed

+64
-18
lines changed

7 files changed

+64
-18
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Generators/RequestMiddlewareSyntaxGenerator.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ private void WriteCtorServiceResolution(List<RequestMiddlewareParameterInfo> par
106106
{
107107
case RequestMiddlewareParameterKind.Service when !parameter.IsNullable:
108108
_writer.WriteIndentedLine(
109-
"var cp{0} = core.Services.GetRequiredService<global::{1}>();",
109+
"var cp{0} = core.Services.GetRequiredService<{1}>();",
110110
i,
111111
parameter.TypeName);
112112
break;
113113

114114
case RequestMiddlewareParameterKind.Service when parameter.IsNullable:
115115
_writer.WriteIndentedLine(
116-
"var cp{0} = core.Services.GetService<global::{1}>();",
116+
"var cp{0} = core.Services.GetService<{1}>();",
117117
i,
118118
parameter.TypeName);
119119
break;
@@ -155,6 +155,11 @@ private void WriteFactory(string typeName, List<RequestMiddlewareParameterInfo>
155155
for (var i = 0; i < parameters.Count; i++)
156156
{
157157
var parameter = parameters[i];
158+
159+
if(i > 0)
160+
{
161+
_writer.Write(", ");
162+
}
158163

159164
if (parameter.Kind is RequestMiddlewareParameterKind.Next)
160165
{
@@ -179,14 +184,14 @@ private void WriteInvokeServiceResolution(List<RequestMiddlewareParameterInfo> p
179184
{
180185
case RequestMiddlewareParameterKind.Service when !parameter.IsNullable:
181186
_writer.WriteIndentedLine(
182-
"var ip{0} = context.Services.GetRequiredService<global::{1}>();",
187+
"var ip{0} = context.Services.GetRequiredService<{1}>();",
183188
i,
184189
parameter.TypeName);
185190
break;
186191

187192
case RequestMiddlewareParameterKind.Service when parameter.IsNullable:
188193
_writer.WriteIndentedLine(
189-
"var ip{0} = context.Services.GetService<global::{1}>();",
194+
"var ip{0} = context.Services.GetService<{1}>();",
190195
i,
191196
parameter.TypeName);
192197
break;
@@ -217,6 +222,11 @@ private void WriteInvoke(string methodName, List<RequestMiddlewareParameterInfo>
217222
for (var i = 0; i < parameters.Count; i++)
218223
{
219224
var parameter = parameters[i];
225+
226+
if(i > 0)
227+
{
228+
_writer.Write(", ");
229+
}
220230

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

244254
_writer.WriteIndentedLine(
245255
"[InterceptsLocation(\"{0}\", {1}, {2})]",
246-
location.FilePath,
256+
location.FilePath.Replace("\\", "\\\\"),
247257
location.LineNumber,
248258
location.CharacterNumber);
249259
_writer.WriteIndentedLine(

src/HotChocolate/Core/test/Types.Analyzers.Tests/AnnotationBasedSchemaTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task SchemaSnapshot()
1717
.AddCustomModule()
1818
.BuildSchemaAsync();
1919

20-
schema.MatchSnapshot();
20+
schema.MatchMarkdownSnapshot();
2121
}
2222

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

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

32-
result.MatchSnapshot();
32+
result.MatchMarkdownSnapshot();
3333
}
3434

3535
[Fact]
3636
public async Task ExecuteWithMiddleware()
3737
{
3838
var services = new ServiceCollection()
39+
.AddSingleton<Service1>()
40+
.AddSingleton<Service2>()
41+
.AddSingleton<Service3>()
3942
.AddGraphQL()
4043
.AddCustomModule()
4144
.UseRequest<SomeRequestMiddleware>()
4245
.UseDefaultPipeline();
4346

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

46-
result.MatchSnapshot();
49+
result.MatchMarkdownSnapshot();
4750
}
4851
}

src/HotChocolate/Core/test/Types.Analyzers.Tests/SomeRequestMiddleware.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,36 @@
44

55
namespace HotChocolate.Types;
66

7-
public class SomeRequestMiddleware(RequestDelegate next)
7+
public class SomeRequestMiddleware(RequestDelegate next, Service1 service1, Service2 service2)
88
{
9-
public async ValueTask InvokeAsync(IRequestContext context)
9+
public async ValueTask InvokeAsync(IRequestContext context, Service3 service3)
1010
{
1111
await next(context);
1212

1313
context.Result =
1414
QueryResultBuilder.New()
15-
.SetData(new Dictionary<string, object?> { { "hello", true } })
15+
.SetData(
16+
new Dictionary<string, object?>
17+
{
18+
{
19+
$"{service1.Say()} {service3.Hello()} {service2.World()}", true
20+
}
21+
})
1622
.Create();
1723
}
24+
}
25+
26+
public class Service1
27+
{
28+
public string Say() => nameof(Say);
29+
}
30+
31+
public class Service2
32+
{
33+
public string World() => nameof(World);
34+
}
35+
36+
public class Service3
37+
{
38+
public string Hello() => nameof(Hello);
1839
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# ExecuteRootField
2+
3+
```json
14
{
25
"data": {
36
"foo": "foo"
47
}
5-
}
8+
}
9+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ExecuteWithMiddleware
2+
3+
```json
4+
{
5+
"data": {
6+
"Say Hello World": true
7+
}
8+
}
9+
```

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/SchemaTests.ExecuteWithMiddleware.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# SchemaSnapshot
2+
3+
```graphql
14
schema {
25
query: Query
36
mutation: Mutation
@@ -39,4 +42,5 @@ type Subscription {
3942
enum CustomEnum {
4043
ABC
4144
DEF
42-
}
45+
}
46+
```

0 commit comments

Comments
 (0)