Skip to content

Commit ba4a210

Browse files
committed
Fixed code generation
1 parent d6f5397 commit ba4a210

File tree

7 files changed

+586
-26
lines changed

7 files changed

+586
-26
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Helpers/SymbolExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ private static bool IsPostProcessorAttribute(INamedTypeSymbol? attributeClass)
492492
return false;
493493
}
494494

495+
public static bool IsOrInheritsFrom(this ITypeSymbol? attributeClass, params string[] fullTypeName)
496+
{
497+
var current = attributeClass;
498+
499+
while (current != null)
500+
{
501+
foreach(var typeName in fullTypeName)
502+
{
503+
if (current.ToDisplayString() == typeName)
504+
{
505+
return true;
506+
}
507+
}
508+
509+
current = current.BaseType;
510+
}
511+
512+
return false;
513+
}
514+
495515
public static bool IsOrInheritsFrom(this ITypeSymbol? attributeClass, string fullTypeName)
496516
{
497517
var current = attributeClass;

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -302,32 +302,34 @@ private static Resolver CreateNodeResolver(
302302
file static class Extensions
303303
{
304304
public static bool IsNodeResolver(this IMethodSymbol methodSymbol)
305-
=> methodSymbol
306-
.GetAttributes()
307-
.Any(t => t.AttributeClass?.ToDisplayString().Equals(NodeResolverAttribute, Ordinal) ?? false);
308-
309-
public static bool Skip(this IMethodSymbol methodSymbol)
310-
=> methodSymbol
311-
.GetAttributes()
312-
.Any(t =>
305+
{
306+
foreach (var attribute in methodSymbol.GetAttributes())
307+
{
308+
if (attribute.AttributeClass.IsOrInheritsFrom(NodeResolverAttribute))
313309
{
314-
var name = t.AttributeClass?.ToDisplayString();
310+
return true;
311+
}
312+
}
315313

316-
if (name is null)
317-
{
318-
return false;
319-
}
314+
return false;
315+
}
320316

321-
if (name.Equals(DataLoaderAttribute, Ordinal) ||
322-
name.Equals(QueryAttribute, Ordinal) ||
323-
name.Equals(MutationAttribute, Ordinal) ||
324-
name.Equals(SubscriptionAttribute, Ordinal))
325-
{
326-
return true;
327-
}
317+
public static bool Skip(this IMethodSymbol methodSymbol)
318+
{
319+
foreach (var attribute in methodSymbol.GetAttributes())
320+
{
321+
if (attribute.AttributeClass.IsOrInheritsFrom(
322+
DataLoaderAttribute,
323+
QueryAttribute,
324+
MutationAttribute,
325+
SubscriptionAttribute))
326+
{
327+
return true;
328+
}
329+
}
328330

329-
return false;
330-
});
331+
return false;
332+
}
331333

332334
public static ImmutableArray<MemberBinding> GetMemberBindings(this ISymbol member)
333335
{

src/HotChocolate/Core/src/Types/Types/Descriptors/TypeConfiguration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ internal void Apply<TDescriptor>(
8080
{
8181
foreach (var item in list)
8282
{
83-
if (item is Func<Action<TDescriptor>> factory)
83+
if (item is Action<TDescriptor> configure)
8484
{
85-
factory()(descriptor);
85+
configure(descriptor);
8686
}
8787
}
8888
}
@@ -100,9 +100,9 @@ internal void Apply<TDescriptor>(
100100
{
101101
foreach (var item in list)
102102
{
103-
if (item is Func<Action<TDescriptor>> factory)
103+
if (item is Action<TDescriptor> configure)
104104
{
105-
factory()(descriptor);
105+
configure(descriptor);
106106
}
107107
}
108108
}

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,76 @@ internal class Test;
169169
internal class Entity;
170170
""").MatchMarkdownAsync();
171171
}
172+
173+
[Fact]
174+
public async Task Ensure_Entity_Becomes_Node()
175+
{
176+
await TestHelper.GetGeneratedSourceSnapshot(
177+
"""
178+
using HotChocolate;
179+
using HotChocolate.Types;
180+
using HotChocolate.Types.Relay;
181+
using System.Threading.Tasks;
182+
183+
namespace TestNamespace;
184+
185+
[QueryType]
186+
internal static partial class Query
187+
{
188+
public static Task<Test?> GetTestById([ID<Test>] int id)
189+
=> Task.FromResult<Test?>(null);
190+
}
191+
192+
[ObjectType<Test>]
193+
internal static partial class TestType
194+
{
195+
[NodeResolver]
196+
public static Task<Test?> GetTest(int id)
197+
=> Task.FromResult<Test?>(null);
198+
}
199+
200+
internal class Test
201+
{
202+
public int Id { get; set; }
203+
204+
public string Name { get; set; }
205+
}
206+
""").MatchMarkdownAsync();
207+
}
208+
209+
[Fact]
210+
public async Task Ensure_Entity_Becomes_Node_With_Query_Node_Resolver()
211+
{
212+
await TestHelper.GetGeneratedSourceSnapshot(
213+
"""
214+
using HotChocolate;
215+
using HotChocolate.Types;
216+
using HotChocolate.Types.Relay;
217+
using System.Threading.Tasks;
218+
219+
namespace TestNamespace;
220+
221+
[QueryType]
222+
internal static partial class Query
223+
{
224+
[NodeResolver]
225+
public static Task<Test?> GetTestById(int id)
226+
=> Task.FromResult<Test?>(null);
227+
}
228+
229+
[ObjectType<Test>]
230+
internal static partial class TestType
231+
{
232+
public static Task<Test?> GetTest(int id)
233+
=> Task.FromResult<Test?>(null);
234+
}
235+
236+
internal class Test
237+
{
238+
public int Id { get; set; }
239+
240+
public string Name { get; set; }
241+
}
242+
""").MatchMarkdownAsync();
243+
}
172244
}

0 commit comments

Comments
 (0)