|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | + |
| 3 | +namespace HotChocolate.Types; |
| 4 | + |
| 5 | +public class ConnectionTests |
| 6 | +{ |
| 7 | + [Fact] |
| 8 | + public async Task Build_Schema_Should_Fail_When_Same_Connection_Name_In_Two_Assemblies() |
| 9 | + { |
| 10 | + // arrange |
| 11 | + // Both assemblies source-generate a connection named "AuthorConnection" (and an |
| 12 | + // accompanying "AuthorEdge"). The second assembly references the first, but each |
| 13 | + // assembly bakes its own distinct CLR connection and edge ObjectType and registers |
| 14 | + // them through its own generated AddXxxTypes() extension method, so the runtime type |
| 15 | + // registry sees two different CLR types claiming the same GraphQL type name. |
| 16 | + const string assemblyOneSource = |
| 17 | + """ |
| 18 | + using System.Collections.Generic; |
| 19 | + using System.Threading; |
| 20 | + using System.Threading.Tasks; |
| 21 | + using HotChocolate; |
| 22 | + using HotChocolate.Types; |
| 23 | + using HotChocolate.Types.Pagination; |
| 24 | +
|
| 25 | + [assembly: Module("ConnectionAssemblyOneTypes")] |
| 26 | +
|
| 27 | + namespace Demo.CrossAssembly; |
| 28 | +
|
| 29 | + public sealed class Author |
| 30 | + { |
| 31 | + public int Id { get; set; } |
| 32 | +
|
| 33 | + public string Name { get; set; } = string.Empty; |
| 34 | + } |
| 35 | +
|
| 36 | + public class AuthorConnection : ConnectionBase<Author, AuthorEdge, ConnectionPageInfo> |
| 37 | + { |
| 38 | + public override IReadOnlyList<AuthorEdge> Edges => default!; |
| 39 | +
|
| 40 | + public IReadOnlyList<Author> Nodes => default!; |
| 41 | +
|
| 42 | + public override ConnectionPageInfo PageInfo => default!; |
| 43 | +
|
| 44 | + public int TotalCount => 0; |
| 45 | + } |
| 46 | +
|
| 47 | + public class AuthorEdge : IEdge<Author> |
| 48 | + { |
| 49 | + public Author Node => default!; |
| 50 | +
|
| 51 | + object? IEdge.Node => Node; |
| 52 | +
|
| 53 | + public string Cursor => default!; |
| 54 | + } |
| 55 | +
|
| 56 | + [QueryType] |
| 57 | + public static partial class ConnectionAssemblyOneQuery |
| 58 | + { |
| 59 | + public static Task<AuthorConnection> GetAuthors( |
| 60 | + GreenDonut.Data.PagingArguments pagingArgs, |
| 61 | + CancellationToken ct) => default!; |
| 62 | + } |
| 63 | + """; |
| 64 | + |
| 65 | + const string assemblyTwoSource = |
| 66 | + """ |
| 67 | + using System.Collections.Generic; |
| 68 | + using System.Threading; |
| 69 | + using System.Threading.Tasks; |
| 70 | + using HotChocolate; |
| 71 | + using HotChocolate.Types; |
| 72 | + using HotChocolate.Types.Pagination; |
| 73 | + using Demo.CrossAssembly; |
| 74 | +
|
| 75 | + [assembly: Module("ConnectionAssemblyTwoTypes")] |
| 76 | +
|
| 77 | + namespace Demo.CrossAssembly.Two; |
| 78 | +
|
| 79 | + public class AuthorConnection : ConnectionBase<Author, AuthorEdge, ConnectionPageInfo> |
| 80 | + { |
| 81 | + public override IReadOnlyList<AuthorEdge> Edges => default!; |
| 82 | +
|
| 83 | + public IReadOnlyList<Author> Nodes => default!; |
| 84 | +
|
| 85 | + public override ConnectionPageInfo PageInfo => default!; |
| 86 | +
|
| 87 | + public int TotalCount => 0; |
| 88 | + } |
| 89 | +
|
| 90 | + public class AuthorEdge : IEdge<Author> |
| 91 | + { |
| 92 | + public Author Node => default!; |
| 93 | +
|
| 94 | + object? IEdge.Node => Node; |
| 95 | +
|
| 96 | + public string Cursor => default!; |
| 97 | + } |
| 98 | +
|
| 99 | + [QueryType] |
| 100 | + public static partial class ConnectionAssemblyTwoQuery |
| 101 | + { |
| 102 | + public static Task<AuthorConnection> GetMoreAuthors( |
| 103 | + GreenDonut.Data.PagingArguments pagingArgs, |
| 104 | + CancellationToken ct) => default!; |
| 105 | + } |
| 106 | + """; |
| 107 | + |
| 108 | + var assemblies = new GeneratorAssembly[] |
| 109 | + { |
| 110 | + new("Demo.ConnectionAssemblyOne", assemblyOneSource), |
| 111 | + new( |
| 112 | + "Demo.ConnectionAssemblyTwo", |
| 113 | + [assemblyTwoSource], |
| 114 | + References: ["Demo.ConnectionAssemblyOne"]) |
| 115 | + }; |
| 116 | + |
| 117 | + // act |
| 118 | + async Task BuildSchema() |
| 119 | + => await GeneratorTestServer.CreateSchemaAsync( |
| 120 | + assemblies, |
| 121 | + configure: static b => b.AddPagingArguments(), |
| 122 | + disableDefaultSecurity: false); |
| 123 | + |
| 124 | + var exception = await Assert.ThrowsAnyAsync<Exception>(BuildSchema); |
| 125 | + |
| 126 | + // assert |
| 127 | + // Each assembly bakes its own distinct CLR companion for the connection and edge, so |
| 128 | + // the type registry sees two different types claiming the same GraphQL name and fails. |
| 129 | + // This documents the current cross-assembly limitation of the source generator. |
| 130 | + Assert.Contains("already registered", exception.ToString()); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task Build_Schema_Should_Reuse_Single_Companion_When_Same_Connection_Name_In_One_Assembly() |
| 135 | + { |
| 136 | + // arrange |
| 137 | + // A single compilation pages the same AuthorConnection from two different fields. |
| 138 | + // The generator runs once and de-duplicates the companion, so the runtime only ever |
| 139 | + // sees one "AuthorConnection" type and the schema builds without a collision. |
| 140 | + const string source = |
| 141 | + """ |
| 142 | + using System.Collections.Generic; |
| 143 | + using System.Threading; |
| 144 | + using System.Threading.Tasks; |
| 145 | + using HotChocolate; |
| 146 | + using HotChocolate.Types; |
| 147 | + using HotChocolate.Types.Pagination; |
| 148 | +
|
| 149 | + namespace Demo.SingleAssembly; |
| 150 | +
|
| 151 | + public sealed class Author |
| 152 | + { |
| 153 | + public int Id { get; set; } |
| 154 | +
|
| 155 | + public string Name { get; set; } = string.Empty; |
| 156 | + } |
| 157 | +
|
| 158 | + public class AuthorConnection : ConnectionBase<Author, AuthorEdge, ConnectionPageInfo> |
| 159 | + { |
| 160 | + public override IReadOnlyList<AuthorEdge> Edges => default!; |
| 161 | +
|
| 162 | + public IReadOnlyList<Author> Nodes => default!; |
| 163 | +
|
| 164 | + public override ConnectionPageInfo PageInfo => default!; |
| 165 | +
|
| 166 | + public int TotalCount => 0; |
| 167 | + } |
| 168 | +
|
| 169 | + public class AuthorEdge : IEdge<Author> |
| 170 | + { |
| 171 | + public Author Node => default!; |
| 172 | +
|
| 173 | + object? IEdge.Node => Node; |
| 174 | +
|
| 175 | + public string Cursor => default!; |
| 176 | + } |
| 177 | +
|
| 178 | + [QueryType] |
| 179 | + public static partial class Query |
| 180 | + { |
| 181 | + public static Task<AuthorConnection> GetAuthors( |
| 182 | + GreenDonut.Data.PagingArguments pagingArgs, |
| 183 | + CancellationToken ct) => default!; |
| 184 | +
|
| 185 | + public static Task<AuthorConnection> GetMoreAuthors( |
| 186 | + GreenDonut.Data.PagingArguments pagingArgs, |
| 187 | + CancellationToken ct) => default!; |
| 188 | + } |
| 189 | + """; |
| 190 | + |
| 191 | + // act |
| 192 | + var schema = await GeneratorTestServer.CreateSchemaAsync( |
| 193 | + source, |
| 194 | + configure: static b => b.AddPagingArguments(), |
| 195 | + disableDefaultSecurity: false); |
| 196 | + |
| 197 | + // assert |
| 198 | + // Both fields share the single generated AuthorConnection companion. |
| 199 | + var authors = schema.QueryType.Fields["authors"]; |
| 200 | + var moreAuthors = schema.QueryType.Fields["moreAuthors"]; |
| 201 | + Assert.Equal("AuthorConnection", authors.Type.NamedType().Name); |
| 202 | + Assert.Equal("AuthorConnection", moreAuthors.Type.NamedType().Name); |
| 203 | + Assert.Single(schema.Types, t => t.Name == "AuthorConnection"); |
| 204 | + } |
| 205 | +} |
0 commit comments