diff --git a/src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs b/src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs index 77ca3cb76c4..2fb925b4cb6 100644 --- a/src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs +++ b/src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs @@ -68,8 +68,8 @@ public void WriteDataLoaderInterface( kind is DataLoaderKind.Group ? ": global::GreenDonut.IDataLoader<{0}, {1}[]>" : ": global::GreenDonut.IDataLoader<{0}, {1}>", - key.ToFullyQualified(), - value.ToFullyQualified()); + key.ToFullyQualifiedWithNullRefQualifier(), + value.ToFullyQualifiedWithNullRefQualifier()); _writer.DecreaseIndent(); _writer.WriteIndentedLine("{"); @@ -97,8 +97,8 @@ public void WriteBeginDataLoaderClass( kind is DataLoaderKind.Group ? ": global::GreenDonut.DataLoaderBase<{0}, {1}[]>" : ": global::GreenDonut.DataLoaderBase<{0}, {1}>", - key.ToFullyQualified(), - value.ToFullyQualified()); + key.ToFullyQualifiedWithNullRefQualifier(), + value.ToFullyQualifiedWithNullRefQualifier()); if (withInterface) { _writer.WriteIndentedLine(", {0}", interfaceName); @@ -174,23 +174,20 @@ public void WriteDataLoaderConstructor( if (lookup.IsTransform) { _writer.WriteIndentedLine( - ".Create<{0}, {1}{2}, {3}{4}>({5}.{6}, this)", - keyType.ToFullyQualified(), - valueType.ToFullyQualified(), - valueType.PrintNullRefQualifier(), - lookup.Method.Parameters[0].Type.ToFullyQualified(), - lookup.Method.Parameters[0].Type.PrintNullRefQualifier(), - lookup.Method.ContainingType.ToFullyQualified(), + ".Create<{0}, {1}, {2}>({3}.{4}, this)", + keyType.ToFullyQualifiedWithNullRefQualifier(), + valueType.ToFullyQualifiedWithNullRefQualifier(), + lookup.Method.Parameters[0].Type.ToFullyQualifiedWithNullRefQualifier(), + lookup.Method.ContainingType.ToFullyQualifiedWithNullRefQualifier(), lookup.Method.Name); } else { _writer.WriteIndentedLine( - ".Create<{0}, {1}{2}>({3}.{4}, this)", - keyType.ToFullyQualified(), - valueType.ToFullyQualified(), - valueType.PrintNullRefQualifier(), - lookup.Method.ContainingType.ToFullyQualified(), + ".Create<{0}, {1}>({2}.{3}, this)", + keyType.ToFullyQualifiedWithNullRefQualifier(), + valueType.ToFullyQualifiedWithNullRefQualifier(), + lookup.Method.ContainingType.ToFullyQualifiedWithNullRefQualifier(), lookup.Method.Name); } @@ -221,18 +218,18 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "global::{0}<{1}> keys,", WellKnownTypes.ReadOnlyList, - key.ToFullyQualified()); + key.ToFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( - "global::{0}<{1}<{2}{3}{4}>> results,", + "global::{0}<{1}<{2}>> results,", WellKnownTypes.Memory, WellKnownTypes.Result, - value.ToFullyQualified(), - kind is DataLoaderKind.Group ? "[]" : string.Empty, - value.IsValueType ? string.Empty : "?"); + kind is DataLoaderKind.Group + ? $"{value.ToClassNonNullableFullyQualifiedWithNullRefQualifier()}[]?" + : value.ToNullableFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( "global::{0}<{1}{2}> context,", WellKnownTypes.DataLoaderFetchContext, - value.ToFullyQualified(), + value.ToFullyQualifiedWithNullRefQualifier(), kind is DataLoaderKind.Group ? "[]" : string.Empty); _writer.WriteIndentedLine( "global::{0} ct)", @@ -256,14 +253,14 @@ public void WriteDataLoaderLoadMethod( "var {0} = {1}.GetRequiredService<{2}>();", parameter.VariableName, isScoped ? "scope.ServiceProvider" : "_services", - parameter.Type.ToFullyQualified()); + parameter.Type.ToFullyQualifiedWithNullRefQualifier()); } else if (parameter.Kind is DataLoaderParameterKind.SelectorBuilder) { _writer.WriteIndentedLine( "var {0} = context.GetState<{1}>(\"{2}\")", parameter.VariableName, - parameter.Type.ToFullyQualified(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); _writer.IncreaseIndent(); _writer.WriteIndentedLine( @@ -275,7 +272,7 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "var {0} = context.GetState<{1}>(\"{2}\")", parameter.VariableName, - parameter.Type.ToFullyQualified(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); _writer.IncreaseIndent(); _writer.WriteIndentedLine( @@ -287,12 +284,12 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "var {0} = context.GetState<{1}>(\"{2}\")", parameter.VariableName, - parameter.Type.ToFullyQualified(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); _writer.IncreaseIndent(); _writer.WriteIndentedLine( "?? {0}.Empty;", - parameter.Type.ToFullyQualified()); + parameter.Type.ToFullyQualifiedWithNullRefQualifier()); _writer.DecreaseIndent(); } else if (parameter.Kind is DataLoaderParameterKind.QueryContext) @@ -302,18 +299,18 @@ public void WriteDataLoaderLoadMethod( parameter.VariableName, WellKnownTypes.SelectorBuilder, DataLoaderInfo.Selector, - ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified()); + ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( "var {0}_predicate = context.GetState(\"{2}\")?.TryCompile<{3}>();", parameter.VariableName, WellKnownTypes.PredicateBuilder, DataLoaderInfo.Predicate, - ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified()); + ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( "var {0}_sortDefinition = context.GetState>(\"{3}\");", parameter.VariableName, WellKnownTypes.SortDefinition, - ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified(), + ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualifiedWithNullRefQualifier(), DataLoaderInfo.Sorting); _writer.WriteLine(); @@ -321,7 +318,7 @@ public void WriteDataLoaderLoadMethod( "var {0} = global::{1}<{2}>.Empty;", parameter.VariableName, WellKnownTypes.QueryContext, - ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified()); + ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( "if({0}_selector is not null || {0}_predicate is not null || {0}_sortDefinition is not null)", parameter.VariableName); @@ -332,7 +329,7 @@ public void WriteDataLoaderLoadMethod( + "{0}_predicate, {0}_sortDefinition);", parameter.VariableName, WellKnownTypes.QueryContext, - ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified()); + ((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualifiedWithNullRefQualifier()); _writer.DecreaseIndent(); _writer.WriteIndentedLine("}"); } @@ -341,7 +338,7 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "var {0} = context.GetRequiredState<{1}>(\"{2}\");", parameter.VariableName, - parameter.Type.ToFullyQualified(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); } else if (parameter.Kind is DataLoaderParameterKind.ContextData) @@ -352,20 +349,18 @@ public void WriteDataLoaderLoadMethod( var defaultValueString = ConvertDefaultValueToString(defaultValue, parameter.Type); _writer.WriteIndentedLine( - "var {0} = context.GetStateOrDefault<{1}{2}>(\"{3}\", {4});", + "var {0} = context.GetStateOrDefault<{1}>(\"{2}\", {3});", parameter.VariableName, - parameter.Type.ToFullyQualified(), - parameter.Type.PrintNullRefQualifier(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey, defaultValueString); } else if (parameter.Type.IsNullableType()) { _writer.WriteIndentedLine( - "var {0} = context.GetState<{1}{2}>(\"{3}\");", + "var {0} = context.GetState<{1}>(\"{2}\");", parameter.VariableName, - parameter.Type.ToFullyQualified(), - parameter.Type.PrintNullRefQualifier(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); } else @@ -373,7 +368,7 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "var {0} = context.GetRequiredState<{1}>(\"{2}\");", parameter.VariableName, - parameter.Type.ToFullyQualified(), + parameter.Type.ToFullyQualifiedWithNullRefQualifier(), parameter.StateKey); } } @@ -395,9 +390,8 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndented("var value = "); WriteFetchCall(method, containingType, kind, parameters); _writer.WriteIndentedLine( - "results.Span[i] = Result<{0}{1}>.Resolve(value);", - value.ToFullyQualified(), - value.IsValueType ? string.Empty : "?"); + "results.Span[i] = Result<{0}>.Resolve(value);", + value.ToNullableFullyQualifiedWithNullRefQualifier()); } _writer.WriteIndentedLine("}"); @@ -407,9 +401,8 @@ public void WriteDataLoaderLoadMethod( using (_writer.IncreaseIndent()) { _writer.WriteIndentedLine( - "results.Span[i] = Result<{0}{1}>.Reject(ex);", - value.ToFullyQualified(), - value.IsValueType ? string.Empty : "?"); + "results.Span[i] = Result<{0}>.Reject(ex);", + value.ToNullableFullyQualifiedWithNullRefQualifier()); } _writer.WriteIndentedLine("}"); @@ -439,14 +432,12 @@ public void WriteDataLoaderLoadMethod( _writer.WriteIndentedLine( "global::{0}<{1}> keys,", WellKnownTypes.ReadOnlyList, - key.ToFullyQualified()); + key.ToFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( - "global::{0}<{1}<{2}{3}{4}>> results,", + "global::{0}<{1}<{2}>> results,", WellKnownTypes.Span, WellKnownTypes.Result, - value.ToFullyQualified(), - kind is DataLoaderKind.Group ? "[]" : string.Empty, - value.IsValueType ? string.Empty : "?"); + kind is DataLoaderKind.Group ? $"{value.ToClassNonNullableFullyQualifiedWithNullRefQualifier()}[]?" : value.ToNullableFullyQualifiedWithNullRefQualifier()); _writer.WriteIndentedLine( "{0} resultMap)", ExtractMapType(method.ReturnType).ToFullyQualifiedWithNullRefQualifier()); @@ -501,10 +492,9 @@ public void WriteDataLoaderLoadMethod( using (_writer.IncreaseIndent()) { _writer.WriteIndentedLine( - "results[i] = global::{0}<{1}{2}>.Resolve(value);", + "results[i] = global::{0}<{1}>.Resolve(value);", WellKnownTypes.Result, - value.ToFullyQualified(), - value.IsValueType ? string.Empty : "?"); + value.ToNullableFullyQualifiedWithNullRefQualifier()); } _writer.WriteIndentedLine("}"); @@ -514,11 +504,10 @@ public void WriteDataLoaderLoadMethod( using (_writer.IncreaseIndent()) { _writer.WriteIndentedLine( - "results[i] = global::{0}<{1}{2}>.Resolve(default({3}));", + "results[i] = global::{0}<{1}>.Resolve(default({2}));", WellKnownTypes.Result, - value.ToFullyQualified(), - value.IsValueType ? string.Empty : "?", - value.ToFullyQualified()); + value.ToNullableFullyQualifiedWithNullRefQualifier(), + value.IsValueType ? value.ToNullableFullyQualifiedWithNullRefQualifier() : value.ToFullyQualified()); } _writer.WriteIndentedLine("}"); diff --git a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs index c601f0adda0..558729f54fe 100644 --- a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs +++ b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs @@ -39,6 +39,18 @@ public static string ToFullyQualifiedWithNullRefQualifier(this ITypeSymbol typeS return typeSymbol.ToDisplayString(format); } + public static string ToNullableFullyQualifiedWithNullRefQualifier(this ITypeSymbol typeSymbol) + { + var value = typeSymbol.ToFullyQualifiedWithNullRefQualifier(); + return value[value.Length - 1] != '?' ? value + "?" : value; + } + + public static string ToClassNonNullableFullyQualifiedWithNullRefQualifier(this ITypeSymbol typeSymbol) + { + var value = typeSymbol.ToFullyQualifiedWithNullRefQualifier(); + return !typeSymbol.IsValueType && value[value.Length - 1] == '?' ? value.Substring(0, value.Length - 1) : value; + } + public static bool IsParent(this IParameterSymbol parameter) => parameter.IsThis || parameter diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/DataLoaderTests.cs b/src/HotChocolate/Core/test/Types.Analyzers.Tests/DataLoaderTests.cs index e149ab905d7..1d7611a63be 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/DataLoaderTests.cs +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/DataLoaderTests.cs @@ -698,4 +698,95 @@ public static Task> GetEntityByIdAsync( } """).MatchMarkdownAsync(); } + + [Fact] + public async Task Generate_DataLoader_NullableAnnotated_AnonymousType_AsKey_MatchesSnapshot() + { + await TestHelper.GetGeneratedSourceSnapshot( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using HotChocolate; + using GreenDonut; + + namespace TestNamespace; + + public record Id1(); + public record Id2(); + public record Stuff(); + + public class Dataloaders + { + [DataLoader] + public static async Task> GetStuff( + IReadOnlyList<(Id1, Id2?)> keys, + CancellationToken cancellationToken) + { + await Task.CompletedTask; + return null!; + } + } + """).MatchMarkdownAsync(); + } + + [Fact] + public async Task GenerateSource_BatchDataLoader_ReturnsNullableStruct_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> GetEntityByIdAsync( + IReadOnlyList entityIds, + CancellationToken cancellationToken) + => default!; + } + + public struct Entity + { + public int Id { get; set; } + } + """).MatchMarkdownAsync(); + } + + [Fact] + public async Task GenerateSource_GroupedDataLoader_ReturnsNullableStruct_MatchesSnapshot() + { + await TestHelper.GetGeneratedSourceSnapshot( + """ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using HotChocolate; + using GreenDonut; + + namespace TestNamespace; + + internal static class TestClass + { + [DataLoader] + public static Task> GetEntitiesByIdAsync( + IReadOnlyList entityIds, + CancellationToken cancellationToken) + => default!; + } + + public struct Entity + { + public int Id { get; set; } + } + """).MatchMarkdownAsync(); + } } diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/RequestMiddlewareTests.cs b/src/HotChocolate/Core/test/Types.Analyzers.Tests/RequestMiddlewareTests.cs index b2db3200314..19d4a247d3a 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/RequestMiddlewareTests.cs +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/RequestMiddlewareTests.cs @@ -1,3 +1,5 @@ +using System.Globalization; + namespace HotChocolate.Types; public class RequestMiddlewareTests @@ -5,46 +7,56 @@ public class RequestMiddlewareTests [Fact] public async Task GenerateSource_RequestMiddleware_MatchesSnapshot() { - await TestHelper.GetGeneratedSourceSnapshot( - """ - #nullable enable - using System.Threading.Tasks; - using HotChocolate; - using HotChocolate.Execution; - using Microsoft.AspNetCore.Builder; - using Microsoft.Extensions.DependencyInjection; + var currentUiCulture = CultureInfo.CurrentUICulture; + try + { + // Snapshot contains localized strings -> adjust culture + CultureInfo.CurrentUICulture = new CultureInfo("en-US"); + await TestHelper.GetGeneratedSourceSnapshot( + """ + #nullable enable + using System.Threading.Tasks; + using HotChocolate; + using HotChocolate.Execution; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; - public class Program - { - public static void Main(string[] args) + public class Program { - var builder = WebApplication.CreateBuilder(args); - builder.Services - .AddGraphQLServer() - .UseRequest(); + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + builder.Services + .AddGraphQLServer() + .UseRequest(); + } } - } - public class SomeRequestMiddleware( - RequestDelegate next, - #pragma warning disable CS9113 - [SchemaService] Service1 service1, - [SchemaService] Service2? service2) - #pragma warning restore CS9113 - { - public async ValueTask InvokeAsync( - IRequestContext context, + public class SomeRequestMiddleware( + RequestDelegate next, #pragma warning disable CS9113 - Service1 service1, - Service2? service2) + [SchemaService] Service1 service1, + [SchemaService] Service2? service2) #pragma warning restore CS9113 { - await next(context); + public async ValueTask InvokeAsync( + IRequestContext context, + #pragma warning disable CS9113 + Service1 service1, + Service2? service2) + #pragma warning restore CS9113 + { + await next(context); + } } - } - public class Service1; - public class Service2; - """).MatchMarkdownAsync(); + public class Service1; + public class Service2; + """).MatchMarkdownAsync(); + } + finally + { + CultureInfo.CurrentUICulture = currentUiCulture; + } } } diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot.md index 724c562533f..86fc950d10f 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot.md +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Class_Instance_Result_MatchesSnapshot.md @@ -16,12 +16,12 @@ using GreenDonut; namespace TestNamespace.DataLoaderGen { public interface IEntityByIdDataLoader - : global::GreenDonut.IDataLoader + : global::GreenDonut.IDataLoader { } public sealed partial class EntityByIdDataLoader - : global::GreenDonut.DataLoaderBase + : global::GreenDonut.DataLoaderBase , IEntityByIdDataLoader { private readonly global::System.IServiceProvider _services; @@ -39,7 +39,7 @@ namespace TestNamespace.DataLoaderGen protected override async global::System.Threading.Tasks.ValueTask FetchAsync( global::System.Collections.Generic.IReadOnlyList keys, global::System.Memory> results, - global::GreenDonut.DataLoaderFetchContext context, + global::GreenDonut.DataLoaderFetchContext context, global::System.Threading.CancellationToken ct) { var temp = await global::TestNamespace.DataLoaderGen.TestClass.GetEntityByIdAsync(keys, ct).ConfigureAwait(false); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Result_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Result_MatchesSnapshot.md index e85309a09de..7a24d2fbe4c 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Result_MatchesSnapshot.md +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_Nullable_Result_MatchesSnapshot.md @@ -16,12 +16,12 @@ using GreenDonut; namespace TestNamespace { public interface IEntityByIdDataLoader - : global::GreenDonut.IDataLoader + : global::GreenDonut.IDataLoader { } public sealed partial class EntityByIdDataLoader - : global::GreenDonut.DataLoaderBase + : global::GreenDonut.DataLoaderBase , IEntityByIdDataLoader { private readonly global::System.IServiceProvider _services; @@ -39,7 +39,7 @@ namespace TestNamespace protected override async global::System.Threading.Tasks.ValueTask FetchAsync( global::System.Collections.Generic.IReadOnlyList keys, global::System.Memory> results, - global::GreenDonut.DataLoaderFetchContext context, + global::GreenDonut.DataLoaderFetchContext context, global::System.Threading.CancellationToken ct) { var temp = await global::TestNamespace.TestClass.GetEntityByIdAsync(keys, ct).ConfigureAwait(false); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_ReturnsNullableStruct_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_ReturnsNullableStruct_MatchesSnapshot.md new file mode 100644 index 00000000000..1dd02d2b461 --- /dev/null +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_ReturnsNullableStruct_MatchesSnapshot.md @@ -0,0 +1,100 @@ +# GenerateSource_BatchDataLoader_ReturnsNullableStruct_MatchesSnapshot + +## GreenDonutDataLoader.735550c.g.cs + +```csharp +// + +#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 + { + } + + public sealed partial class EntityByIdDataLoader + : global::GreenDonut.DataLoaderBase + , 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 keys, + global::System.Memory> results, + global::GreenDonut.DataLoaderFetchContext context, + global::System.Threading.CancellationToken ct) + { + var temp = await global::TestNamespace.TestClass.GetEntityByIdAsync(keys, ct).ConfigureAwait(false); + CopyResults(keys, results.Span, temp); + } + + private void CopyResults( + global::System.Collections.Generic.IReadOnlyList keys, + global::System.Span> results, + global::System.Collections.Generic.IReadOnlyDictionary 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.Resolve(value); + } + else + { + results[i] = global::GreenDonut.Result.Resolve(default(global::TestNamespace.Entity?)); + } + } + } + } +} + + +``` + +## HotChocolateTypeModule.735550c.g.cs + +```csharp +// + +#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(); + return builder; + } + } +} + +``` + diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Optional_State_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Optional_State_MatchesSnapshot.md index 7012928a393..70b03e4a17b 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Optional_State_MatchesSnapshot.md +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Optional_State_MatchesSnapshot.md @@ -16,12 +16,12 @@ using GreenDonut; namespace TestNamespace { public interface IEntityByIdDataLoader - : global::GreenDonut.IDataLoader + : global::GreenDonut.IDataLoader { } public sealed partial class EntityByIdDataLoader - : global::GreenDonut.DataLoaderBase + : global::GreenDonut.DataLoaderBase , IEntityByIdDataLoader { private readonly global::System.IServiceProvider _services; @@ -39,7 +39,7 @@ namespace TestNamespace protected override async global::System.Threading.Tasks.ValueTask FetchAsync( global::System.Collections.Generic.IReadOnlyList keys, global::System.Memory> results, - global::GreenDonut.DataLoaderFetchContext context, + global::GreenDonut.DataLoaderFetchContext context, global::System.Threading.CancellationToken ct) { var p1 = context.GetState("key"); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Required_State_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Required_State_MatchesSnapshot.md index b1116d98436..16c601147c0 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Required_State_MatchesSnapshot.md +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_Required_State_MatchesSnapshot.md @@ -16,12 +16,12 @@ using GreenDonut; namespace TestNamespace { public interface IEntityByIdDataLoader - : global::GreenDonut.IDataLoader + : global::GreenDonut.IDataLoader { } public sealed partial class EntityByIdDataLoader - : global::GreenDonut.DataLoaderBase + : global::GreenDonut.DataLoaderBase , IEntityByIdDataLoader { private readonly global::System.IServiceProvider _services; @@ -39,7 +39,7 @@ namespace TestNamespace protected override async global::System.Threading.Tasks.ValueTask FetchAsync( global::System.Collections.Generic.IReadOnlyList keys, global::System.Memory> results, - global::GreenDonut.DataLoaderFetchContext context, + global::GreenDonut.DataLoaderFetchContext context, global::System.Threading.CancellationToken ct) { var p1 = context.GetRequiredState("key"); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_State_With_Default_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_State_With_Default_MatchesSnapshot.md index 633e7aa703f..ad8c805e013 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_State_With_Default_MatchesSnapshot.md +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_BatchDataLoader_With_State_With_Default_MatchesSnapshot.md @@ -16,12 +16,12 @@ using GreenDonut; namespace TestNamespace { public interface IEntityByIdDataLoader - : global::GreenDonut.IDataLoader + : global::GreenDonut.IDataLoader { } public sealed partial class EntityByIdDataLoader - : global::GreenDonut.DataLoaderBase + : global::GreenDonut.DataLoaderBase , IEntityByIdDataLoader { private readonly global::System.IServiceProvider _services; @@ -39,7 +39,7 @@ namespace TestNamespace protected override async global::System.Threading.Tasks.ValueTask FetchAsync( global::System.Collections.Generic.IReadOnlyList keys, global::System.Memory> results, - global::GreenDonut.DataLoaderFetchContext context, + global::GreenDonut.DataLoaderFetchContext context, global::System.Threading.CancellationToken ct) { var p1 = context.GetStateOrDefault("key", "default"); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_GroupedDataLoader_ReturnsNullableStruct_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_GroupedDataLoader_ReturnsNullableStruct_MatchesSnapshot.md new file mode 100644 index 00000000000..7ae2af0e650 --- /dev/null +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.GenerateSource_GroupedDataLoader_ReturnsNullableStruct_MatchesSnapshot.md @@ -0,0 +1,101 @@ +# GenerateSource_GroupedDataLoader_ReturnsNullableStruct_MatchesSnapshot + +## GreenDonutDataLoader.735550c.g.cs + +```csharp +// + +#nullable enable +#pragma warning disable + +using System; +using System.Runtime.CompilerServices; +using Microsoft.Extensions.DependencyInjection; +using GreenDonut; + +namespace TestNamespace +{ + public interface IEntitiesByIdDataLoader + : global::GreenDonut.IDataLoader + { + } + + public sealed partial class EntitiesByIdDataLoader + : global::GreenDonut.DataLoaderBase + , IEntitiesByIdDataLoader + { + private readonly global::System.IServiceProvider _services; + + public EntitiesByIdDataLoader( + 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 keys, + global::System.Memory> results, + global::GreenDonut.DataLoaderFetchContext context, + global::System.Threading.CancellationToken ct) + { + var temp = await global::TestNamespace.TestClass.GetEntitiesByIdAsync(keys, ct).ConfigureAwait(false); + CopyResults(keys, results.Span, temp); + } + + private void CopyResults( + global::System.Collections.Generic.IReadOnlyList keys, + global::System.Span> results, + global::System.Linq.ILookup resultMap) + { + for (var i = 0; i < keys.Count; i++) + { + var key = keys[i]; + if (resultMap.Contains(key)) + { + var items = resultMap[key]; + results[i] = global::GreenDonut.Result.Resolve(global::System.Linq.Enumerable.ToArray(items)); + } + else + { + results[i] = global::GreenDonut.Result.Resolve(global::System.Array.Empty()); + } + } + } + } +} + + +``` + +## HotChocolateTypeModule.735550c.g.cs + +```csharp +// + +#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(); + return builder; + } + } +} + +``` + diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.Generate_DataLoader_NullableAnnotated_AnonymousType_AsKey_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.Generate_DataLoader_NullableAnnotated_AnonymousType_AsKey_MatchesSnapshot.md new file mode 100644 index 00000000000..3c8f9289fd3 --- /dev/null +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/DataLoaderTests.Generate_DataLoader_NullableAnnotated_AnonymousType_AsKey_MatchesSnapshot.md @@ -0,0 +1,101 @@ +# Generate_DataLoader_NullableAnnotated_AnonymousType_AsKey_MatchesSnapshot + +## GreenDonutDataLoader.735550c.g.cs + +```csharp +// + +#nullable enable +#pragma warning disable + +using System; +using System.Runtime.CompilerServices; +using Microsoft.Extensions.DependencyInjection; +using GreenDonut; + +namespace TestNamespace +{ + public interface IStuffDataLoader + : global::GreenDonut.IDataLoader<(global::TestNamespace.Id1, global::TestNamespace.Id2?), global::TestNamespace.Stuff[]> + { + } + + public sealed partial class StuffDataLoader + : global::GreenDonut.DataLoaderBase<(global::TestNamespace.Id1, global::TestNamespace.Id2?), global::TestNamespace.Stuff[]> + , IStuffDataLoader + { + private readonly global::System.IServiceProvider _services; + + public StuffDataLoader( + 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<(global::TestNamespace.Id1, global::TestNamespace.Id2?)> keys, + global::System.Memory> results, + global::GreenDonut.DataLoaderFetchContext context, + global::System.Threading.CancellationToken ct) + { + var temp = await global::TestNamespace.Dataloaders.GetStuff(keys, ct).ConfigureAwait(false); + CopyResults(keys, results.Span, temp); + } + + private void CopyResults( + global::System.Collections.Generic.IReadOnlyList<(global::TestNamespace.Id1, global::TestNamespace.Id2?)> keys, + global::System.Span> results, + global::System.Linq.ILookup<(global::TestNamespace.Id1, global::TestNamespace.Id2?), global::TestNamespace.Stuff> resultMap) + { + for (var i = 0; i < keys.Count; i++) + { + var key = keys[i]; + if (resultMap.Contains(key)) + { + var items = resultMap[key]; + results[i] = global::GreenDonut.Result.Resolve(global::System.Linq.Enumerable.ToArray(items)); + } + else + { + results[i] = global::GreenDonut.Result.Resolve(global::System.Array.Empty()); + } + } + } + } +} + + +``` + +## HotChocolateTypeModule.735550c.g.cs + +```csharp +// + +#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(); + return builder; + } + } +} + +``` +