Skip to content
Closed
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/Core/Resolvers/PostgreSqlExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ private void ConfigurePostgreSqlQueryExecutor()
{
NpgsqlConnectionStringBuilder builder = new(dataSource.ConnectionString);

if (string.IsNullOrEmpty(builder.Timezone))
{
builder.Timezone = "UTC";
}

if (_runtimeConfigProvider.IsLateConfigured)
{
builder.SslMode = SslMode.VerifyFull;
Expand Down
40 changes: 40 additions & 0 deletions src/Core/Services/ExecutionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,49 @@ private static bool TryGetPropertyFromParent(
SupportedHotChocolateTypes.SINGLE_TYPE => value is IntValueNode intValueNode ? intValueNode.ToSingle() : ((FloatValueNode)value).ToSingle(),
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.UUID_TYPE => Guid.TryParse(value.Value!.ToString(), out Guid guidValue) ? guidValue : value.Value,
_ => value.Value
Comment on lines +400 to 402

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make too much sense to me...if we are dealing with SupportedHotChocolateTypes.DATETIME_TYPE shouldn't we return a DateTime (not a DateTimeOffset)?

};

static object? ParseDateTimeValue(object? raw)
{
if (raw is null)
{
return null;
}

if (raw is DateTime dt)
{
return dt.Kind switch
{
DateTimeKind.Utc => dt,
DateTimeKind.Unspecified => DateTime.SpecifyKind(dt, DateTimeKind.Utc),
_ => dt.ToUniversalTime()
};
}

if (raw is DateTimeOffset dto)
{
return dto.UtcDateTime;
}

if (raw is string s)
{
// HotChocolate DateTime inputs are supplied as strings; parse them so DB providers
// (notably PostgreSQL) receive a typed UTC parameter instead of text.
if (DateTimeOffset.TryParse(
s,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal,

@Aniruddh25 Aniruddh25 Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt this forcing UTC culture for all db types? What if the original string had culture information and its losing that while parsing here?

This explains why the MSSQL test is failing as per your analysis. If the culture was set to: CultureInfo.DefaultThreadCurrentCulture = ci; by some other test, it fails.

Similarly, if the test string itself specifies a different timezone wont it fail, since with this change, we are assuming utc?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this path be special cased to PostgreSQL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right. Please see #3728 for the most up-to-date version of the PR (PostgreSQL is special cased and the parsing logic is moved to a different method).

out DateTimeOffset parsedDto))
{
return parsedDto.UtcDateTime;
}
}

return raw;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ public async Task PGSQL_real_graphql_in_filter_expectedValues(
await QueryTypeColumnFilterAndOrderBy(type, "in", sqlValue, gqlValue, "IN");
}

/// <summary>
/// PostgreSQL datetime filter tests with timezone offsets.
/// Verifies that GraphQL datetime arguments are normalized to UTC before filtering.
/// Tests all comparison operators (eq, neq, gt, gte, lt, lte) with offset and offset-less inputs.
/// </summary>
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T05:23:54-05:00\"", "=",
DisplayName = "DateTime eq converts -05:00 offset to UTC.")]
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T15:53:54+05:30\"", "=",
DisplayName = "DateTime eq converts +05:30 offset to UTC.")]
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T10:23:54Z\"", "=",
DisplayName = "DateTime eq preserves UTC input.")]
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T10:23:54\"", "=",
DisplayName = "DateTime eq treats offset-less input as UTC.")]
[DataRow(DATETIME_TYPE, "neq", "'1999-01-08 10:23:54'", "\"1999-01-08T05:23:54-05:00\"", "!=",
DisplayName = "DateTime neq converts -05:00 offset to UTC.")]
[DataRow(DATETIME_TYPE, "gt", "'1999-01-08 10:23:53'", "\"1999-01-08T05:23:53-05:00\"", ">",
DisplayName = "DateTime gt converts -05:00 offset to UTC.")]
[DataRow(DATETIME_TYPE, "gte", "'1999-01-08 10:23:54'", "\"1999-01-08T05:23:54-05:00\"", ">=",
DisplayName = "DateTime gte converts -05:00 offset to UTC.")]
[DataRow(DATETIME_TYPE, "lt", "'1999-01-08 10:23:55'", "\"1999-01-08T05:23:55-05:00\"", "<",
DisplayName = "DateTime lt converts -05:00 offset to UTC.")]
[DataRow(DATETIME_TYPE, "lte", "'1999-01-08 10:23:54'", "\"1999-01-08T05:23:54-05:00\"", "<=",
DisplayName = "DateTime lte converts -05:00 offset to UTC.")]
[DataTestMethod]
public async Task PGSQL_real_graphql_datetime_filter_offset_expectedValues(
string type,
string filterOperator,
string sqlValue,
string gqlValue,
string queryOperator)
{
await QueryTypeColumnFilterAndOrderBy(type, filterOperator, sqlValue, gqlValue, queryOperator);
}

