Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/HotChocolate/Data/src/Data/Filters/Fields/FilterField.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Configuration;
using HotChocolate.Internal;
Expand Down Expand Up @@ -46,6 +47,10 @@ protected override void OnCompleteField(
{
RuntimeType = context.TypeInspector.GetReturnType(Member, ignoreAttributes: true);
}
else if (definition is FilterFieldConfiguration { Expression: LambdaExpression lambda })
{
RuntimeType = context.TypeInspector.GetType(lambda.ReturnType);
}
else if (base.RuntimeType is { } runtimeType)
{
RuntimeType = context.TypeInspector.GetType(runtimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ protected FilterFieldDescriptor(
if (Configuration.Expression is LambdaExpression lambda)
{
Configuration.Type = convention.GetFieldType(lambda.ReturnType);
Configuration.RuntimeType = lambda.ReturnType;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,50 @@ public async Task Expression_WithMoreThanOneParameter_ThrowsException()
ex.Errors.Single().Message.MatchSnapshot();
}

[Fact]
public async Task Create_CollectionExpression_WithVariables()
{
// arrange
var tester = _cache.CreateSchema<Foo, FooFilterInputType>(s_fooEntities);
const string query =
"""
query Test($where: FooFilterInput) {
root(where: $where) {
name
lastName
}
}
""";

// act
var res1 = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(query)
.SetVariableValues(new Dictionary<string, object?>
{
{ "where", CreateLatestBarFilter("A") }
})
.Build(),
TestContext.Current.CancellationToken);

var res2 = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(query)
.SetVariableValues(new Dictionary<string, object?>
{
{ "where", CreateLatestBarFilter("NoMatch") }
})
.Build(),
TestContext.Current.CancellationToken);

// assert
await Snapshot
.Create()
.Add(res1, "A")
.Add(res2, "NoMatch")
.MatchAsync(TestContext.Current.CancellationToken);
}

[Fact]
public async Task Create_CollectionLengthExpression()
{
Expand Down Expand Up @@ -112,6 +156,30 @@ await Snapshot
.MatchAsync(TestContext.Current.CancellationToken);
}

private static Dictionary<string, object?> CreateLatestBarFilter(string value)
=> new()
{
{
"latestBar",
new Dictionary<string, object?>
{
{
"some",
new Dictionary<string, object?>
{
{
"value",
new Dictionary<string, object?>
{
{ "eq", value }
}
}
}
}
}
}
};

public class Foo
{
public int Id { get; set; }
Expand All @@ -136,6 +204,12 @@ protected override void Configure(IFilterInputTypeDescriptor<Foo> descriptor)
{
descriptor.Field(x => x.Name + " " + x.LastName).Name("displayName");
descriptor.Field(x => x.Bars!.Count).Name("barLength");
descriptor
.Field(x => x.Bars!.OrderByDescending(b => b.Id).Take(1))
.Name("latestBar")
.Type<ListFilterInputType<BarFilterInputType>>();
}
}

public class BarFilterInputType : FilterInputType<Bar>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
A
---------------
{
"data": {
"root": [
{
"name": "Foo",
"lastName": "Galoo"
}
]
}
}
---------------

NoMatch
---------------
{
"data": {
"root": []
}
}
---------------
Loading