-
-
Notifications
You must be signed in to change notification settings - Fork 800
Expand file tree
/
Copy pathIssue6291ReproTests.cs
More file actions
242 lines (211 loc) · 7.32 KB
/
Issue6291ReproTests.cs
File metadata and controls
242 lines (211 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System.Text.Json;
using HotChocolate.Execution;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using Squadron;
namespace HotChocolate.Data.MongoDb.Projections;
public sealed class Issue6291ReproTests(MongoResource resource) : IClassFixture<MongoResource>
{
private static int _initialized;
[Fact]
public async Task Interface_Projection_With_CamelCase_Conventions_Should_Project_Derived_Fields()
{
// arrange
EnsureMongoMappings();
var collection = resource.CreateCollection<Person>("issue6291_" + Guid.NewGuid().ToString("N"));
collection.InsertOne(
new Person
{
PersonProp1 = 6,
PetList =
[
new Dog
{
AnimalProp1 = 5,
AnimalProp2 = "animal-dog",
DogProp1 = 11,
DogProp2 = "dog-2"
},
new Cat
{
AnimalProp1 = 6,
AnimalProp2 = "animal-cat",
CatProp1 = 22,
CatProp2 = "cat-2"
}
]
});
var executor = await new ServiceCollection()
.AddSingleton<IMongoCollection<Person>>(collection)
.AddGraphQL()
.AddMongoDbProjections()
.AddObjectIdConverters()
.AddMongoDbFiltering()
.AddMongoDbSorting()
.AddMongoDbPagingProviders()
.AddType<PersonType>()
.AddType<AnimalType>()
.AddType<CatType>()
.AddType<DogType>()
.AddQueryType<QueryType>()
.UseDefaultPipeline()
.Services
.BuildServiceProvider()
.GetRequiredService<IRequestExecutorProvider>()
.GetExecutorAsync();
// act
var result = await executor.ExecuteAsync(
"""
{
userProfiles {
items {
personProp1
petList {
__typename
animalProp1
animalProp2
... on Cat {
catProp1
catProp2
}
... on Dog {
dogProp1
dogProp2
}
}
}
}
}
""");
// assert
var operationResult = result.ExpectOperationResult();
Assert.True(operationResult.Errors is null || operationResult.Errors.Count is 0);
var json = result.ToJson();
using var document = JsonDocument.Parse(json);
var pets = document.RootElement
.GetProperty("data")
.GetProperty("userProfiles")
.GetProperty("items")[0]
.GetProperty("petList");
var dog = pets.EnumerateArray().First(x => x.GetProperty("__typename").GetString() == nameof(Dog));
var cat = pets.EnumerateArray().First(x => x.GetProperty("__typename").GetString() == nameof(Cat));
Assert.Equal(11, dog.GetProperty("dogProp1").GetInt32());
Assert.Equal("dog-2", dog.GetProperty("dogProp2").GetString());
Assert.Equal(22, cat.GetProperty("catProp1").GetInt32());
Assert.Equal("cat-2", cat.GetProperty("catProp2").GetString());
}
private static void EnsureMongoMappings()
{
if (Interlocked.Exchange(ref _initialized, 1) is 1)
{
return;
}
var conventionPack = new ConventionPack
{
new CamelCaseElementNameConvention(),
new IgnoreIfNullConvention(true),
new IgnoreExtraElementsConvention(true)
};
ConventionRegistry.Register(
"issue6291_" + Guid.NewGuid().ToString("N"),
conventionPack,
t => t == typeof(Person)
|| t == typeof(Animal)
|| t == typeof(Cat)
|| t == typeof(Dog));
if (!BsonClassMap.IsClassMapRegistered(typeof(Animal)))
{
BsonClassMap.RegisterClassMap<Animal>(
cm =>
{
cm.AutoMap();
cm.SetIsRootClass(true);
cm.AddKnownType(typeof(Cat));
cm.AddKnownType(typeof(Dog));
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(Cat)))
{
BsonClassMap.RegisterClassMap<Cat>(cm => cm.AutoMap());
}
if (!BsonClassMap.IsClassMapRegistered(typeof(Dog)))
{
BsonClassMap.RegisterClassMap<Dog>(cm => cm.AutoMap());
}
if (!BsonClassMap.IsClassMapRegistered(typeof(Person)))
{
BsonClassMap.RegisterClassMap<Person>(cm => cm.AutoMap());
}
}
private sealed class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Name("Query");
descriptor
.Field("userProfiles")
.Resolve(
static context =>
context.Service<IMongoCollection<Person>>().AsExecutable())
.UseOffsetPaging<ObjectType<Person>>(options: new() { IncludeTotalCount = true })
.UseProjection()
.UseFiltering()
.UseSorting();
}
}
private sealed class PersonType : ObjectType<Person>
{
protected override void Configure(IObjectTypeDescriptor<Person> descriptor)
=> descriptor.Field(x => x.PetList).Type<ListType<AnimalType>>();
}
private sealed class AnimalType : InterfaceType<Animal>;
private sealed class CatType : ObjectType<Cat>
{
protected override void Configure(IObjectTypeDescriptor<Cat> descriptor)
{
descriptor.Implements<AnimalType>();
descriptor
.Field("_t")
.Type<StringType>()
.IsProjected()
.Resolve(static _ => nameof(Cat));
}
}
private sealed class DogType : ObjectType<Dog>
{
protected override void Configure(IObjectTypeDescriptor<Dog> descriptor)
{
descriptor.Implements<AnimalType>();
descriptor
.Field("_t")
.Type<StringType>()
.IsProjected()
.Resolve(static _ => nameof(Dog));
}
}
private sealed class Person
{
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
public int PersonProp1 { get; set; }
public IReadOnlyList<Animal> PetList { get; set; } = [];
}
private abstract class Animal
{
public int AnimalProp1 { get; set; }
public string AnimalProp2 { get; set; } = string.Empty;
}
private sealed class Cat : Animal
{
public int CatProp1 { get; set; }
public string CatProp2 { get; set; } = string.Empty;
}
private sealed class Dog : Animal
{
public int DogProp1 { get; set; }
public string DogProp2 { get; set; } = string.Empty;
}
}