Add support for parsing DateTime values#3250
Add support for parsing DateTime values#3250ArjunNarendra wants to merge 1 commit intoAzure:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses #3094 by ensuring GraphQL DateTime (and DateTimeOffset) argument values are parsed into typed CLR values before being passed to database providers, avoiding PostgreSQL treating them as text parameters in comparisons.
Changes:
- Added explicit handling for
SupportedHotChocolateTypes.DATETIME_TYPEandSupportedHotChocolateTypes.DATETIMEOFFSET_TYPEinExecutionHelper.ExtractValueFromIValueNode. - Introduced local parsing helpers to convert string inputs into
DateTime/DateTimeOffset-typed values using invariant culture + roundtrip parsing.
| SupportedHotChocolateTypes.DATETIME_TYPE => ParseDateTimeValue(value.Value), | ||
| SupportedHotChocolateTypes.DATETIMEOFFSET_TYPE => ParseDateTimeOffsetValue(value.Value), | ||
| SupportedHotChocolateTypes.UUID_TYPE => Guid.TryParse(value.Value!.ToString(), out Guid guidValue) ? guidValue : value.Value, | ||
| _ => value.Value |
| if (DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTimeOffset parsedDto)) | ||
| { | ||
| return parsedDto.UtcDateTime; | ||
| } | ||
|
|
||
| if (DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime parsedDt)) | ||
| { | ||
| return parsedDt; |
|
|
||
| if (raw is DateTime dt) | ||
| { | ||
| return new DateTimeOffset(dt); |
| SupportedHotChocolateTypes.FLOAT_TYPE => value is IntValueNode intValueNode ? intValueNode.ToDouble() : ((FloatValueNode)value).ToDouble(), | ||
| SupportedHotChocolateTypes.DECIMAL_TYPE => value is IntValueNode intValueNode ? intValueNode.ToDecimal() : ((FloatValueNode)value).ToDecimal(), | ||
| SupportedHotChocolateTypes.DATETIME_TYPE => ParseDateTimeValue(value.Value), | ||
| SupportedHotChocolateTypes.DATETIMEOFFSET_TYPE => ParseDateTimeOffsetValue(value.Value), |
|
@ArjunNarendra looks like copilot is complaining because DateTimeOffset isn't implemented here. We need to discuss and figure out how to handle the situation of TZ-aware/localized timestamps and the interaction between PG/DAB/GQL. Unlike SQL, Postgres inherently does not support the direct storage of localized timestamps and converts them to UTC. It's up to the user to store additional TZ data separately in the table and perform post computation. These links may be a good place to start: |
Why make this change?
This PR is to address #3094, which is a bug that happens because the default type conversion for C# to Npgsql is to convert a string to a TEXT field, but this doesn't work when trying to compare a DATE type for PostgreSql databases.
What is this change?
I have updated ExecutionHelper.cs in ExtractValueFromIValueNode to explicitly handle SupportedHotChocolateTypes.DATETIME_TYPE and SupportedHotChocolateTypes.DATETIMEOFFSET_TYPE.
How was this tested?
Sample Request(s)
With query:
Before:
After: