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

Added support for DateOnly and TimeOnly #4483

Merged
merged 3 commits into from
Nov 26, 2021
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
5 changes: 5 additions & 0 deletions src/HotChocolate/Core/src/Types/Types/Scalars/Scalars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static class Scalars
{ typeof(byte[]), typeof(ByteArrayType) },
{ typeof(NameString), typeof(NameType) },
{ typeof(TimeSpan), typeof(TimeSpanType) },

#if NET6_0_OR_GREATER
{ typeof(DateOnly), typeof(DateType) },
{ typeof(TimeOnly), typeof(TimeSpanType) },
#endif
};

private static readonly Dictionary<NameString, Type> _nameLookup = new()
Expand Down
290 changes: 142 additions & 148 deletions src/HotChocolate/Core/src/Types/Utilities/DefaultTypeConverter.Setup.cs

Large diffs are not rendered by default.

141 changes: 116 additions & 25 deletions src/HotChocolate/Core/test/Types.Tests/Types/Scalars/DateTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Tests;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.Types
Expand All @@ -15,10 +20,10 @@ public void Serialize_Date()
var dateType = new DateType();
var dateTime = new DateTime(
2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
string expectedValue = "2018-06-11";
var expectedValue = "2018-06-11";

// act
string serializedValue = (string)dateType.Serialize(dateTime);
var serializedValue = (string)dateType.Serialize(dateTime);

// assert
Assert.Equal(expectedValue, serializedValue);
Expand All @@ -31,7 +36,7 @@ public void Serialize_Null()
var dateType = new DateType();

// act
object serializedValue = dateType.Serialize(null);
var serializedValue = dateType.Serialize(null);

// assert
Assert.Null(serializedValue);
Expand All @@ -44,10 +49,10 @@ public void Serialize_String_Exception()
var dateType = new DateType();

// act
Action a = () => dateType.Serialize("foo");
void Action() => dateType.Serialize("foo");

// assert
Assert.Throws<SerializationException>(a);
Assert.Throws<SerializationException>(Action);
}

[Fact]
Expand All @@ -58,7 +63,7 @@ public void Deserialize_IsoString_DateTime()
var date = new DateTime(2018, 6, 11);

// act
var result = (DateTime)dateType.Deserialize("2018-06-11");
var result = (DateTime)dateType.Deserialize("2018-06-11")!;

// assert
Assert.Equal(date, result);
Expand All @@ -71,7 +76,7 @@ public void Deserialize_InvalidString_To_DateTimeOffset()
var type = new DateType();

// act
bool success = type.TryDeserialize("abc", out object deserialized);
var success = type.TryDeserialize("abc", out _);

// assert
Assert.False(success);
Expand All @@ -86,7 +91,7 @@ public void Deserialize_DateTimeOffset_To_DateTime()
new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc));

// act
bool success = type.TryDeserialize(time, out object deserialized);
var success = type.TryDeserialize(time, out var deserialized);

// assert
Assert.True(success);
Expand All @@ -102,7 +107,7 @@ public void Deserialize_DateTime_To_DateTime()
var time = new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);

// act
bool success = type.TryDeserialize(time, out object deserialized);
var success = type.TryDeserialize(time, out var deserialized);

// assert
Assert.True(success);
Expand All @@ -118,7 +123,7 @@ public void Deserialize_NullableDateTime_To_DateTime()
new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);

// act
bool success = type.TryDeserialize(time, out object deserialized);
var success = type.TryDeserialize(time, out var deserialized);

// assert
Assert.True(success);
Expand All @@ -133,7 +138,7 @@ public void Deserialize_NullableDateTime_To_DateTime_2()
DateTime? time = null;

// act
bool success = type.TryDeserialize(time, out object deserialized);
var success = type.TryDeserialize(time, out var deserialized);

// assert
Assert.True(success);
Expand All @@ -147,7 +152,7 @@ public void Deserialize_Null_To_Null()
var type = new DateType();

// act
bool success = type.TryDeserialize(null, out object deserialized);
var success = type.TryDeserialize(null, out var deserialized);

// assert
Assert.True(success);
Expand All @@ -163,8 +168,7 @@ public void ParseLiteral_StringValueNode()
var expectedDateTime = new DateTime(2018, 6, 29);

// act
var dateTime = (DateTime)dateType
.ParseLiteral(literal);
var dateTime = (DateTime)dateType.ParseLiteral(literal)!;

