Skip to content

Commit

Permalink
Fixed clr type binding for issue with new type discovery. (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 17, 2019
1 parent 1252157 commit 3d8fa25
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/Core/Types.Tests/CodeFirstTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ public void Type_Is_Correctly_Upgraded()
.MatchSnapshot();
}

[Fact]
public void Change_DefaultBinding_For_DateTime()
{
SchemaBuilder.New()
.AddQueryType<QueryWithDateTimeType>()
.BindClrType<DateTime, DateTimeType>()
.Create()
.ToString()
.MatchSnapshot();
}

[Fact]
public void Remove_ClrType_Bindings_That_Are_Not_Used()
{
// arrange
// act
ISchema schema = SchemaBuilder.New()
.AddQueryType<QueryWithDateTimeType>()
.BindClrType<DateTime, DateTimeType>()
.BindClrType<int, UrlType>()
.ModifyOptions(o => o.RemoveUnreachableTypes = true)
.Create();

// assert
bool exists = schema.TryGetType("Url", out INamedType _);
Assert.False(exists);
}

public class Query
{
public string SayHello(string name) =>
Expand Down Expand Up @@ -72,5 +100,33 @@ public class Dog : IPet
public string? Name =>
throw new NotImplementedException();
}

public class QueryWithDateTimeType : ObjectType<QueryWithDateTime>
{
protected override void Configure(IObjectTypeDescriptor<QueryWithDateTime> descriptor)
{
descriptor.Field(t => t.GetModel()).Type<ModelWithDateTimeType>();
}
}

public class QueryWithDateTime
{
public ModelWithDateTime GetModel() => new ModelWithDateTime();
}

public class ModelWithDateTimeType : ObjectType<ModelWithDateTime>
{
protected override void Configure(IObjectTypeDescriptor<ModelWithDateTime> descriptor)
{
descriptor.Field(t => t.Foo).Type<DateType>();
}
}

public class ModelWithDateTime
{
public DateTime Foo { get; set; }

public DateTime Bar { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
schema {
query: QueryWithDateTime
}

type ModelWithDateTime {
bar: DateTime!
foo: Date
}

type QueryWithDateTime {
model: ModelWithDateTime
}

"The `Date` scalar represents an ISO-8601 compliant date type."
scalar Date

"The `DateTime` scalar represents an ISO-8601 compliant date time type."
scalar DateTime
2 changes: 1 addition & 1 deletion src/Core/Types/Configuration/ClrTypeReferenceHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Utilities;
Expand Down
1 change: 1 addition & 0 deletions src/Core/Types/Configuration/TypeDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public TypeDiscoverer(
{
_unregistered.AddRange(IntrospectionTypes.All);
_unregistered.AddRange(Directives.All);
_unregistered.AddRange(clrTypeReferences.Values);
_unregistered.AddRange(initialTypes);

_clrTypeReferences = clrTypeReferences;
Expand Down

0 comments on commit 3d8fa25

Please sign in to comment.