Skip to content

Commit 8be6806

Browse files
authored
Fixed issue with predicates and selectors in generated DataLoader. (#7610)
1 parent 6df0802 commit 8be6806

File tree

5 files changed

+314
-2
lines changed

5 files changed

+314
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,19 @@ public void WriteDataLoaderLoadMethod(
264264
parameter.StateKey);
265265
_writer.IncreaseIndent();
266266
_writer.WriteIndentedLine(
267-
"?? new global::GreenDonut.Projections.DefaultSelectorBuilder();");
267+
"?? new global::GreenDonut.Selectors.DefaultSelectorBuilder();");
268+
_writer.DecreaseIndent();
269+
}
270+
else if (parameter.Kind is DataLoaderParameterKind.PredicateBuilder)
271+
{
272+
_writer.WriteIndentedLine(
273+
"var {0} = context.GetState<{1}>(\"{2}\")",
274+
parameter.VariableName,
275+
parameter.Type.ToFullyQualified(),
276+
parameter.StateKey);
277+
_writer.IncreaseIndent();
278+
_writer.WriteIndentedLine(
279+
"?? new global::GreenDonut.Predicates.DefaultPredicateBuilder();");
268280
_writer.DecreaseIndent();
269281
}
270282
else if (parameter.Kind is DataLoaderParameterKind.PagingArguments)

