Skip to content

Commit

Permalink
Fixed DateTime conversion issue. (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Mar 27, 2019
1 parent d6fee85 commit c6b8b00
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate.Language;
using HotChocolate.Types;
using Snapshooter.Xunit;
Expand Down Expand Up @@ -483,6 +484,37 @@ public void Variable_List_NonNullListItemHasValue()
variables.GetVariable<List<string>>("test").MatchSnapshot();
}

[Fact]
public async Task EnsureThatDateTimeIsCoercedTheSameInAllCases()
{
// arrange
IQueryExecutor executor = Schema.Create(
"type Query { a(a: DateTime) : DateTime }",
c =>
{
c.RegisterExtendedScalarTypes();
c.Use(next => context =>
{
context.Result = context.Argument<DateTimeOffset>("a");
return Task.CompletedTask;
});
}).MakeExecutable();

// act
IExecutionResult result = await executor.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(@"
query a($d: DateTime!) {
a: a(a: ""2018-01-01T01:00:00.000Z"")
b: a(a: $d)
}")
.AddVariableValue("d", "2018-01-01T01:00:00.000Z")
.Create());

// assert
result.MatchSnapshot();
}

private Schema CreateSchema()
{
return Schema.Create(
Expand Down
4 changes: 3 additions & 1 deletion src/Server/AspNetCore/GetQueryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using HotChocolate.Execution;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

#if ASPNETCLASSIC
using Microsoft.Owin;
Expand Down Expand Up @@ -81,7 +82,8 @@ private static QueryRequestDto ReadRequest(HttpContext context)
NamedQuery = requestQuery[_namedQueryIdentifier],
OperationName = requestQuery[_operationNameIdentifier],
Variables = (variables != null && variables.Any())
? JObject.Parse(variables)
? JsonConvert.DeserializeObject<JObject>(
variables, QueryMiddlewareUtilities.JsonSettings)
: null
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Server/AspNetCore/PostQueryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ private static async Task<QueryRequestDto> ReadRequestAsync(
switch (context.Request.ContentType.Split(';')[0])
{
case ContentType.Json:
return JsonConvert
.DeserializeObject<QueryRequestDto>(content);
return JsonConvert.DeserializeObject<QueryRequestDto>(
content, QueryMiddlewareUtilities.JsonSettings);

case ContentType.GraphQL:
return new QueryRequestDto { Query = content };
Expand Down
7 changes: 7 additions & 0 deletions src/Server/AspNetCore/QueryMiddlewareUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

#if ASPNETCLASSIC
Expand All @@ -11,6 +12,12 @@ namespace HotChocolate.AspNetCore
{
internal static class QueryMiddlewareUtilities
{
public static JsonSerializerSettings JsonSettings { get; } =
new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
};

public static Dictionary<string, object> ToDictionary(
this JObject input)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ query a($id: ID!) {
Snapshot.Match(result);
}

[Fact(Skip = "Fix this issue")]
[Fact]
public async Task ExtendedScalarAsInAndOutputType()
{
// arrange
Expand Down Expand Up @@ -613,7 +613,57 @@ query a($d: DateTime!) {
}");
request.VariableValues = new Dictionary<string, object>
{
{"d", "2019-01-01T01:00"}
{"d", "2019-01-01T01:00:00.000Z"}
};
request.Services = scope.ServiceProvider;

result = await executor.ExecuteAsync(request);
}

// assert
Snapshot.Match(result);
}

[Fact]
public async Task DateTimeIsHandledCorrectly()
{
// arrange
IHttpClientFactory clientFactory = CreateRemoteSchemas();

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(clientFactory);
serviceCollection.AddStitchedSchema(builder =>
builder.AddSchemaFromHttp("contract")
.AddSchemaFromHttp("customer")
.AddExtensionsFromString(
"directive @custom(d: DateTime) on FIELD")
.AddSchemaConfiguration(c =>
{
c.RegisterExtendedScalarTypes();
})
.AddSchemaConfiguration(c =>
c.RegisterType<PaginationAmountType>()));

IServiceProvider services =
serviceCollection.BuildServiceProvider();

IQueryExecutor executor = services
.GetRequiredService<IQueryExecutor>();
IExecutionResult result = null;

// act
using (IServiceScope scope = services.CreateScope())
{
var request = new QueryRequest(@"
query a($d: DateTime!) {
a: extendedScalar(d: ""2018-01-01T01:00:00.000Z"")
b: extendedScalar(d: $d)
c: extendedScalar(d: $d)
@custom(d: ""2020-09-01T01:00:00.000Z"")
}");
request.VariableValues = new Dictionary<string, object>
{
{"d", "2019-01-01T01:00:00.000Z"}
};
request.Services = scope.ServiceProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Data": {
"a": "2018-01-01T01:00:00.000Z",
"b": "2019-01-01T01:00:00.000Z",
"c": "2020-09-01T01:00:00.000Z"
},
"Extensions": {},
"Errors": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Data": {
"a": "2018-01-01T01:00:00.000Z",
"b": "2019-01-01T01:00:00.000Z"
},
"Extensions": {},
"Errors": []
}
1 change: 1 addition & 0 deletions src/Stitching/Stitching/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal static class ErrorCodes
public const string FieldNotDefined = "STITCHING_FLD_NOT_DEFINED";
public const string VariableNotDefined = "STITCHING_VAR_NOT_DEFINED";
public const string ScopeNotDefined = "STITCHING_SCOPE_NOT_DEFINED";
public const string TypeNotDefined = "STITCHING_TYPE_NOT_DEFINED";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Stitching.Delegation;
using HotChocolate.Stitching.Properties;
using HotChocolate.Stitching.Utilities;
using HotChocolate.Types;
using HotChocolate.Utilities;

namespace HotChocolate.Stitching
{
Expand Down Expand Up @@ -90,7 +93,7 @@ private static IRemoteQueryRequest CreateQuery(

var requestBuilder = new RemoteQueryRequestBuilder();

AddVariables(context.Schema, schemaName,
AddVariables(context, schemaName,
requestBuilder, query, variableValues);

requestBuilder.SetQuery(query);
Expand Down Expand Up @@ -299,8 +302,22 @@ private static void ResolveScopedVariableArguments(

if (argument.Value is ScopedVariableNode sv)
{
variables.Add(_resolvers.Resolve(
context, sv, arg.Type.ToTypeNode()));
VariableValue variable =
_resolvers.Resolve(context, sv, arg.Type.ToTypeNode());

if (arg.Type.IsLeafType()
&& arg.Type.NamedType() is ISerializableType s)
{
variable = new VariableValue
(
variable.Name,
variable.Type,
s.Serialize(variable.Value),
variable.DefaultValue
);
}

variables.Add(variable);
}
}
}
Expand All @@ -326,7 +343,7 @@ private static IEnumerable<VariableValue> ResolveUsedRequestVariables(
}

private static void AddVariables(
ISchema schema,
IResolverContext context,
NameString schemaName,
IRemoteQueryRequestBuilder builder,
DocumentNode query,
Expand All @@ -344,15 +361,13 @@ private static void AddVariables(
{
object value = variableValue.Value;

if (schema.TryGetType(
if (context.Schema.TryGetType(
variableValue.Type.NamedType().Name.Value,
out InputObjectType inputType))
{
var wrapped = WrapType(inputType, variableValue.Type);
value = ObjectVariableRewriter.RewriteVariable(
schemaName,
WrapType(inputType,
variableValue.Type),
value);
schemaName, wrapped, value);
}

builder.AddVariableValue(variableValue.Name, value);
Expand Down
2 changes: 0 additions & 2 deletions src/Stitching/Stitching/Utilities/ObjectVariableRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,5 @@ private static object RewriteVariableValue(
return value;
}
}


}
}

0 comments on commit c6b8b00

Please sign in to comment.