protected override string MakeQueryOnTypeTable(List<DabField> queryFields, int id)
{
return MakeQueryOnTypeTable(queryFields, filterValue: id.ToString(), filterField: "id");
Expand All @@ -99,7 +133,9 @@ protected override string MakeQueryOnTypeTable(
string orderBy = "id",
string limit = "1")
{
string formattedSelect = limit.Equals("1") ? "SELECT to_jsonb(subq3) AS DATA" : "SELECT json_agg(to_jsonb(subq3)) AS DATA";
string formattedSelect = limit.Equals("1")
? "SELECT to_jsonb(subq3) AS DATA"
: "SELECT COALESCE(json_agg(to_jsonb(subq3)), '[]'::json) AS DATA";

return @"
" + formattedSelect + @"
Expand Down Expand Up @@ -141,15 +177,5 @@ private static string ProperlyFormatTypeTableColumn(string columnName)
return columnName;
}
}

/// <summary>
/// Bypass DateTime GQL tests for PostreSql
/// </summary>
[DataTestMethod]
[Ignore]
public new void QueryTypeColumnFilterAndOrderByDateTime(string type, string filterOperator, string sqlValue, string gqlValue, string queryOperator)
{
Assert.Inconclusive("Test skipped for PostgreSql.");
}
}
}
60 changes: 60 additions & 0 deletions src/Service.Tests/UnitTests/ExecutionHelperUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using Azure.DataApiBuilder.Service.Services;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Types;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Azure.DataApiBuilder.Service.Tests.UnitTests;

[TestClass]
public class ExecutionHelperUnitTests
{
[TestMethod]
public void ExtractValueFromIValueNode_DateTimeLiteral_ReturnsUtcDateTime()
{
Mock<IInputValueDefinition> argumentSchema = CreateArgumentSchema(new DateTimeType());
Mock<IVariableValueCollection> variables = new();

object result = ExecutionHelper.ExtractValueFromIValueNode(
new StringValueNode("2026-04-22T10:15:30-07:00"),
argumentSchema.Object,
variables.Object);

Assert.IsInstanceOfType<DateTime>(result);
Assert.AreEqual(
new DateTime(2026, 4, 22, 17, 15, 30, DateTimeKind.Utc),
(DateTime)result);
}

[TestMethod]
public void ExtractValueFromIValueNode_DateTimeVariable_ReturnsUtcDateTime()
{
Mock<IInputValueDefinition> argumentSchema = CreateArgumentSchema(new DateTimeType());
Mock<IVariableValueCollection> variables = new();
variables
.Setup(v => v.GetValue<IValueNode>("createdAt"))
.Returns(new StringValueNode("2026-04-22T10:15:30Z"));

object result = ExecutionHelper.ExtractValueFromIValueNode(
new VariableNode("createdAt"),
argumentSchema.Object,
variables.Object);

Assert.IsInstanceOfType<DateTime>(result);
Assert.AreEqual(
new DateTime(2026, 4, 22, 10, 15, 30, DateTimeKind.Utc),
(DateTime)result);
}

private static Mock<IInputValueDefinition> CreateArgumentSchema(IInputType type)
{
Mock<IInputValueDefinition> argumentSchema = new();
argumentSchema.SetupGet(a => a.Type).Returns(type);
return argumentSchema;
}
}
Loading