Skip to content

Commit 212c9b0

Browse files
svihpincmichaelstaib
authored andcommitted
fix: #8264 - fully qualified string not used for types in dataloader generator (#8265)
1 parent 4978f77 commit 212c9b0

11 files changed

+149
-9
lines changed

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ public void WriteDataLoaderLoadMethod(
448448
kind is DataLoaderKind.Group ? "[]" : string.Empty,
449449
value.IsValueType ? string.Empty : "?");
450450
_writer.WriteIndentedLine(
451-
"global::{0} resultMap)",
452-
ExtractMapType(method.ReturnType));
451+
"{0} resultMap)",
452+
ExtractMapType(method.ReturnType).ToFullyQualifiedWithNullRefQualifier());
453453
}
454454

455455
_writer.WriteIndentedLine("{");

src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public static string PrintNullRefQualifier(this ITypeSymbol typeSymbol)
3232
public static string ToFullyQualified(this ITypeSymbol typeSymbol)
3333
=> typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
3434

35+
public static string ToFullyQualifiedWithNullRefQualifier(this ITypeSymbol typeSymbol)
36+
{
37+
var format = SymbolDisplayFormat.FullyQualifiedFormat
38+
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
39+
return typeSymbol.ToDisplayString(format);
40+
}
41+
3542
public static bool IsParent(this IParameterSymbol parameter)
3643
=> parameter.IsThis
3744
|| parameter

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,39 @@ internal static class TestClass
318318
""").MatchMarkdownAsync();
319319
}
320320

321+
/**
322+
* test specific for <see href="https://github.com/ChilliCream/graphql-platform/pull/8264"/>
323+
*/
324+
[Fact]
325+
public async Task GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot()
326+
{
327+
await TestHelper.GetGeneratedSourceSnapshot([
328+
"""
329+
namespace DataLoaderGen.Result;
330+
public class TestResultClass {}
331+
""",
332+
"""
333+
using System.Collections.Generic;
334+
using System.Threading;
335+
using System.Threading.Tasks;
336+
using HotChocolate;
337+
using GreenDonut;
338+
using DataLoaderGen.Result;
339+
340+
namespace TestNamespace.DataLoaderGen;
341+
342+
internal static class TestClass
343+
{
344+
[DataLoader]
345+
public static Task<Dictionary<int, TestResultClass?>> GetEntityByIdAsync(
346+
IReadOnlyList<int> entityIds,
347+
CancellationToken cancellationToken)
348+
=> default!;
349+
}
350+
"""
351+
]).MatchMarkdownAsync();
352+
}
353+
321354
[Fact]
322355
public async Task GenerateSource_BatchDataLoader_With_Optional_State_MatchesSnapshot()
323356
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.DataLoader_With_Optional_Lookup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity?>> results,
52-
global::System.Collections.Generic.IReadOnlyDictionary<int, TestNamespace.Entity> resultMap)
52+
global::System.Collections.Generic.IReadOnlyDictionary<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity?>> results,
52-
global::System.Collections.Generic.IReadOnlyDictionary<int, TestNamespace.Entity> resultMap)
52+
global::System.Collections.Generic.IReadOnlyDictionary<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot
2+
3+
## GreenDonutDataLoader.735550c.g.cs
4+
5+
```csharp
6+
// <auto-generated/>
7+
8+
#nullable enable
9+
#pragma warning disable
10+
11+
using System;
12+
using System.Runtime.CompilerServices;
13+
using Microsoft.Extensions.DependencyInjection;
14+
using GreenDonut;
15+
16+
namespace TestNamespace.DataLoaderGen
17+
{
18+
public interface IEntityByIdDataLoader
19+
: global::GreenDonut.IDataLoader<int, global::DataLoaderGen.Result.TestResultClass>
20+
{
21+
}
22+
23+
public sealed partial class EntityByIdDataLoader
24+
: global::GreenDonut.DataLoaderBase<int, global::DataLoaderGen.Result.TestResultClass>
25+
, IEntityByIdDataLoader
26+
{
27+
private readonly global::System.IServiceProvider _services;
28+
29+
public EntityByIdDataLoader(
30+
global::System.IServiceProvider services,
31+
global::GreenDonut.IBatchScheduler batchScheduler,
32+
global::GreenDonut.DataLoaderOptions options)
33+
: base(batchScheduler, options)
34+
{
35+
_services = services ??
36+
throw new global::System.ArgumentNullException(nameof(services));
37+
}
38+
39+
protected override async global::System.Threading.Tasks.ValueTask FetchAsync(
40+
global::System.Collections.Generic.IReadOnlyList<int> keys,
41+
global::System.Memory<GreenDonut.Result<global::DataLoaderGen.Result.TestResultClass?>> results,
42+
global::GreenDonut.DataLoaderFetchContext<global::DataLoaderGen.Result.TestResultClass> context,
43+
global::System.Threading.CancellationToken ct)
44+
{
45+
var temp = await global::TestNamespace.DataLoaderGen.TestClass.GetEntityByIdAsync(keys, ct).ConfigureAwait(false);
46+
CopyResults(keys, results.Span, temp);
47+
}
48+
49+
private void CopyResults(
50+
global::System.Collections.Generic.IReadOnlyList<int> keys,
51+
global::System.Span<GreenDonut.Result<global::DataLoaderGen.Result.TestResultClass?>> results,
52+
global::System.Collections.Generic.Dictionary<int, global::DataLoaderGen.Result.TestResultClass?> resultMap)
53+
{
54+
for (var i = 0; i < keys.Count; i++)
55+
{
56+
var key = keys[i];
57+
if (resultMap.TryGetValue(key, out var value))
58+
{
59+
results[i] = global::GreenDonut.Result<global::DataLoaderGen.Result.TestResultClass?>.Resolve(value);
60+
}
61+
else
62+
{
63+
results[i] = global::GreenDonut.Result<global::DataLoaderGen.Result.TestResultClass?>.Resolve(default(global::DataLoaderGen.Result.TestResultClass));
64+
}
65+
}
66+
}
67+
}
68+
}
69+
70+
71+
```
72+
73+
## HotChocolateTypeModule.735550c.g.cs
74+
75+
```csharp
76+
// <auto-generated/>
77+
78+
#nullable enable
79+
#pragma warning disable
80+
81+
using System;
82+
using System.Runtime.CompilerServices;
83+
using HotChocolate;
84+
using HotChocolate.Types;
85+
using HotChocolate.Execution.Configuration;
86+
87+
namespace Microsoft.Extensions.DependencyInjection
88+
{
89+
public static partial class TestsTypesRequestExecutorBuilderExtensions
90+
{
91+
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
92+
{
93+
builder.AddDataLoader<global::TestNamespace.DataLoaderGen.IEntityByIdDataLoader, global::TestNamespace.DataLoaderGen.EntityByIdDataLoader>();
94+
return builder;
95+
}
96+
}
97+
}
98+
99+
```
100+

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Group_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity?>> results,
52-
global::System.Collections.Generic.IReadOnlyDictionary<int, TestNamespace.Entity> resultMap)
52+
global::System.Collections.Generic.IReadOnlyDictionary<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Group_Only_On_Class_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity?>> results,
52-
global::System.Collections.Generic.IReadOnlyDictionary<int, TestNamespace.Entity> resultMap)
52+
global::System.Collections.Generic.IReadOnlyDictionary<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Group_Only_On_Method_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity?>> results,
52-
global::System.Collections.Generic.IReadOnlyDictionary<int, TestNamespace.Entity> resultMap)
52+
global::System.Collections.Generic.IReadOnlyDictionary<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Lookup_From_OtherType_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace TestNamespace
5353
private void CopyResults(
5454
global::System.Collections.Generic.IReadOnlyList<int> keys,
5555
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity2?>> results,
56-
global::System.Collections.Generic.IDictionary<int, TestNamespace.Entity2> resultMap)
56+
global::System.Collections.Generic.IDictionary<int, global::TestNamespace.Entity2> resultMap)
5757
{
5858
for (var i = 0; i < keys.Count; i++)
5959
{

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_GroupedDataLoader_MatchesSnapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace TestNamespace
4949
private void CopyResults(
5050
global::System.Collections.Generic.IReadOnlyList<int> keys,
5151
global::System.Span<GreenDonut.Result<global::TestNamespace.Entity[]?>> results,
52-
global::System.Linq.ILookup<int, TestNamespace.Entity> resultMap)
52+
global::System.Linq.ILookup<int, global::TestNamespace.Entity> resultMap)
5353
{
5454
for (var i = 0; i < keys.Count; i++)
5555
{

0 commit comments

Comments
 (0)