Skip to content
Open
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
45 changes: 44 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 @@ -386,6 +386,9 @@ private static bool TryGetPropertyFromParent(
return value.Value;
}

// TODO: Look into why there is no support for SupportedHotChocolateTypes.DATETIMEOFFSET_TYPE translation in argumentSchema.
// This is more of a semantic issue than a logical one because we can still do appropriate parsing even if every date/time
// (with offset or not) is treated as SupportedHotChocolateTypes.DATETIME_TYPE but it is worth looking into regardless.
return argumentSchema.Type.TypeName() switch
{
SupportedHotChocolateTypes.BYTE_TYPE => ((IntValueNode)value).ToByte(),
Expand All @@ -395,9 +398,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 +401 to 403
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,29 @@ 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.
/// </summary>
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T05:23:54-05:00\"", "=",
DisplayName = "DateTime filter eq 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 filter gte 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 filter eq converts +05:30 offset to UTC.")]
[DataRow(DATETIME_TYPE, "eq", "'1999-01-08 10:23:54'", "\"1999-01-08T10:23:54Z\"", "=",
DisplayName = "DateTime filter eq preserves UTC input.")]
[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;
}
}