src/HotChocolate/Core/src/Types.Analyzers/WellKnownTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static class WellKnownTypes
6666
public const string PromiseCacheObserver = "GreenDonut.PromiseCacheObserver";
6767
public const string KeyValuePair = "System.Collections.Generic.KeyValuePair";
6868
public const string EnumerableExtensions = "System.Linq.Enumerable";
69-
public const string SelectorBuilder = "GreenDonut.Projections.ISelectorBuilder";
69+
public const string SelectorBuilder = "GreenDonut.Selectors.ISelectorBuilder";
7070
public const string PredicateBuilder = "GreenDonut.Predicates.IPredicateBuilder";
7171
public const string PagingArguments = "HotChocolate.Pagination.PagingArguments";
7272

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,54 @@ public static Task<IDictionary<int, string>> GetEntityByIdAsync(
449449
}
450450
""").MatchMarkdownAsync();
451451
}
452+
453+
[Fact]
454+
public async Task GenerateSource_BatchDataLoader_With_SelectorBuilder_MatchesSnapshot()
455+
{
456+
await TestHelper.GetGeneratedSourceSnapshot(
457+
"""
458+
using System.Collections.Generic;
459+
using System.Threading;
460+
using System.Threading.Tasks;
461+
using HotChocolate;
462+
using GreenDonut;
463+
464+
namespace TestNamespace;
465+
466+
internal static class TestClass
467+
{
468+
[DataLoader]
469+
public static Task<IDictionary<int, string>> GetEntityByIdAsync(
470+
IReadOnlyList<int> entityIds,
471+
GreenDonut.Selectors.ISelectorBuilder selector,
472+
CancellationToken cancellationToken)
473+
=> default!;
474+
}
475+
""").MatchMarkdownAsync();
476+
}
477+
478+
[Fact]
479+
public async Task GenerateSource_BatchDataLoader_With_PredicateBuilder_MatchesSnapshot()
480+
{
481+
await TestHelper.GetGeneratedSourceSnapshot(
482+
"""
483+
using System.Collections.Generic;
484+
using System.Threading;
485+
using System.Threading.Tasks;
486+
using HotChocolate;
487+
using GreenDonut;
488+
489+
namespace TestNamespace;
490+
491+
internal static class TestClass
492+
{
493+
[DataLoader]
494+
public static Task<IDictionary<int, string>> GetEntityByIdAsync(
495+
IReadOnlyList<int> entityIds,
496+
GreenDonut.Predicates.IPredicateBuilder predicate,
497+
CancellationToken cancellationToken)
498+
=> default!;
499+
}
500+
""").MatchMarkdownAsync();
501+
}
452502
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# GenerateSource_BatchDataLoader_With_PredicateBuilder_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
17+
{
18+
public interface IEntityByIdDataLoader
19+
: global::GreenDonut.IDataLoader<int, string>
20+
{
21+
}
22+
23+
public sealed class EntityByIdDataLoader
24+
: global::GreenDonut.DataLoaderBase<int, string>
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<string?>> results,
42+
global::GreenDonut.DataLoaderFetchContext<string> context,
43+
global::System.Threading.CancellationToken ct)
44+
{
45+
var p1 = context.GetState<global::GreenDonut.Predicates.IPredicateBuilder>("GreenDonut.Predicates.IPredicateBuilder")
46+
?? new global::GreenDonut.Predicates.DefaultPredicateBuilder();
47+
var temp = await TestNamespace.TestClass.GetEntityByIdAsync(keys, p1, ct).ConfigureAwait(false);
48+
CopyResults(keys, results.Span, temp);
49+
}
50+
51+
private void CopyResults(
52+
global::System.Collections.Generic.IReadOnlyList<int> keys,
53+
global::System.Span<GreenDonut.Result<string?>> results,
54+
global::System.Collections.Generic.IDictionary<int, string> resultMap)
55+
{
56+
for (var i = 0; i < keys.Count; i++)
57+
{
58+
var key = keys[i];
59+
if (resultMap.TryGetValue(key, out var value))
60+
{
61+
results[i] = global::GreenDonut.Result<string?>.Resolve(value);
62+
}
63+
else
64+
{
65+
results[i] = global::GreenDonut.Result<string?>.Resolve(default(string));
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
73+
```
74+
75+
## HotChocolateTypeModule.735550c.g.cs
76+
77+
```csharp
78+
// <auto-generated/>
79+
80+
#nullable enable
81+
#pragma warning disable
82+
83+
using System;
84+
using System.Runtime.CompilerServices;
85+
using HotChocolate;
86+
using HotChocolate.Types;
87+
using HotChocolate.Execution.Configuration;
88+
89+
namespace Microsoft.Extensions.DependencyInjection
90+
{
91+
public static partial class TestsTypesRequestExecutorBuilderExtensions
92+
{
93+
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
94+
{
95+
builder.AddDataLoader<global::TestNamespace.IEntityByIdDataLoader, global::TestNamespace.EntityByIdDataLoader>();
96+
return builder;
97+
}
98+
}
99+
}
100+
101+
```
102+
103+
## Compilation Diagnostics
104+
105+
```json
106+
[
107+
{
108+
"Id": "GD0002",
109+
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
110+
"Severity": "Error",
111+
"WarningLevel": 0,
112+
"Location": ": (13,8)-(13,47)",
113+
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
114+
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
115+
"Message": "'GreenDonut.Predicates.IPredicateBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
116+
"Category": "Compiler",
117+
"CustomTags": [
118+
"Compiler",
119+
"Telemetry",
120+
"CustomObsolete"
121+
]
122+
}
123+
]
124+
```
125+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# GenerateSource_BatchDataLoader_With_SelectorBuilder_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
17+
{
18+
public interface IEntityByIdDataLoader
19+
: global::GreenDonut.IDataLoader<int, string>
20+
{
21+
}
22+
23+
public sealed class EntityByIdDataLoader
24+
: global::GreenDonut.DataLoaderBase<int, string>
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<string?>> results,
42+
global::GreenDonut.DataLoaderFetchContext<string> context,
43+
global::System.Threading.CancellationToken ct)
44+
{
45+
var p1 = context.GetState<global::GreenDonut.Selectors.ISelectorBuilder>("GreenDonut.Selectors.ISelectorBuilder")
46+
?? new global::GreenDonut.Selectors.DefaultSelectorBuilder();
47+
var temp = await TestNamespace.TestClass.GetEntityByIdAsync(keys, p1, ct).ConfigureAwait(false);
48+
CopyResults(keys, results.Span, temp);
49+
}
50+
51+
private void CopyResults(
52+
global::System.Collections.Generic.IReadOnlyList<int> keys,
53+
global::System.Span<GreenDonut.Result<string?>> results,
54+
global::System.Collections.Generic.IDictionary<int, string> resultMap)
55+
{
56+
for (var i = 0; i < keys.Count; i++)
57+
{
58+
var key = keys[i];
59+
if (resultMap.TryGetValue(key, out var value))
60+
{
61+
results[i] = global::GreenDonut.Result<string?>.Resolve(value);
62+
}
63+
else
64+
{
65+
results[i] = global::GreenDonut.Result<string?>.Resolve(default(string));
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
73+
```
74+
75+
## HotChocolateTypeModule.735550c.g.cs
76+
77+
```csharp
78+
// <auto-generated/>
79+
80+
#nullable enable
81+
#pragma warning disable
82+
83+
using System;
84+
using System.Runtime.CompilerServices;
85+
using HotChocolate;
86+
using HotChocolate.Types;
87+
using HotChocolate.Execution.Configuration;
88+
89+
namespace Microsoft.Extensions.DependencyInjection
90+
{
91+
public static partial class TestsTypesRequestExecutorBuilderExtensions
92+
{
93+
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
94+
{
95+
builder.AddDataLoader<global::TestNamespace.IEntityByIdDataLoader, global::TestNamespace.EntityByIdDataLoader>();
96+
return builder;
97+
}
98+
}
99+
}
100+
101+
```
102+
103+
## Compilation Diagnostics
104+
105+
```json
106+
[
107+
{
108+
"Id": "GD0001",
109+
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
110+
"Severity": "Error",
111+
"WarningLevel": 0,
112+
"Location": ": (13,8)-(13,45)",
113+
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
114+
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
115+
"Message": "'GreenDonut.Selectors.ISelectorBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
116+
"Category": "Compiler",
117+
"CustomTags": [
118+
"Compiler",
119+
"Telemetry",
120+
"CustomObsolete"
121+
]
122+
}
123+
]
124+
```
125+

0 commit comments

Comments
 (0)