Skip to content

Commit

Permalink
Fixed issue with predicates and selectors in generated DataLoader. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Oct 16, 2024
1 parent 6df0802 commit 8be6806
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,19 @@ public void WriteDataLoaderLoadMethod(
parameter.StateKey);
_writer.IncreaseIndent();
_writer.WriteIndentedLine(
"?? new global::GreenDonut.Projections.DefaultSelectorBuilder();");
"?? new global::GreenDonut.Selectors.DefaultSelectorBuilder();");
_writer.DecreaseIndent();
}
else if (parameter.Kind is DataLoaderParameterKind.PredicateBuilder)
{
_writer.WriteIndentedLine(
"var {0} = context.GetState<{1}>(\"{2}\")",
parameter.VariableName,
parameter.Type.ToFullyQualified(),
parameter.StateKey);
_writer.IncreaseIndent();
_writer.WriteIndentedLine(
"?? new global::GreenDonut.Predicates.DefaultPredicateBuilder();");
_writer.DecreaseIndent();
}
else if (parameter.Kind is DataLoaderParameterKind.PagingArguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class WellKnownTypes
public const string PromiseCacheObserver = "GreenDonut.PromiseCacheObserver";
public const string KeyValuePair = "System.Collections.Generic.KeyValuePair";
public const string EnumerableExtensions = "System.Linq.Enumerable";
public const string SelectorBuilder = "GreenDonut.Projections.ISelectorBuilder";
public const string SelectorBuilder = "GreenDonut.Selectors.ISelectorBuilder";
public const string PredicateBuilder = "GreenDonut.Predicates.IPredicateBuilder";
public const string PagingArguments = "HotChocolate.Pagination.PagingArguments";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,54 @@ public static Task<IDictionary<int, string>> GetEntityByIdAsync(
}
""").MatchMarkdownAsync();
}

[Fact]
public async Task GenerateSource_BatchDataLoader_With_SelectorBuilder_MatchesSnapshot()
{
await TestHelper.GetGeneratedSourceSnapshot(
"""
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate;
using GreenDonut;

namespace TestNamespace;

internal static class TestClass
{
[DataLoader]
public static Task<IDictionary<int, string>> GetEntityByIdAsync(
IReadOnlyList<int> entityIds,
GreenDonut.Selectors.ISelectorBuilder selector,
CancellationToken cancellationToken)
=> default!;
}
""").MatchMarkdownAsync();
}

[Fact]
public async Task GenerateSource_BatchDataLoader_With_PredicateBuilder_MatchesSnapshot()
{
await TestHelper.GetGeneratedSourceSnapshot(
"""
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate;
using GreenDonut;

namespace TestNamespace;

internal static class TestClass
{
[DataLoader]
public static Task<IDictionary<int, string>> GetEntityByIdAsync(
IReadOnlyList<int> entityIds,
GreenDonut.Predicates.IPredicateBuilder predicate,
CancellationToken cancellationToken)
=> default!;
}
""").MatchMarkdownAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# GenerateSource_BatchDataLoader_With_PredicateBuilder_MatchesSnapshot

## GreenDonutDataLoader.735550c.g.cs

```csharp
// <auto-generated/>
#nullable enable
#pragma warning disable

using System;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using GreenDonut;

namespace TestNamespace
{
public interface IEntityByIdDataLoader
: global::GreenDonut.IDataLoader<int, string>
{
}

public sealed class EntityByIdDataLoader
: global::GreenDonut.DataLoaderBase<int, string>
, IEntityByIdDataLoader
{
private readonly global::System.IServiceProvider _services;

public EntityByIdDataLoader(
global::System.IServiceProvider services,
global::GreenDonut.IBatchScheduler batchScheduler,
global::GreenDonut.DataLoaderOptions options)
: base(batchScheduler, options)
{
_services = services ??
throw new global::System.ArgumentNullException(nameof(services));
}

protected override async global::System.Threading.Tasks.ValueTask FetchAsync(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Memory<GreenDonut.Result<string?>> results,
global::GreenDonut.DataLoaderFetchContext<string> context,
global::System.Threading.CancellationToken ct)
{
var p1 = context.GetState<global::GreenDonut.Predicates.IPredicateBuilder>("GreenDonut.Predicates.IPredicateBuilder")
?? new global::GreenDonut.Predicates.DefaultPredicateBuilder();
var temp = await TestNamespace.TestClass.GetEntityByIdAsync(keys, p1, ct).ConfigureAwait(false);
CopyResults(keys, results.Span, temp);
}

private void CopyResults(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Span<GreenDonut.Result<string?>> results,
global::System.Collections.Generic.IDictionary<int, string> resultMap)
{
for (var i = 0; i < keys.Count; i++)
{
var key = keys[i];
if (resultMap.TryGetValue(key, out var value))
{
results[i] = global::GreenDonut.Result<string?>.Resolve(value);
}
else
{
results[i] = global::GreenDonut.Result<string?>.Resolve(default(string));
}
}
}
}
}


```

## HotChocolateTypeModule.735550c.g.cs

```csharp
// <auto-generated/>
#nullable enable
#pragma warning disable

using System;
using System.Runtime.CompilerServices;
using HotChocolate;
using HotChocolate.Types;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class TestsTypesRequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
{
builder.AddDataLoader<global::TestNamespace.IEntityByIdDataLoader, global::TestNamespace.EntityByIdDataLoader>();
return builder;
}
}
}

```

## Compilation Diagnostics

```json
[
{
"Id": "GD0002",
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Severity": "Error",
"WarningLevel": 0,
"Location": ": (13,8)-(13,47)",
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Message": "'GreenDonut.Predicates.IPredicateBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Category": "Compiler",
"CustomTags": [
"Compiler",
"Telemetry",
"CustomObsolete"
]
}
]
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# GenerateSource_BatchDataLoader_With_SelectorBuilder_MatchesSnapshot

## GreenDonutDataLoader.735550c.g.cs

```csharp
// <auto-generated/>
#nullable enable
#pragma warning disable

using System;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using GreenDonut;

namespace TestNamespace
{
public interface IEntityByIdDataLoader
: global::GreenDonut.IDataLoader<int, string>
{
}

public sealed class EntityByIdDataLoader
: global::GreenDonut.DataLoaderBase<int, string>
, IEntityByIdDataLoader
{
private readonly global::System.IServiceProvider _services;

public EntityByIdDataLoader(
global::System.IServiceProvider services,
global::GreenDonut.IBatchScheduler batchScheduler,
global::GreenDonut.DataLoaderOptions options)
: base(batchScheduler, options)
{
_services = services ??
throw new global::System.ArgumentNullException(nameof(services));
}

protected override async global::System.Threading.Tasks.ValueTask FetchAsync(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Memory<GreenDonut.Result<string?>> results,
global::GreenDonut.DataLoaderFetchContext<string> context,
global::System.Threading.CancellationToken ct)
{
var p1 = context.GetState<global::GreenDonut.Selectors.ISelectorBuilder>("GreenDonut.Selectors.ISelectorBuilder")
?? new global::GreenDonut.Selectors.DefaultSelectorBuilder();
var temp = await TestNamespace.TestClass.GetEntityByIdAsync(keys, p1, ct).ConfigureAwait(false);
CopyResults(keys, results.Span, temp);
}

private void CopyResults(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Span<GreenDonut.Result<string?>> results,
global::System.Collections.Generic.IDictionary<int, string> resultMap)
{
for (var i = 0; i < keys.Count; i++)
{
var key = keys[i];
if (resultMap.TryGetValue(key, out var value))
{
results[i] = global::GreenDonut.Result<string?>.Resolve(value);
}
else
{
results[i] = global::GreenDonut.Result<string?>.Resolve(default(string));
}
}
}
}
}


```

## HotChocolateTypeModule.735550c.g.cs

```csharp
// <auto-generated/>
#nullable enable
#pragma warning disable

using System;
using System.Runtime.CompilerServices;
using HotChocolate;
using HotChocolate.Types;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class TestsTypesRequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
{
builder.AddDataLoader<global::TestNamespace.IEntityByIdDataLoader, global::TestNamespace.EntityByIdDataLoader>();
return builder;
}
}
}

```

## Compilation Diagnostics

```json
[
{
"Id": "GD0001",
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Severity": "Error",
"WarningLevel": 0,
"Location": ": (13,8)-(13,45)",
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Message": "'GreenDonut.Selectors.ISelectorBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Category": "Compiler",
"CustomTags": [
"Compiler",
"Telemetry",
"CustomObsolete"
]
}
]
```

0 comments on commit 8be6806

Please sign in to comment.