// assert
Assert.Equal(expectedDateTime, dateTime);
Expand All @@ -188,8 +192,7 @@ public void ParseLiteral_StringValueNode_DifferentCulture(
var expectedDateTime = new DateTime(2018, 6, 29);

// act
var dateTime = (DateTime)dateType
.ParseLiteral(literal);
var dateTime = (DateTime)dateType.ParseLiteral(literal)!;

// assert
Assert.Equal(expectedDateTime, dateTime);
Expand All @@ -203,7 +206,7 @@ public void ParseLiteral_NullValueNode()
NullValueNode literal = NullValueNode.Default;

// act
object value = dateType.ParseLiteral(literal);
var value = dateType.ParseLiteral(literal);

// assert
Assert.Null(value);
Expand All @@ -214,9 +217,8 @@ public void ParseValue_DateTime()
{
// arrange
var dateType = new DateType();
var dateTime =
new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
string expectedLiteralValue = "2018-06-11";
var dateTime = new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
var expectedLiteralValue = "2018-06-11";

// act
var stringLiteral =
Expand All @@ -231,9 +233,8 @@ public void ParseValue_Null()
{
// arrange
var dateType = new DateType();
var dateTime =
new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
string expectedLiteralValue = "2018-06-11";
var dateTime = new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
var expectedLiteralValue = "2018-06-11";

// act
var stringLiteral =
Expand All @@ -244,7 +245,7 @@ public void ParseValue_Null()
}

[Fact]
public void EnsureDateTypeKindIsCorret()
public void EnsureDateTypeKindIsCorrect()
{
// arrange
var type = new DateType();
Expand All @@ -253,7 +254,7 @@ public void EnsureDateTypeKindIsCorret()
TypeKind kind = type.Kind;

// assert
Assert.Equal(TypeKind.Scalar, type.Kind);
Assert.Equal(TypeKind.Scalar, kind);
}

[Fact]
Expand All @@ -274,12 +275,102 @@ public void DateType_Binds_Only_Explicitly()
Assert.IsType<DateTimeType>(dateTimeType);
}

#if NET6_0_OR_GREATER
[Fact]
public async Task DateOnly_And_TimeOnly_As_Argument_Schema()
{
Snapshot.FullName();

await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryDateTime1>()
.BuildSchemaAsync()
.MatchSnapshotAsync();
}

[Fact]
public async Task DateOnly_And_TimeOnly_As_Argument()
{
Snapshot.FullName();

await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryDateTime1>()
.AddType(() => new TimeSpanType(TimeSpanFormat.DotNet))
.ExecuteRequestAsync(
@"{
foo {
time(time: ""11:22"")
date(date: ""2017-12-30"")
}
}")
.MatchSnapshotAsync();
}

[Fact]
public async Task DateOnly_And_TimeOnly_As_ReturnValue_Schema()
{
Snapshot.FullName();

await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryDateTime2>()
.BuildSchemaAsync()
.MatchSnapshotAsync();
}

[Fact]
public async Task DateOnly_And_TimeOnly_As_ReturnValue()
{
Snapshot.FullName();

await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryDateTime2>()
.AddType(() => new TimeSpanType(TimeSpanFormat.DotNet))
.ExecuteRequestAsync(
@"{
bar {
time
date
}
}")
.MatchSnapshotAsync();
}
#endif

public class Query
{
[GraphQLType(typeof(DateType))]
public DateTime? DateField => DateTime.UtcNow;

public DateTime? DateTimeField => DateTime.UtcNow;
}

#if NET6_0_OR_GREATER
public class QueryDateTime1
{
public Foo Foo => new();
}

public class Foo
{
public string GetTime(TimeOnly time) => time.ToString();

public string GetDate(DateOnly date) => date.ToString();
}

public class QueryDateTime2
{
public Bar Bar => new();
}

public class Bar
{
public TimeOnly GetTime() => TimeOnly.MaxValue;

public DateOnly GetDate() => DateOnly.MaxValue;
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"foo": {
"time": "11:22",
"date": "12/30/2017"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema {
query: QueryDateTime1
}

type Foo {
time(time: TimeSpan!): String
date(date: Date!): String
}

type QueryDateTime1 {
foo: Foo
}

"The `@defer` directive may be provided for fragment spreads and inline fragments to inform the executor to delay the execution of the current fragment to indicate deprioritization of the current fragment. A query with `@defer` directive will cause the request to potentially return multiple responses, where non-deferred data is delivered in the initial response and data deferred is delivered in a subsequent response. `@include` and `@skip` take precedence over `@defer`."
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD

"The `Date` scalar represents an ISO-8601 compliant date type."
scalar Date

"The `TimeSpan` scalar represents an ISO-8601 compliant duration type."
scalar TimeSpan
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"bar": {
"time": "23:59:59.9999999",
"date": "9999-12-31"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema {
query: QueryDateTime2
}

type Bar {
time: TimeSpan!
date: Date!
}

type QueryDateTime2 {
bar: Bar
}

"The `@defer` directive may be provided for fragment spreads and inline fragments to inform the executor to delay the execution of the current fragment to indicate deprioritization of the current fragment. A query with `@defer` directive will cause the request to potentially return multiple responses, where non-deferred data is delivered in the initial response and data deferred is delivered in a subsequent response. `@include` and `@skip` take precedence over `@defer`."
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD

"The `Date` scalar represents an ISO-8601 compliant date type."
scalar Date

"The `TimeSpan` scalar represents an ISO-8601 compliant duration type."
scalar TimeSpan