Skip to content

Commit 23ebecf

Browse files
authored
Where date and time (#778)
1 parent 7a206a6 commit 23ebecf

21 files changed

+299
-6
lines changed

docs/mdsource/query-usage.source.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,31 @@ All where statements require a `path`. This is a full path to a, possible nested
5454

5555
#### Supported Types
5656

57-
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string), [Guid](https://docs.microsoft.com/en-us/dotnet/api/system.guid), [Double](https://docs.microsoft.com/en-us/dotnet/api/system.double), [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean), [Float](https://docs.microsoft.com/en-us/dotnet/api/system.float), [Byte](https://docs.microsoft.com/en-us/dotnet/api/system.byte), [DateTime](https://docs.microsoft.com/en-us/dotnet/api/system.datetime), [DateTimeOffset](https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset), [Decimal](https://docs.microsoft.com/en-us/dotnet/api/system.decimal), [Int16](https://docs.microsoft.com/en-us/dotnet/api/system.int16), [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32), [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64), [UInt16](https://docs.microsoft.com/en-us/dotnet/api/system.uint16), [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32), [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64), and [Enum](https://docs.microsoft.com/en-us/dotnet/api/system.enum).
57+
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string), [Guid](https://docs.microsoft.com/en-us/dotnet/api/system.guid), [Double](https://docs.microsoft.com/en-us/dotnet/api/system.double), [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean), [Float](https://docs.microsoft.com/en-us/dotnet/api/system.float), [Byte](https://docs.microsoft.com/en-us/dotnet/api/system.byte), [DateTime](https://docs.microsoft.com/en-us/dotnet/api/system.datetime), [DateOnly](https://docs.microsoft.com/en-us/dotnet/api/system.dateonly), [TimeOnly](https://docs.microsoft.com/en-us/dotnet/api/system.timeonly), [DateTimeOffset](https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset), [Decimal](https://docs.microsoft.com/en-us/dotnet/api/system.decimal), [Int16](https://docs.microsoft.com/en-us/dotnet/api/system.int16), [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32), [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64), [UInt16](https://docs.microsoft.com/en-us/dotnet/api/system.uint16), [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32), [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64), and [Enum](https://docs.microsoft.com/en-us/dotnet/api/system.enum).
58+
59+
60+
##### TimeOnly query
61+
62+
```
63+
{
64+
timeEntities (where: {path: 'Property', comparison: equal, value: '10:11 AM'})
65+
{
66+
id
67+
}
68+
}
69+
```
70+
71+
72+
##### DateOnly query
73+
74+
```
75+
{
76+
dateEntities (where: {path: 'Property', comparison: equal, value: '2020-10-1'})
77+
{
78+
id
79+
}
80+
}
81+
```
5882

5983

6084
#### Supported Comparisons

docs/query-usage.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,31 @@ All where statements require a `path`. This is a full path to a, possible nested
6161

6262
#### Supported Types
6363

64-
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string), [Guid](https://docs.microsoft.com/en-us/dotnet/api/system.guid), [Double](https://docs.microsoft.com/en-us/dotnet/api/system.double), [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean), [Float](https://docs.microsoft.com/en-us/dotnet/api/system.float), [Byte](https://docs.microsoft.com/en-us/dotnet/api/system.byte), [DateTime](https://docs.microsoft.com/en-us/dotnet/api/system.datetime), [DateTimeOffset](https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset), [Decimal](https://docs.microsoft.com/en-us/dotnet/api/system.decimal), [Int16](https://docs.microsoft.com/en-us/dotnet/api/system.int16), [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32), [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64), [UInt16](https://docs.microsoft.com/en-us/dotnet/api/system.uint16), [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32), [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64), and [Enum](https://docs.microsoft.com/en-us/dotnet/api/system.enum).
64+
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string), [Guid](https://docs.microsoft.com/en-us/dotnet/api/system.guid), [Double](https://docs.microsoft.com/en-us/dotnet/api/system.double), [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean), [Float](https://docs.microsoft.com/en-us/dotnet/api/system.float), [Byte](https://docs.microsoft.com/en-us/dotnet/api/system.byte), [DateTime](https://docs.microsoft.com/en-us/dotnet/api/system.datetime), [DateOnly](https://docs.microsoft.com/en-us/dotnet/api/system.dateonly), [TimeOnly](https://docs.microsoft.com/en-us/dotnet/api/system.timeonly), [DateTimeOffset](https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset), [Decimal](https://docs.microsoft.com/en-us/dotnet/api/system.decimal), [Int16](https://docs.microsoft.com/en-us/dotnet/api/system.int16), [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32), [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64), [UInt16](https://docs.microsoft.com/en-us/dotnet/api/system.uint16), [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32), [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64), and [Enum](https://docs.microsoft.com/en-us/dotnet/api/system.enum).
65+
66+
67+
##### TimeOnly query
68+
69+
```
70+
{
71+
timeEntities (where: {path: 'Property', comparison: equal, value: '10:11 AM'})
72+
{
73+
id
74+
}
75+
}
76+
```
77+
78+
79+
##### DateOnly query
80+
81+
```
82+
{
83+
dateEntities (where: {path: 'Property', comparison: equal, value: '2020-10-1'})
84+
{
85+
id
86+
}
87+
}
88+
```
6589

6690

6791
#### Supported Comparisons

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;NU5104;CS1573</NoWarn>
5-
<Version>21.2.0</Version>
5+
<Version>21.3.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<PackageTags>EntityFrameworkCore, EntityFramework, GraphQL</PackageTags>
88
<ImplicitUsings>true</ImplicitUsings>

src/GraphQL.EntityFramework/Where/ExpressionBuilder.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ static Expression ProcessList(string path, Comparison comparison, string?[]? val
119119
.MakeGenericType(listItemType)
120120
.GetMethods(BindingFlags.Public | BindingFlags.Static)
121121
.Single(m => m.Name == "BuildPredicate" && m.GetParameters().Length == 5)
122-
.Invoke(new(), new object[] { listPath, comparison, values!, false, stringComparison! })!;
122+
.Invoke(new(), new object[]
123+
{
124+
listPath,
125+
comparison,
126+
values!,
127+
false,
128+
stringComparison!
129+
})!;
123130

124131
// Generate a method info for the Any Enumerable Static Method
125132
var anyInfo = typeof(Enumerable)
@@ -142,7 +149,7 @@ static Expression GetExpression(string path, Comparison comparison, string?[]? v
142149
{
143150
case Comparison.NotIn:
144151
WhereValidator.ValidateString(comparison, stringComparison);
145-
expressionBody = NegateExpression(MakeStringListInComparison(values!, property, stringComparison)); // Ensure expression is negated
152+
expressionBody = NegateExpression(MakeStringListInComparison(values!, property, stringComparison)); // Ensure expression is negated
146153
break;
147154
case Comparison.In:
148155
WhereValidator.ValidateString(comparison, stringComparison);
@@ -162,7 +169,7 @@ static Expression GetExpression(string path, Comparison comparison, string?[]? v
162169
{
163170
case Comparison.NotIn:
164171
WhereValidator.ValidateObject(property.PropertyType, comparison, stringComparison);
165-
expressionBody = NegateExpression(MakeObjectListInComparision(values!, property)); // Ensure expression is negated
172+
expressionBody = NegateExpression(MakeObjectListInComparision(values!, property));
166173
break;
167174
case Comparison.In:
168175
WhereValidator.ValidateObject(property.PropertyType, comparison, stringComparison);

src/GraphQL.EntityFramework/Where/TypeConverter.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ static IList ConvertStringsToListInternal(IEnumerable<string> values, Type type)
115115
return values.Select(_ => (DateTime?)DateTime.Parse(_)).ToList();
116116
}
117117

118+
if (type == typeof(Time))
119+
{
120+
return values.Select(Time.Parse).ToList();
121+
}
122+
if (type == typeof(Time?))
123+
{
124+
return values.Select(_ => (Time?)Time.Parse(_)).ToList();
125+
}
126+
127+
if (type == typeof(Date))
128+
{
129+
return values.Select(_ => Date.ParseExact(_, "yyyy-MM-dd")).ToList();
130+
}
131+
if (type == typeof(Date?))
132+
{
133+
return values.Select(_ => (Date?)Date.ParseExact(_, "yyyy-MM-dd")).ToList();
134+
}
135+
118136
if (type == typeof(DateTimeOffset))
119137
{
120138
return values.Select(DateTimeOffset.Parse).ToList();
@@ -151,6 +169,16 @@ static IList ConvertStringsToListInternal(IEnumerable<string> values, Type type)
151169
return ValueConverter.ConvertTo<DateTime>(value);
152170
}
153171

172+
if (type == typeof(Date))
173+
{
174+
return ValueConverter.ConvertTo<Date>(value);
175+
}
176+
177+
if (type == typeof(Time))
178+
{
179+
return ValueConverter.ConvertTo<Time>(value);
180+
}
181+
154182
if (type == typeof(DateTimeOffset))
155183
{
156184
return ValueConverter.ConvertTo<DateTimeOffset>(value);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class DateConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Date, DateTime>
2+
{
3+
public DateConverter() : base(
4+
d => d.ToDateTime(Time.MinValue),
5+
d => Date.FromDateTime(d))
6+
{
7+
}
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class NullableDateConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Date?, DateTime?>
2+
{
3+
public NullableDateConverter() : base(
4+
d => d == null
5+
? null
6+
: new DateTime?(d.Value.ToDateTime(Time.MinValue)),
7+
d => d == null
8+
? null
9+
: new Date?(Date.FromDateTime(d.Value)))
10+
{
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class NullableTimeConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Time?, TimeSpan?>
2+
{
3+
public NullableTimeConverter() : base(
4+
t => t == null ? null : new TimeSpan?(t.Value.ToTimeSpan()),
5+
t => t == null ? null : new Time?(Time.FromTimeSpan(t.Value)))
6+
{
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class TimeConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Time, TimeSpan>
2+
{
3+
public TimeConverter() : base(
4+
t => t.ToTimeSpan(),
5+
t => Time.FromTimeSpan(t))
6+
{
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class DateEntity
2+
{
3+
public Guid Id { get; set; } = Guid.NewGuid();
4+
public Date? Property { get; set; }
5+
}

0 commit comments

Comments
 (0)