Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Comparable Conversion #1132

Merged
merged 6 commits into from
Oct 18, 2019
Merged
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
70 changes: 70 additions & 0 deletions src/Core/Types.Filters.Tests/QueryableFilterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HotChocolate.Execution;
using Snapshooter.Xunit;
using Xunit;
Expand Down Expand Up @@ -242,6 +244,74 @@ public void Execute_Filter_Equals_Or()
result.MatchSnapshot();
}

[Fact]
public async Task Execute_DateTime_Filter()
{
// arrange
ISchema schema = SchemaBuilder.New()
.AddQueryType<QueryFooDateTime>(d => d
.Name("Query")
.Field(y => y.Foo)
.UseFiltering())
.Create();

IQueryExecutor executor = schema.MakeExecutable();

IReadOnlyQueryRequest request = QueryRequestBuilder.New()
.SetQuery("{ foo(where: { foo_gte: \"2019-06-01\"}) { foo } }")
.Create();

// act
IExecutionResult result = await executor.ExecuteAsync(request);

// assert
result.MatchSnapshot();
}

[Fact]
public async Task Execute_DateTime_Filter_With_Variables()
{
// arrange
ISchema schema = SchemaBuilder.New()
.AddQueryType<QueryFooDateTime>(d => d
.Name("Query")
.Field(y => y.Foo)
.UseFiltering())
.Create();

IQueryExecutor executor = schema.MakeExecutable();

IReadOnlyQueryRequest request = QueryRequestBuilder.New()
.SetQuery(
"query TestQuery($where: FooDateTimeFilter) {" +
"foo(where: $where) { foo } }")
.SetVariableValue("where", new Dictionary<string, object>
{
{ "foo_gte", "2019-06-01" }
})
.Create();

// act
IExecutionResult result = await executor.ExecuteAsync(request);

// assert
result.MatchSnapshot();
}

public class FooDateTime
{
public DateTime Foo { get; set; }
}

public class QueryFooDateTime
{
public IEnumerable<FooDateTime> Foo { get; set; } = new List<FooDateTime>
{
new FooDateTime { Foo = new DateTime(2020,01,01, 18, 0, 0, DateTimeKind.Utc) },
new FooDateTime { Foo = new DateTime(2018,01,01, 18, 0, 0, DateTimeKind.Utc) }
};
}

public class QueryType
: ObjectType<Query>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Data": {
"foo": null
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Data": {
"foo": [
{
"foo": "2020-01-01T18:00:00.000Z"
}
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Data": {
"foo": [
{
"foo": "2020-01-01T18:00:00.000Z"
}
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public bool TryHandle(
if (operation.Type == typeof(IComparable)
&& type.IsInstanceOfType(value))
{
MemberExpression property =
Expression.Property(instance, operation.Property);
MemberExpression property = Expression.Property(instance, operation.Property);
var parsedValue = type.ParseLiteral(value);

if (operation.Property.PropertyType
.IsInstanceOfType(parsedValue))
if (!operation.Property.PropertyType.IsInstanceOfType(parsedValue))
{
parsedValue = converter.Convert(
typeof(object),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public bool TryHandle(
out Expression expression)
{
if (operation.Type == typeof(string)
&& type.IsInstanceOfType(value))
&& type.IsInstanceOfType(value))
{
object parsedValue = type.ParseLiteral(value);

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Types/Utilities/TypeConversion.Setup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using SysConv = System.Convert;
Expand Down