Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: The type discoverer ignored a type if it was already discovered in another context. #350

Merged
merged 3 commits into from
Nov 20, 2018
Merged
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
14 changes: 11 additions & 3 deletions src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ public void DiscoverInputArgumentTypes()
});

// assert
IInputType fooInput = schema.GetType<INamedInputType>("FooInput");
var foo = schema.GetType<INamedOutputType>("Foo");
Assert.NotNull(foo);

var bar = schema.GetType<INamedOutputType>("Bar");
Assert.NotNull(foo);

var fooInput = schema.GetType<INamedInputType>("FooInput");
Assert.NotNull(fooInput);

IInputType barInput = schema.GetType<INamedInputType>("BarInput");
var barInput = schema.GetType<INamedInputType>("BarInput");
Assert.NotNull(barInput);
}

Expand Down Expand Up @@ -129,12 +135,14 @@ public void InferCustomScalarTypes()
var fooByte = schema.GetType<ObjectType>("FooByte");
Assert.NotNull(fooByte);

ObjectField field = fooByte.Fields["bar"];
ObjectField field = fooByte.Fields["bar"];
Assert.Equal("ByteArray", field.Type.NamedType().Name);
}

public class QueryFieldArgument
{
public Bar Bar { get; }

public Foo GetFoo(Foo foo)
{
return foo;
Expand Down
20 changes: 10 additions & 10 deletions src/Types.Tests/Types/UuidTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public void Serialize_Guid()
{
// arrange
var uuidType = new UuidType();
Guid guid = Guid.NewGuid();
string expectedValue = guid.ToString("N");
var guid = Guid.NewGuid();
var expectedValue = guid.ToString("N");

// act
string serializedValue = (string)uuidType.Serialize(guid);
var serializedValue = (string)uuidType.Serialize(guid);

// assert
Assert.Equal(expectedValue, serializedValue);
Expand All @@ -28,7 +28,7 @@ public void Serialize_Null()
var uuidType = new UuidType();

// act
object serializedValue = uuidType.Serialize(null);
var serializedValue = uuidType.Serialize(null);

// assert
Assert.Null(serializedValue);
Expand All @@ -39,11 +39,11 @@ public void ParseLiteral_StringValueNode()
{
// arrange
var uuidType = new UuidType();
Guid expected = Guid.NewGuid();
var expected = Guid.NewGuid();
var literal = new StringValueNode(expected.ToString());

// act
Guid actual = (Guid)uuidType
var actual = (Guid)uuidType
.ParseLiteral(literal);

// assert
Expand All @@ -58,7 +58,7 @@ public void ParseLiteral_NullValueNode()
NullValueNode literal = NullValueNode.Default;

// act
object value = uuidType.ParseLiteral(literal);
var value = uuidType.ParseLiteral(literal);

// assert
Assert.Null(value);
Expand All @@ -69,11 +69,11 @@ public void ParseValue_Guid()
{
// arrange
var uuidType = new UuidType();
Guid expected = Guid.NewGuid();
string expectedLiteralValue = expected.ToString("N");
var expected = Guid.NewGuid();
var expectedLiteralValue = expected.ToString("N");

// act
StringValueNode stringLiteral =
var stringLiteral =
(StringValueNode)uuidType.ParseValue(expected);

// assert
Expand Down
5 changes: 3 additions & 2 deletions src/Types/Configuration/TypeInitializers/TypeRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private void ProcessBatch(

private void ProcessUnresolvedTypes(ITypeRegistry typeRegistry)
{
foreach (TypeReference unresolvedType in typeRegistry.GetUnresolvedTypes())
foreach (TypeReference unresolvedType in
typeRegistry.GetUnresolvedTypes())
{
if (IsObjectType(unresolvedType))
{
Expand All @@ -107,7 +108,7 @@ private void ProcessUnresolvedTypes(ITypeRegistry typeRegistry)
typeRegistry.RegisterType(
new TypeReference(typeof(EnumType<>)
.MakeGenericType(unresolvedType.ClrType)));
}
}
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/Types/Configuration/TypeRegistry.GetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ private bool TryGetTypeFromClrType<T>(
TypeContext context,
out T type)
{
if (TryGetTypeFromClrType(clrType, context, t => t, out type))
if (TryGetTypeFromClrType(
DotNetTypeInfoFactory.Unwrap(clrType),
context,
t => t,
out type))
{
return true;
}
Expand Down Expand Up @@ -233,15 +237,18 @@ public IEnumerable<TypeReference> GetUnresolvedTypes()
return _unresolvedTypes;
}

private bool IsTypeResolved(TypeReference unresolvedType)
private bool IsTypeResolved(TypeReference typeReference) =>
IsTypeResolved(typeReference.ClrType, typeReference.Context);


private bool IsTypeResolved(Type clrType, TypeContext context)
{
if (_clrTypes.TryGetValue(
unresolvedType.ClrType,
if (_clrTypes.TryGetValue(clrType,
out HashSet<NameString> associated))
{
foreach (NameString name in associated)
{
switch (unresolvedType.Context)
switch (context)
{
case TypeContext.Input:
if (IsInputType(name))
Expand Down
4 changes: 2 additions & 2 deletions src/Types/Configuration/TypeRegistry.RegisterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ private void RegisterType(Type type, TypeContext context)
{
if (typeof(INamedType).IsAssignableFrom(typeInfo.ClrType))
{
INamedType namedType = (INamedType)_serviceFactory
var namedType = (INamedType)_serviceFactory
.CreateInstance(typeInfo.ClrType);
TryUpdateNamedType(namedType);
}
else if (!_clrTypes.ContainsKey(typeInfo.ClrType))
else if (!IsTypeResolved(typeInfo.ClrType, context))
{
_unresolvedTypes.Add(
new TypeReference(typeInfo.ClrType, context));
Expand Down
8 changes: 2 additions & 6 deletions src/Types/Configuration/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ internal partial class TypeRegistry

public TypeRegistry(ServiceFactory serviceFactory)
{
if (serviceFactory == null)
{
throw new ArgumentNullException(nameof(serviceFactory));
}

_serviceFactory = serviceFactory;
_serviceFactory = serviceFactory
?? throw new ArgumentNullException(nameof(serviceFactory));
}

public void CompleteRegistartion()
Expand Down
1 change: 0 additions & 1 deletion src/Types/Resolvers/FieldReferenceBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using HotChocolate.Utilities;

namespace HotChocolate.Resolvers
{
Expand Down
3 changes: 2 additions & 1 deletion src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public InputObjectTypeDescriptor(Type clrType)
// this convention will fix most type colisions where the
// .net type is and input and an output type.
// It is still possible to opt out via the descriptor.Name("Foo").
if (!ObjectDescription.Name.EndsWith("Input"))
if (!ObjectDescription.Name.EndsWith("Input",
StringComparison.Ordinal))
{
ObjectDescription.Name = ObjectDescription.Name + "Input";
}
Expand Down
1 change: 1 addition & 0 deletions src/Types/Types/FieldBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected override void OnRegisterDependencies(

if (TypeReference != null)
{
string s = TypeReference.ToString();
context.RegisterType(TypeReference);
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/Types/Types/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,12 @@ public override int GetHashCode()
return (ClrType.GetHashCode() * 397)
^ (Context.GetHashCode() * 97);
}
else
{
return Type.GetHashCode();
}
return Type.GetHashCode();
}
}

public override string ToString()
{
if (ClrType == null)
{
return Type.ToString();
}
return ClrType.GetTypeName();
}
public override string ToString() =>
ClrType == null ? Type.ToString() : ClrType.GetTypeName();
}

internal static class TypeReferenceExtensions
Expand Down
17 changes: 10 additions & 7 deletions src/Types/Utilities/DotNetTypeInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using HotChocolate.Types;

Expand Down Expand Up @@ -35,6 +33,11 @@ public bool TryCreate(Type type, out TypeInfo typeInfo)
return false;
}

public static Type Unwrap(Type type)
{
return RemoveNonEssentialParts(type);
}

private static bool TryCreate3ComponentType(
List<Type> components, out TypeInfo typeInfo)
{
Expand Down Expand Up @@ -85,7 +88,7 @@ private static bool TryCreate2ComponentType(
}

private static bool TryCreate1ComponentType(
List<Type> components, out TypeInfo typeInfo)
List<Type> components, out TypeInfo typeInfo)
{
if (components.Count == 1)
{
Expand All @@ -108,7 +111,7 @@ private static bool TryCreate1ComponentType(

private static List<Type> DecomposeType(Type type)
{
List<Type> components = new List<Type>();
var components = new List<Type>();
Type current = RemoveNonEssentialParts(type);

do
Expand Down Expand Up @@ -138,7 +141,7 @@ private static Type RemoveNonEssentialParts(Type type)
current = GetInnerType(current);
}

if (IsResoverResultType(current))
if (IsResolverResultType(current))
{
current = GetInnerType(current);
}
Expand Down Expand Up @@ -173,7 +176,7 @@ private static Type GetInnerType(Type type)
if (IsTaskType(type)
|| IsNullableType(type)
|| IsWrapperType(type)
|| IsResoverResultType(type))
|| IsResolverResultType(type))
{
return type.GetGenericArguments().First();
}
Expand Down Expand Up @@ -243,7 +246,7 @@ private static bool IsTaskType(Type type)
&& typeof(Task<>) == type.GetGenericTypeDefinition();
}

private static bool IsResoverResultType(Type type)
private static bool IsResolverResultType(Type type)
{
return type.IsGenericType
&& (typeof(IResolverResult<>) == type.GetGenericTypeDefinition()
Expand Down