Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that type headers are only written once. #7013

Merged
merged 1 commit into from
Mar 25, 2024
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 @@ -175,7 +175,8 @@ private static void WriteConfiguration(

case ObjectTypeExtensionInfo objectTypeExtension:
if ((module.Options & ModuleOptions.RegisterTypes) ==
ModuleOptions.RegisterTypes)
ModuleOptions.RegisterTypes &&
objectTypeExtension.Diagnostics.Length == 0)
{
generator.WriteRegisterObjectTypeExtension(
objectTypeExtension.RuntimeType.ToFullyQualified(),
Expand Down
49 changes: 24 additions & 25 deletions src/HotChocolate/Core/src/Types.Analyzers/TypesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class TypesGenerator : IIncrementalGenerator
{
private static readonly ISyntaxInspector[] _inspectors =
[
new ModuleInspector(),
new ObjectTypeExtensionInfoInspector(),
];

Expand Down Expand Up @@ -68,41 +67,41 @@ private static void Execute(
return;
}

var module = syntaxInfos.GetModuleInfo(compilation.AssemblyName, out var defaultModule);

// if there is only the module info we do not need to generate a module.
if (!defaultModule && syntaxInfos.Length == 1)
{
return;
}

var sb = StringBuilderPool.Get();
var first = true;

foreach (var syntaxInfo in syntaxInfos)
{
if (syntaxInfo is ObjectTypeExtensionInfo objectTypeExtension)
if (syntaxInfo is not ObjectTypeExtensionInfo objectTypeExtension)
{
if (objectTypeExtension.Diagnostics.Length > 0)
continue;
}

if (objectTypeExtension.Diagnostics.Length > 0)
{
foreach (var diagnostic in objectTypeExtension.Diagnostics)
{
foreach (var diagnostic in objectTypeExtension.Diagnostics)
{
context.ReportDiagnostic(diagnostic);
}
continue;
context.ReportDiagnostic(diagnostic);
}
continue;
}

var generator = new ObjectTypeExtensionSyntaxGenerator(
sb,
objectTypeExtension.Type.ContainingNamespace.ToDisplayString());
var generator = new ObjectTypeExtensionSyntaxGenerator(
sb,
objectTypeExtension.Type.ContainingNamespace.ToDisplayString());

if (first)
{
generator.WriterHeader();
generator.WriteBeginNamespace();
generator.WriteBeginClass(objectTypeExtension.Type.Name);
generator.WriteInitializeMethod(objectTypeExtension);
generator.WriteConfigureMethod(objectTypeExtension);
generator.WriteEndClass();
generator.WriteEndNamespace();
first = false;
}

generator.WriteBeginNamespace();
generator.WriteBeginClass(objectTypeExtension.Type.Name);
generator.WriteInitializeMethod(objectTypeExtension);
generator.WriteConfigureMethod(objectTypeExtension);
generator.WriteEndClass();
generator.WriteEndNamespace();
}

context.AddSource(WellKnownFileNames.TypesFile, sb.ToString());
Expand Down
13 changes: 13 additions & 0 deletions src/HotChocolate/Core/test/Types.Analyzers.Tests/BookType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ protected override void Configure(IObjectTypeDescriptor<Book> descriptor)
public class Book
{
public string? Title { get; set; }

public Author? Author => null;
}

public class Author
{
public string Name { get; set; } = default!;
}

[ObjectType<Author>]
public static partial class AuthorNode
{
public static string Address([Parent] Author author) => "something";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface Node {
id: ID!
}

type Author {
address: String!
name: String!
}

type Mutation {
bar: String!
doSomething: String!
Expand Down Expand Up @@ -44,6 +49,7 @@ type Query {

type SomeBook {
title: String
author: Author
}

type Subscription {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
<Description>Contains the Hot Chocolate GraphQL parser and lexer. Also included are syntax visitor and syntax rewriter base classes.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Remove="AST\**" />
<EmbeddedResource Remove="AST\**" />
<None Remove="AST\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Language.SyntaxTree\HotChocolate.Language.SyntaxTree.csproj" />
<ProjectReference Include="..\Language.Utf8\HotChocolate.Language.Utf8.csproj" />
Expand Down
Loading