Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ private static bool IsBatchResolverMethod(
if (fullName is "System.Collections.Generic.List<T>"
or "System.Collections.Generic.IList<T>"
or "System.Collections.Generic.IReadOnlyList<T>"
or "System.Collections.Generic.ICollection<T>"
or "System.Collections.Generic.IEnumerable<T>")
or "System.Collections.Immutable.ImmutableArray<T>")
{
return namedType.TypeArguments[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,104 @@ public class Brand
enableAnalyzers: true).MatchMarkdownAsync();
}

[Fact]
public async Task ParentAttribute_BatchResolver_ImmutableArrayOfParentType_NoError()
{
await TestHelper.GetGeneratedSourceSnapshot(
["""
using HotChocolate;
using HotChocolate.Types;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace TestNamespace;

[ObjectType<Product>]
public static partial class ProductNode
{
[BatchResolver]
public static List<string> GetDisplayName(
[Parent] ImmutableArray<Product> products)
=> products.Select(p => p.Name).ToList();
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
"""],
enableAnalyzers: true).MatchMarkdownAsync();
}

[Fact]
public async Task ParentAttribute_BatchResolver_ImmutableArrayOfWrongType_RaisesError()
{
await TestHelper.GetGeneratedSourceSnapshot(
["""
using HotChocolate;
using HotChocolate.Types;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace TestNamespace;

[ObjectType<Product>]
public static partial class ProductNode
{
[BatchResolver]
public static List<string> GetDisplayName(
[Parent] ImmutableArray<Brand> brands)
=> brands.Select(b => b.Name).ToList();
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Brand
{
public int Id { get; set; }
public string Name { get; set; }
}
"""],
enableAnalyzers: true).MatchMarkdownAsync();
}

[Fact]
public async Task ParentAttribute_BatchResolver_ArrayOfParentType_NoError()
{
await TestHelper.GetGeneratedSourceSnapshot(
["""
using HotChocolate;
using HotChocolate.Types;
using System.Collections.Generic;
using System.Linq;

namespace TestNamespace;

[ObjectType<Product>]
public static partial class ProductNode
{
[BatchResolver]
public static List<string> GetDisplayName(
[Parent] Product[] products)
=> products.Select(p => p.Name).ToList();
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
"""],
enableAnalyzers: true).MatchMarkdownAsync();
}

[Fact]
public async Task ParentAttribute_WithRequires_TypeMismatch_RaisesError()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# ParentAttribute_BatchResolver_ArrayOfParentType_NoError

## 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.ConfigureDescriptorContext(ctx => ctx.TypeConfiguration.TryAdd<global::TestNamespace.Product>(
"Tests::TestNamespace.ProductNode",
() => global::TestNamespace.ProductNode.Initialize));
builder.AddType<ObjectType<global::TestNamespace.Product>>();
return builder;
}
}
}

```

## ProductNode.WaAdMHmlGJHjtEI4nqY7WA.hc.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;
using Microsoft.Extensions.DependencyInjection;
using HotChocolate.Internal;

namespace TestNamespace
{
public static partial class ProductNode
{
internal static void Initialize(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Product> descriptor)
{
var extension = descriptor.Extend();
var configuration = extension.Configuration;
var thisType = typeof(global::TestNamespace.ProductNode);
var bindingResolver = extension.Context.ParameterBindingResolver;
var resolvers = new __Resolvers();

var naming = descriptor.Extend().Context.Naming;

descriptor
.Field(naming.GetMemberName("DisplayName", global::HotChocolate.Types.MemberKind.ObjectField))
.ExtendWith(static (field, context) =>
{
var configuration = field.Configuration;
var typeInspector = field.Context.TypeInspector;
var bindingResolver = field.Context.ParameterBindingResolver;
var naming = field.Context.Naming;

configuration.Type = global::HotChocolate.Types.Descriptors.TypeReference.Create(
typeInspector.GetTypeRef(typeof(string), HotChocolate.Types.TypeContext.Output),
new global::HotChocolate.Language.NonNullTypeNode(new global::HotChocolate.Language.NamedTypeNode("string")));
configuration.ResultType = typeof(string);

configuration.SetSourceGeneratorFlags();
configuration.SetBatchResolverFlags();

configuration.BatchResolver = context.Resolvers.GetDisplayName();
},
(Resolvers: resolvers, ThisType: thisType));

Configure(descriptor);
}

static partial void Configure(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Product> descriptor);

private sealed class __Resolvers
{
public HotChocolate.Resolvers.BatchFieldDelegate GetDisplayName()
=> GetDisplayName;

private global::System.Threading.Tasks.ValueTask GetDisplayName(global::System.Collections.Immutable.ImmutableArray<HotChocolate.Resolvers.IMiddlewareContext> contexts)
{
var args0 = new global::TestNamespace.Product[](contexts.Length);

for (var i = 0; i < contexts.Length; i++)
{
args0.Add(contexts[i].Parent<global::TestNamespace.Product>());
}

var result = global::TestNamespace.ProductNode.GetDisplayName(args0);

if (result is global::System.Collections.IList list)
{
for (var i = 0; i < contexts.Length; i++)
{
contexts[i].Result = i < list.Count ? list[i] : null;
}
}
return default;
}
}
}
}


```

## Assembly Emit Diagnostics

```json
[
{
"Id": "CS1586",
"Title": "",
"Severity": "Error",
"WarningLevel": 0,
"Location": "ProductNode.WaAdMHmlGJHjtEI4nqY7WA.hc.g.cs: (60,61)-(60,63)",
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS1586)",
"MessageFormat": "Array creation must have array size or array initializer",
"Message": "Array creation must have array size or array initializer",
"Category": "Compiler",
"CustomTags": [
"Compiler",
"Telemetry",
"NotConfigurable"
]
}
]
```
Loading
Loading