Skip to content

Commit 9ed4805

Browse files
authored
Fix: Allow internal members in ObjectType and InterfaceType source generators (#8621)
1 parent 1143b21 commit 9ed4805

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Inspectors/InterfaceTypeInfoInspector.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public bool TryHandle(GeneratorSyntaxContext context, [NotNullWhen(true)] out Sy
4747

4848
foreach (var member in members)
4949
{
50-
if (member.DeclaredAccessibility is Accessibility.Public or Accessibility.Internal)
50+
if (member.DeclaredAccessibility is
51+
Accessibility.Public or
52+
Accessibility.Internal or
53+
Accessibility.ProtectedAndInternal)
5154
{
5255
if (member is IMethodSymbol { MethodKind: MethodKind.Ordinary } methodSymbol)
5356
{

src/HotChocolate/Core/src/Types.Analyzers/Inspectors/ObjectTypeInspector.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public bool TryHandle(GeneratorSyntaxContext context, [NotNullWhen(true)] out Sy
5858

5959
foreach (var member in members)
6060
{
61-
if (member.DeclaredAccessibility is Accessibility.Public && !member.IsIgnored())
61+
if (member.DeclaredAccessibility is
62+
Accessibility.Public or
63+
Accessibility.Internal or
64+
Accessibility.ProtectedAndInternal
65+
&& !member.IsIgnored())
6266
{
6367
if (member is IMethodSymbol { MethodKind: MethodKind.Ordinary } methodSymbol)
6468
{

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,33 @@ internal class Test
241241
}
242242
""").MatchMarkdownAsync();
243243
}
244+
245+
[Fact]
246+
public async Task Internal_NodeResolver_Should_Generate_Source()
247+
{
248+
await TestHelper.GetGeneratedSourceSnapshot(
249+
"""
250+
using HotChocolate;
251+
using HotChocolate.Types;
252+
using HotChocolate.Types.Relay;
253+
using System.Threading.Tasks;
254+
255+
namespace TestNamespace;
256+
257+
[ObjectType<Test>]
258+
internal static partial class TestType
259+
{
260+
[NodeResolver]
261+
internal static Task<Test?> GetTestByIdAsync(int id)
262+
=> Task.FromResult<Test?>(null);
263+
}
264+
265+
internal class Test
266+
{
267+
public int Id { get; set; }
268+
269+
public string Name { get; set; }
270+
}
271+
""").MatchMarkdownAsync();
272+
}
244273
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Internal_NodeResolver_Should_Generate_Source
2+
3+
## HotChocolateTypeModule.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 HotChocolate;
14+
using HotChocolate.Types;
15+
using HotChocolate.Execution.Configuration;
16+
17+
namespace Microsoft.Extensions.DependencyInjection
18+
{
19+
public static partial class TestsTypesRequestExecutorBuilderExtensions
20+
{
21+
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
22+
{
23+
builder.ConfigureDescriptorContext(ctx => ctx.TypeConfiguration.TryAdd<global::TestNamespace.Test>(
24+
"Tests::TestNamespace.TestType",
25+
() => global::TestNamespace.TestType.Initialize));
26+
builder.AddType<ObjectType<global::TestNamespace.Test>>();
27+
return builder;
28+
}
29+
}
30+
}
31+
32+
```
33+
34+
## TestType.WaAdMHmlGJHjtEI4nqY7WA.hc.g.cs
35+
36+
```csharp
37+
// <auto-generated/>
38+
39+
#nullable enable
40+
#pragma warning disable
41+
42+
using System;
43+
using System.Runtime.CompilerServices;
44+
using HotChocolate;
45+
using HotChocolate.Types;
46+
using HotChocolate.Execution.Configuration;
47+
using Microsoft.Extensions.DependencyInjection;
48+
using HotChocolate.Internal;
49+
50+
namespace TestNamespace
51+
{
52+
internal static partial class TestType
53+
{
54+
internal static void Initialize(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Test> descriptor)
55+
{
56+
var thisType = typeof(global::TestNamespace.TestType);
57+
var bindingResolver = descriptor.Extend().Context.ParameterBindingResolver;
58+
var resolvers = new __Resolvers();
59+
60+
descriptor
61+
.ImplementsNode()
62+
.ResolveNode(resolvers.GetTestByIdAsync().Resolver!);
63+
64+
Configure(descriptor);
65+
}
66+
67+
static partial void Configure(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Test> descriptor);
68+
69+
private sealed class __Resolvers
70+
{
71+
public HotChocolate.Resolvers.FieldResolverDelegates GetTestByIdAsync()
72+
=> new global::HotChocolate.Resolvers.FieldResolverDelegates(resolver: GetTestByIdAsync);
73+
74+
private async global::System.Threading.Tasks.ValueTask<global::System.Object?> GetTestByIdAsync(global::HotChocolate.Resolvers.IResolverContext context)
75+
{
76+
var args0 = context.GetLocalState<int>(global::HotChocolate.WellKnownContextData.InternalId);
77+
var result = await global::TestNamespace.TestType.GetTestByIdAsync(args0);
78+
return result;
79+
}
80+
}
81+
}
82+
}
83+
84+
85+
```
86+

0 commit comments

Comments
 (0)