Skip to content

Commit bfde6dc

Browse files
[Analyzer] Pass type to descriptor attributes in generated code (#9752)
1 parent 6bae505 commit bfde6dc

124 files changed

Lines changed: 175 additions & 120 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/TypeFileBuilderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void WriteInitializationBase(
9595
{
9696
Writer.WriteIndentedLine("extension.Context,");
9797
Writer.WriteIndentedLine("descriptor,");
98-
Writer.WriteIndentedLine("null,");
98+
Writer.WriteIndentedLine("typeof(global::{0}),", schemaFullTypeName);
9999

100100
var first = true;
101101
foreach (var attribute in attributes)

src/HotChocolate/Core/test/Types.Analyzers.Integration.Tests/IntegrationTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ public async Task Subscription_With_Public_Subscribe_Source_Is_Not_Exposed_As_Fi
7575
subscription.Fields.Where(f => !f.IsIntrospectionField).Select(f => f.Name).ToArray());
7676
}
7777

78+
[Fact]
79+
public async Task ObjectTypeDescriptorAttribute_Should_Receive_NonNull_Type_When_Applied_To_StaticPartial_TypeExtension()
80+
{
81+
// arrange
82+
var schema = await new ServiceCollection()
83+
.AddGraphQLServer()
84+
.AddIntegrationTestTypes()
85+
.AddPagingArguments()
86+
.BuildSchemaAsync();
87+
88+
// act
89+
var renamedType = schema.Types.GetType<ObjectType>("renamed_DescriptorAttributeProbe");
90+
91+
// assert
92+
Assert.NotNull(renamedType);
93+
}
94+
7895
[Fact]
7996
public async Task Maps_NullOrdering_From_PagingOptions_To_PagingArguments()
8097
{

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,33 @@ protected override void OnConfigure(
222222
});
223223
}
224224
}
225+
226+
public class DescriptorAttributeProbe
227+
{
228+
public string Name { get; set; } = "default";
229+
}
230+
231+
[ObjectType<DescriptorAttributeProbe>]
232+
[PrefixTypeName("renamed")]
233+
public static partial class DescriptorAttributeProbeType;
234+
235+
public sealed class PrefixTypeNameAttribute(string prefix) : ObjectTypeDescriptorAttribute
236+
{
237+
public string Prefix { get; } = prefix;
238+
239+
protected override void OnConfigure(
240+
IDescriptorContext context,
241+
IObjectTypeDescriptor descriptor,
242+
Type? type)
243+
{
244+
if (type is null)
245+
{
246+
return;
247+
}
248+
249+
var capturedPrefix = Prefix;
250+
descriptor
251+
.Extend()
252+
.OnBeforeCreate((_, cfg) => cfg.Name = $"{capturedPrefix}_{cfg.Name}");
253+
}
254+
}

src/HotChocolate/Core/test/Types.Analyzers.Integration.Tests/__snapshots__/IntegrationTests.Schema_Snapshot.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ type ProductsEdge {
207207
node: Product!
208208
}
209209

210+
type renamed_DescriptorAttributeProbe {
211+
name: String!
212+
}
213+
210214
type Shape {
211215
key: String!
212216
name: String!

src/HotChocolate/Core/test/Types.Analyzers.Integration.Tests/__snapshots__/IntegrationTests.Schema_Snapshot_Without_ConnectionName_Inference.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ type ProductEdge {
164164
node: Product!
165165
}
166166

167+
type renamed_DescriptorAttributeProbe {
168+
name: String!
169+
}
170+
167171
type Shape {
168172
key: String!
169173
name: String!

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/CollectionInferenceTests.Infer_Dictionary_As_List_Of_KeyValuePair.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace TestNamespace
6767
HotChocolate.Internal.ConfigurationHelper.ApplyConfiguration(
6868
extension.Context,
6969
descriptor,
70-
null,
70+
typeof(global::TestNamespace.Query),
7171
new global::HotChocolate.Types.QueryTypeAttribute());
7272
configuration.ConfigurationsAreApplied = true;
7373

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/LookupReturnsListTypeAnalyzerTests.Method_ArrayReturn_RaisesError.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace TestNamespace
6868
HotChocolate.Internal.ConfigurationHelper.ApplyConfiguration(
6969
extension.Context,
7070
descriptor,
71-
null,
71+
typeof(global::TestNamespace.Query),
7272
new global::HotChocolate.Types.QueryTypeAttribute());
7373
configuration.ConfigurationsAreApplied = true;
7474

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/LookupReturnsListTypeAnalyzerTests.Method_IEnumerableReturn_RaisesError.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace TestNamespace
6868
HotChocolate.Internal.ConfigurationHelper.ApplyConfiguration(
6969
extension.Context,
7070
descriptor,
71-
null,
71+
typeof(global::TestNamespace.Query),
7272
new global::HotChocolate.Types.QueryTypeAttribute());
7373
configuration.ConfigurationsAreApplied = true;
7474

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/LookupReturnsListTypeAnalyzerTests.Method_ListReturn_RaisesError.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace TestNamespace
6868
HotChocolate.Internal.ConfigurationHelper.ApplyConfiguration(
6969
extension.Context,
7070
descriptor,
71-
null,
71+
typeof(global::TestNamespace.Query),
7272
new global::HotChocolate.Types.QueryTypeAttribute());
7373
configuration.ConfigurationsAreApplied = true;
7474

src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/LookupReturnsListTypeAnalyzerTests.Method_NoLookupAttribute_NoError.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace TestNamespace
6767
HotChocolate.Internal.ConfigurationHelper.ApplyConfiguration(
6868
extension.Context,
6969
descriptor,
70-
null,
70+
typeof(global::TestNamespace.Query),
7171
new global::HotChocolate.Types.QueryTypeAttribute());
7272
configuration.ConfigurationsAreApplied = true;
7373

0 commit comments

Comments
 (0)