Skip to content

Commit a09e355

Browse files
Add reproduction test
1 parent e4b2ea1 commit a09e355

8 files changed

Lines changed: 898 additions & 205 deletions
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
namespace HotChocolate.Types;
2+
3+
public class ConnectionTests
4+
{
5+
[Fact]
6+
public async Task Build_Schema_Should_Reuse_Single_Connection_When_Same_Type_Paged_Twice_In_One_Assembly()
7+
{
8+
// arrange
9+
const string source =
10+
"""
11+
using System.Linq;
12+
using HotChocolate.Types;
13+
14+
namespace Demo.SingleAssembly;
15+
16+
public sealed class Author
17+
{
18+
public int Id { get; set; }
19+
20+
public string Name { get; set; } = string.Empty;
21+
}
22+
23+
[QueryType]
24+
public static partial class Query
25+
{
26+
[UsePaging(InferConnectionNameFromField = false)]
27+
public static IQueryable<Author> GetAuthors() => default!;
28+
29+
[UsePaging(InferConnectionNameFromField = false)]
30+
public static IQueryable<Author> GetMoreAuthors() => default!;
31+
}
32+
""";
33+
34+
// act
35+
var schema = await GeneratorTestServer.CreateSchemaAsync(
36+
source,
37+
disableDefaultSecurity: false);
38+
39+
// assert
40+
schema.MatchSnapshot();
41+
}
42+
43+
[Fact]
44+
public async Task Build_Schema_Should_Reuse_Single_Connection_When_Same_Type_Paged_Across_Two_Assemblies()
45+
{
46+
// arrange
47+
const string authorAssemblySource =
48+
"""
49+
namespace Demo.CrossAssembly;
50+
51+
public sealed class Author
52+
{
53+
public int Id { get; set; }
54+
55+
public string Name { get; set; } = string.Empty;
56+
}
57+
""";
58+
59+
const string assemblyOneSource =
60+
"""
61+
using System.Linq;
62+
using HotChocolate.Types;
63+
using Demo.CrossAssembly;
64+
65+
namespace Demo.CrossAssembly.One;
66+
67+
[QueryType]
68+
public static partial class AssemblyOneQuery
69+
{
70+
[UsePaging(InferConnectionNameFromField = false)]
71+
public static IQueryable<Author> GetAuthors() => default!;
72+
}
73+
""";
74+
75+
const string assemblyTwoSource =
76+
"""
77+
using System.Linq;
78+
using HotChocolate.Types;
79+
using Demo.CrossAssembly;
80+
81+
namespace Demo.CrossAssembly.Two;
82+
83+
[QueryType]
84+
public static partial class AssemblyTwoQuery
85+
{
86+
[UsePaging(InferConnectionNameFromField = false)]
87+
public static IQueryable<Author> GetMoreAuthors() => default!;
88+
}
89+
""";
90+
91+
var assemblies = new GeneratorAssembly[]
92+
{
93+
new(
94+
"Demo.ConnectionAssemblyAuthor",
95+
[authorAssemblySource],
96+
References: [],
97+
Register: false),
98+
new(
99+
"Demo.ConnectionAssemblyOne",
100+
[assemblyOneSource],
101+
References: ["Demo.ConnectionAssemblyAuthor"]),
102+
new(
103+
"Demo.ConnectionAssemblyTwo",
104+
[assemblyTwoSource],
105+
References: ["Demo.ConnectionAssemblyAuthor"])
106+
};
107+
108+
// act
109+
var schema = await GeneratorTestServer.CreateSchemaAsync(
110+
assemblies,
111+
disableDefaultSecurity: false);
112+
113+
// assert
114+
schema.MatchSnapshot();
115+
}
116+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using Basic.Reference.Assemblies;
2+
using GreenDonut;
3+
using GreenDonut.Data;
4+
using HotChocolate.Data.Filters;
5+
using HotChocolate.Execution;
6+
using HotChocolate.Execution.Configuration;
7+
using HotChocolate.Execution.Processing;
8+
using HotChocolate.Features;
9+
using HotChocolate.Language;
10+
using HotChocolate.Language.Visitors;
11+
using HotChocolate.Types.Pagination;
12+
using Microsoft.AspNetCore.Builder;
13+
using Microsoft.CodeAnalysis;
14+
using Microsoft.Extensions.DependencyInjection;
15+
16+
namespace HotChocolate.Types;
17+
18+
/// <summary>
19+
/// Provides the metadata references that mirror the HotChocolate package graph an
20+
/// application using the source generator would compile against. The list is shared by
21+
/// every in-memory compilation in this test project so that the generated registration
22+
/// code, the HotChocolate runtime, and the test process all bind to the same loaded
23+
/// assemblies (a requirement for type identity to hold across assembly load contexts).
24+
/// </summary>
25+
internal static class GeneratorReferences
26+
{
27+
/// <summary>
28+
/// Gets the metadata references for an in-memory HotChocolate compilation. The list
29+
/// combines the framework reference assemblies for the active target framework with
30+
/// metadata references to the loaded HotChocolate assemblies.
31+
/// </summary>
32+
public static IReadOnlyList<PortableExecutableReference> All { get; } =
33+
[
34+
#if NET8_0
35+
.. Net80.References.All,
36+
#elif NET9_0
37+
.. Net90.References.All,
38+
#elif NET10_0
39+
.. Net100.References.All,
40+
#endif
41+
// HotChocolate.Primitives
42+
MetadataReference.CreateFromFile(typeof(ITypeSystemMember).Assembly.Location),
43+
44+
// HotChocolate.Execution
45+
MetadataReference.CreateFromFile(typeof(RequestDelegate).Assembly.Location),
46+
47+
// HotChocolate.Execution.Abstractions
48+
MetadataReference.CreateFromFile(typeof(RequestContext).Assembly.Location),
49+
50+
// HotChocolate.Execution.Processing
51+
MetadataReference.CreateFromFile(typeof(HotChocolateExecutionSelectionExtensions).Assembly.Location),
52+
53+
// HotChocolate.Execution.Abstractions
54+
MetadataReference.CreateFromFile(typeof(IRequestExecutorBuilder).Assembly.Location),
55+
56+
// HotChocolate.Execution.Operation.Abstractions
57+
MetadataReference.CreateFromFile(typeof(ISelection).Assembly.Location),
58+
59+
// HotChocolate.Types
60+
MetadataReference.CreateFromFile(typeof(ObjectTypeAttribute).Assembly.Location),
61+
MetadataReference.CreateFromFile(typeof(QueryTypeAttribute).Assembly.Location),
62+
MetadataReference.CreateFromFile(typeof(Connection).Assembly.Location),
63+
MetadataReference.CreateFromFile(typeof(PageConnection<>).Assembly.Location),
64+
65+
// HotChocolate.Types.Abstractions
66+
MetadataReference.CreateFromFile(typeof(ISchemaDefinition).Assembly.Location),
67+
68+
// HotChocolate.Features
69+
MetadataReference.CreateFromFile(typeof(IFeatureProvider).Assembly.Location),
70+
71+
// HotChocolate.Language
72+
MetadataReference.CreateFromFile(typeof(OperationType).Assembly.Location),
73+
74+
// HotChocolate.Language.Utf8
75+
MetadataReference.CreateFromFile(typeof(ParserOptions).Assembly.Location),
76+
77+
// HotChocolate.Language.Visitors
78+
MetadataReference.CreateFromFile(typeof(SyntaxVisitor).Assembly.Location),
79+
80+
// HotChocolate.Abstractions
81+
MetadataReference.CreateFromFile(typeof(ParentAttribute).Assembly.Location),
82+
83+
// HotChocolate.AspNetCore
84+
MetadataReference.CreateFromFile(
85+
typeof(HotChocolateAspNetCoreServiceCollectionExtensions).Assembly.Location),
86+
87+
// GreenDonut
88+
MetadataReference.CreateFromFile(typeof(DataLoaderBase<,>).Assembly.Location),
89+
MetadataReference.CreateFromFile(typeof(IDataLoader).Assembly.Location),
90+
91+
// GreenDonut.Data
92+
MetadataReference.CreateFromFile(typeof(PagingArguments).Assembly.Location),
93+
MetadataReference.CreateFromFile(typeof(IPredicateBuilder).Assembly.Location),
94+
MetadataReference.CreateFromFile(typeof(DefaultPredicateBuilder).Assembly.Location),
95+
96+
// HotChocolate.Data
97+
MetadataReference.CreateFromFile(typeof(IFilterContext).Assembly.Location),
98+
99+
// Microsoft.AspNetCore
100+
MetadataReference.CreateFromFile(typeof(WebApplication).Assembly.Location),
101+
102+
// Microsoft.Extensions.DependencyInjection.Abstractions
103+
MetadataReference.CreateFromFile(typeof(IServiceCollection).Assembly.Location),
104+
105+
// Microsoft.AspNetCore.Authorization
106+
MetadataReference.CreateFromFile(typeof(Microsoft.AspNetCore.Authorization.AuthorizeAttribute).Assembly.Location),
107+
108+
// HotChocolate.Authorization
109+
MetadataReference.CreateFromFile(typeof(Authorization.AuthorizeAttribute).Assembly.Location),
110+
111+
// HotChocolate.Types.OffsetPagination
112+
MetadataReference.CreateFromFile(typeof(UseOffsetPagingAttribute).Assembly.Location)
113+
];
114+
}

0 commit comments

Comments
 (0)