Skip to content
Open
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
42 changes: 41 additions & 1 deletion src/Core/Services/ExecutionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async ValueTask ExecuteQueryAsync(IMiddlewareContext context)
IQueryEngine queryEngine = _queryEngineFactory.GetQueryEngine(ds.DatabaseType);

IDictionary<string, object?> parameters = GetParametersFromContext(context);

if (context.Selection.Type.IsListType())
{
Tuple<IEnumerable<JsonDocument>, IMetadata?> result =
Expand Down Expand Up @@ -395,9 +395,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 +398 to 400
Copy link
Copy Markdown
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,
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 Down
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;
}
}