Skip to content

Commit e0d0574

Browse files
authored
Feat use selection improvements (#1555)
1 parent 3b9e329 commit e0d0574

File tree

56 files changed

+1680
-231
lines changed

Some content is hidden

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

56 files changed

+1680
-231
lines changed

src/Core/Core.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Tests", "Type
6767
EndProject
6868
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Mongo.Tests", "Types.Sorting.Mongo.Tests\Types.Sorting.Mongo.Tests.csproj", "{E8843255-77EB-471F-B682-528BB4B5D1D5}"
6969
EndProject
70-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection", "Types.Selection\Types.Selection.csproj", "{0FD6B6D1-00AB-42CF-800F-4039A976A75A}"
70+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selections", "Types.Selection\Types.Selections.csproj", "{0FD6B6D1-00AB-42CF-800F-4039A976A75A}"
7171
EndProject
72-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.Abstractions.Tests", "Types.Selection.Abstractions.Tests\Types.Selection.Abstractions.Tests.csproj", "{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}"
72+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selections.Abstractions.Tests", "Types.Selection.Abstractions.Tests\Types.Selections.Abstractions.Tests.csproj", "{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}"
7373
EndProject
74-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.InMemory.Tests", "Types.Selection.InMemory.Tests\Types.Selection.InMemory.Tests.csproj", "{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}"
74+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selections.InMemory.Tests", "Types.Selection.InMemory.Tests\Types.Selections.InMemory.Tests.csproj", "{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}"
7575
EndProject
76-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.Mongo.Tests", "Types.Selection.Mongo.Tests\Types.Selection.Mongo.Tests.csproj", "{21AA751A-4C29-414C-B476-EE9204A48DDD}"
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selections.Mongo.Tests", "Types.Selection.Mongo.Tests\Types.Selections.Mongo.Tests.csproj", "{21AA751A-4C29-414C-B476-EE9204A48DDD}"
7777
EndProject
78-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.SqlServer.Tests", "Types.Selection.SqlServer.Tests\Types.Selection.SqlServer.Tests.csproj", "{07674D84-4A5C-4608-BECE-03630D38275F}"
78+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selections.SqlServer.Tests", "Types.Selection.SqlServer.Tests\Types.Selections.SqlServer.Tests.csproj", "{07674D84-4A5C-4608-BECE-03630D38275F}"
7979
EndProject
8080
Global
8181
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/Core/Types.Filters/FilterFieldDescriptorBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Linq;
44
using System.Reflection;
55
using HotChocolate.Language;
6-
using HotChocolate.Utilities;
76
using HotChocolate.Types.Descriptors;
87
using HotChocolate.Types.Descriptors.Definitions;
8+
using HotChocolate.Utilities;
99

1010
namespace HotChocolate.Types.Filters
1111
{
@@ -16,7 +16,7 @@ protected FilterFieldDescriptorBase(
1616
IDescriptorContext context,
1717
PropertyInfo property)
1818
: base(context)
19-
{
19+
{
2020
Definition.Property = property
2121
?? throw new ArgumentNullException(nameof(property));
2222
Definition.Name = context.Naming.GetMemberName(
@@ -189,14 +189,14 @@ protected ITypeReference RewriteTypeToNullableType()
189189
if (reference is ISchemaTypeReference schemaRef)
190190
{
191191
return schemaRef.Type is NonNullType nnt
192-
? schemaRef.WithType(nnt)
192+
? schemaRef.WithType(nnt.Type)
193193
: schemaRef;
194194
}
195195

196196
if (reference is ISyntaxTypeReference syntaxRef)
197197
{
198198
return syntaxRef.Type is NonNullTypeNode nnt
199-
? syntaxRef.WithType(nnt)
199+
? syntaxRef.WithType(nnt.Type)
200200
: syntaxRef;
201201
}
202202

src/Core/Types.Selection.Abstractions.Tests/IResolverProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ public interface IResolverProvider
1010
public (IServiceCollection, Func<IResolverContext, IEnumerable<TResult>>)
1111
CreateResolver<TResult>(params TResult[] results)
1212
where TResult : class;
13+
14+
public (IServiceCollection, Func<IResolverContext, IAsyncEnumerable<TResult>>)
15+
CreateAsyncResolver<TResult>(params TResult[] results)
16+
where TResult : class;
1317
}
1418
}

src/Core/Types.Selection.Abstractions.Tests/SelectionTestsBase.cs

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public virtual void Execute_Selection_MultipleScalar()
163163
}
164164

165165
[Fact]
166-
public virtual void Execute_Selection_Method()
166+
public virtual void Execute_Selection_ComputedField()
167167
{
168168
// arrange
169169
IServiceCollection services;
@@ -186,31 +186,33 @@ public virtual void Execute_Selection_Method()
186186
IQueryExecutor executor = schema.MakeExecutable();
187187

188188
// act
189-
IExecutionResult result = executor.Execute(
190-
"{ foos { bar shouldNotVisit } }");
189+
var result = executor.Execute(
190+
"{ foos { bar computedField } }") as IReadOnlyQueryResult;
191191

192192
// assert
193193
Assert.Empty(result.Errors);
194194
Assert.NotNull(resultCtx);
195-
Assert.Collection(resultCtx.ToArray(),
195+
var foos = result.Data["foos"] as IList<object>;
196+
197+
Assert.Collection(foos.ToArray(),
196198
x =>
197199
{
198-
Assert.Equal("aa", x.Bar);
199-
Assert.Equal(0, x.Baz);
200-
Assert.Null(x.Nested);
201-
Assert.Null(x.ObjectArray);
200+
var casted = x as IDictionary<string, object>;
201+
Assert.NotNull(casted);
202+
Assert.Equal("aa", casted["bar"]);
203+
Assert.Equal("aa1", casted["computedField"]);
202204
},
203205
x =>
204206
{
205-
Assert.Equal("bb", x.Bar);
206-
Assert.Equal(0, x.Baz);
207-
Assert.Null(x.Nested);
208-
Assert.Null(x.ObjectArray);
207+
var casted = x as IDictionary<string, object>;
208+
Assert.NotNull(casted);
209+
Assert.Equal("bb", casted["bar"]);
210+
Assert.Equal("bb2", casted["computedField"]);
209211
});
210212
}
211213

212214
[Fact]
213-
public virtual void Execute_Selection_EmptyQueue()
215+
public virtual void Execute_Selection_ComputedFieldParent()
214216
{
215217
// arrange
216218
IServiceCollection services;
@@ -233,26 +235,80 @@ public virtual void Execute_Selection_EmptyQueue()
233235
IQueryExecutor executor = schema.MakeExecutable();
234236

235237
// act
236-
IExecutionResult result = executor.Execute(
237-
"{ foos { shouldNotVisit } }");
238+
var result = executor.Execute(
239+
"{ foos { bar computedFieldParent } }") as IReadOnlyQueryResult;
238240

239241
// assert
240242
Assert.Empty(result.Errors);
241243
Assert.NotNull(resultCtx);
242-
Assert.Collection(resultCtx.ToArray(),
244+
var foos = result.Data["foos"] as IList<object>;
245+
246+
Assert.Collection(foos.ToArray(),
243247
x =>
244248
{
245-
Assert.Null(x.Bar);
246-
Assert.Equal(0, x.Baz);
247-
Assert.Null(x.Nested);
248-
Assert.Null(x.ObjectArray);
249+
var casted = x as IDictionary<string, object>;
250+
Assert.NotNull(casted);
251+
Assert.Equal("aa", casted["bar"]);
252+
Assert.Equal("aa1", casted["computedFieldParent"]);
249253
},
250254
x =>
251255
{
252-
Assert.Null(x.Bar);
253-
Assert.Equal(0, x.Baz);
254-
Assert.Null(x.Nested);
255-
Assert.Null(x.ObjectArray);
256+
var casted = x as IDictionary<string, object>;
257+
Assert.NotNull(casted);
258+
Assert.Equal("bb", casted["bar"]);
259+
Assert.Equal("bb2", casted["computedFieldParent"]);
260+
});
261+
}
262+
263+
[Fact]
264+
public virtual void Execute_Selection_ComputedFieldLambda()
265+
{
266+
// arrange
267+
IServiceCollection services;
268+
Func<IResolverContext, IEnumerable<Foo>> resolver;
269+
(services, resolver) = _provider.CreateResolver(SAMPLE);
270+
271+
IQueryable<Foo> resultCtx = null;
272+
ISchema schema = SchemaBuilder.New()
273+
.AddServices(services.BuildServiceProvider())
274+
.AddObjectType<Foo>(
275+
x => x.Field("computedLambdaField")
276+
.Resolver(ctx => ctx.Parent<Foo>().Bar + ctx.Parent<Foo>().Baz))
277+
.AddQueryType<Query>(
278+
d => d.Field(t => t.Foos)
279+
.Resolver(resolver)
280+
.Use(next => async ctx =>
281+
{
282+
await next(ctx).ConfigureAwait(false);
283+
resultCtx = ctx.Result as IQueryable<Foo>;
284+
})
285+
.UseSelection())
286+
.Create();
287+
IQueryExecutor executor = schema.MakeExecutable();
288+
289+
// act
290+
var result = executor.Execute(
291+
"{ foos { bar computedLambdaField } }") as IReadOnlyQueryResult;
292+
293+
// assert
294+
Assert.Empty(result.Errors);
295+
Assert.NotNull(resultCtx);
296+
var foos = result.Data["foos"] as IList<object>;
297+
298+
Assert.Collection(foos.ToArray(),
299+
x =>
300+
{
301+
var casted = x as IDictionary<string, object>;
302+
Assert.NotNull(casted);
303+
Assert.Equal("aa", casted["bar"]);
304+
Assert.Equal("aa1", casted["computedLambdaField"]);
305+
},
306+
x =>
307+
{
308+
var casted = x as IDictionary<string, object>;
309+
Assert.NotNull(casted);
310+
Assert.Equal("bb", casted["bar"]);
311+
Assert.Equal("bb2", casted["computedLambdaField"]);
256312
});
257313
}
258314

@@ -485,7 +541,8 @@ public virtual void Execute_Selection_Set_Interface()
485541

486542
// assert
487543
Assert.NotNull(resultCtx);
488-
Assert.Collection(resultCtx.ToArray(),
544+
Assert.Collection(
545+
resultCtx.AsEnumerable().OrderBy(x => x.ISet.First().Bar).ToArray(),
489546
x =>
490547
{
491548
Assert.Null(x.Bar);
@@ -535,7 +592,8 @@ public virtual void Execute_Selection_Set()
535592

536593
// assert
537594
Assert.NotNull(resultCtx);
538-
Assert.Collection(resultCtx.ToArray(),
595+
Assert.Collection(
596+
resultCtx.AsEnumerable().OrderBy(x => x.HashSet.First().Bar).ToArray(),
539597
x =>
540598
{
541599
Assert.Null(x.Bar);
@@ -585,7 +643,8 @@ public virtual void Execute_Selection_SortedSet()
585643

586644
// assert
587645
Assert.NotNull(resultCtx);
588-
Assert.Collection(resultCtx.ToArray(),
646+
Assert.Collection(
647+
resultCtx.AsEnumerable().OrderBy(x => x.SortedSet.First().Bar).ToArray(),
589648
x =>
590649
{
591650
Assert.Null(x.Bar);
@@ -1363,8 +1422,9 @@ public class Foo
13631422
[UseSorting]
13641423
public List<NestedFoo> MiddlewareList { get; set; }
13651424

1366-
public IEnumerable<string> GetShouldNotVisit()
1367-
=> new string[] { "should not visit" };
1425+
public string GetComputedField() => Bar + Baz;
1426+
1427+
public string GetComputedFieldParent([Parent]Foo foo) => foo.Bar + foo.Baz;
13681428

13691429
public static Foo Create(string bar, int baz)
13701430
{

0 commit comments

Comments
 (0)