Skip to content

Commit 1784e7c

Browse files
authored
Fixed: The type discoverer ignored a type if it was already discovered in another context. (#350)
1 parent 65924ec commit 1784e7c

File tree

11 files changed

+56
-49
lines changed

11 files changed

+56
-49
lines changed

src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ public void DiscoverInputArgumentTypes()
1919
});
2020

2121
// assert
22-
IInputType fooInput = schema.GetType<INamedInputType>("FooInput");
22+
var foo = schema.GetType<INamedOutputType>("Foo");
23+
Assert.NotNull(foo);
24+
25+
var bar = schema.GetType<INamedOutputType>("Bar");
26+
Assert.NotNull(foo);
27+
28+
var fooInput = schema.GetType<INamedInputType>("FooInput");
2329
Assert.NotNull(fooInput);
2430

25-
IInputType barInput = schema.GetType<INamedInputType>("BarInput");
31+
var barInput = schema.GetType<INamedInputType>("BarInput");
2632
Assert.NotNull(barInput);
2733
}
2834

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

132-
ObjectField field = fooByte.Fields["bar"];
138+
ObjectField field = fooByte.Fields["bar"];
133139
Assert.Equal("ByteArray", field.Type.NamedType().Name);
134140
}
135141

136142
public class QueryFieldArgument
137143
{
144+
public Bar Bar { get; }
145+
138146
public Foo GetFoo(Foo foo)
139147
{
140148
return foo;

src/Types.Tests/Types/UuidTypeTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public void Serialize_Guid()
1111
{
1212
// arrange
1313
var uuidType = new UuidType();
14-
Guid guid = Guid.NewGuid();
15-
string expectedValue = guid.ToString("N");
14+
var guid = Guid.NewGuid();
15+
var expectedValue = guid.ToString("N");
1616

1717
// act
18-
string serializedValue = (string)uuidType.Serialize(guid);
18+
var serializedValue = (string)uuidType.Serialize(guid);
1919

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

3030
// act
31-
object serializedValue = uuidType.Serialize(null);
31+
var serializedValue = uuidType.Serialize(null);
3232

3333
// assert
3434
Assert.Null(serializedValue);
@@ -39,11 +39,11 @@ public void ParseLiteral_StringValueNode()
3939
{
4040
// arrange
4141
var uuidType = new UuidType();
42-
Guid expected = Guid.NewGuid();
42+
var expected = Guid.NewGuid();
4343
var literal = new StringValueNode(expected.ToString());
4444

4545
// act
46-
Guid actual = (Guid)uuidType
46+
var actual = (Guid)uuidType
4747
.ParseLiteral(literal);
4848

4949
// assert
@@ -58,7 +58,7 @@ public void ParseLiteral_NullValueNode()
5858
NullValueNode literal = NullValueNode.Default;
5959

6060
// act
61-
object value = uuidType.ParseLiteral(literal);
61+
var value = uuidType.ParseLiteral(literal);
6262

6363
// assert
6464
Assert.Null(value);
@@ -69,11 +69,11 @@ public void ParseValue_Guid()
6969
{
7070
// arrange
7171
var uuidType = new UuidType();
72-
Guid expected = Guid.NewGuid();
73-
string expectedLiteralValue = expected.ToString("N");
72+
var expected = Guid.NewGuid();
73+
var expectedLiteralValue = expected.ToString("N");
7474

7575
// act
76-
StringValueNode stringLiteral =
76+
var stringLiteral =
7777
(StringValueNode)uuidType.ParseValue(expected);
7878

7979
// assert

src/Types/Configuration/TypeInitializers/TypeRegistrar.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private void ProcessBatch(
8888

8989
private void ProcessUnresolvedTypes(ITypeRegistry typeRegistry)
9090
{
91-
foreach (TypeReference unresolvedType in typeRegistry.GetUnresolvedTypes())
91+
foreach (TypeReference unresolvedType in
92+
typeRegistry.GetUnresolvedTypes())
9293
{
9394
if (IsObjectType(unresolvedType))
9495
{
@@ -107,7 +108,7 @@ private void ProcessUnresolvedTypes(ITypeRegistry typeRegistry)
107108
typeRegistry.RegisterType(
108109
new TypeReference(typeof(EnumType<>)
109110
.MakeGenericType(unresolvedType.ClrType)));
110-
}
111+
}
111112
}
112113
}
113114

src/Types/Configuration/TypeRegistry.GetType.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ private bool TryGetTypeFromClrType<T>(
6767
TypeContext context,
6868
out T type)
6969
{
70-
if (TryGetTypeFromClrType(clrType, context, t => t, out type))
70+
if (TryGetTypeFromClrType(
71+
DotNetTypeInfoFactory.Unwrap(clrType),
72+
context,
73+
t => t,
74+
out type))
7175
{
7276
return true;
7377
}
@@ -233,15 +237,18 @@ public IEnumerable<TypeReference> GetUnresolvedTypes()
233237
return _unresolvedTypes;
234238
}
235239

236-
private bool IsTypeResolved(TypeReference unresolvedType)
240+
private bool IsTypeResolved(TypeReference typeReference) =>
241+
IsTypeResolved(typeReference.ClrType, typeReference.Context);
242+
243+
244+
private bool IsTypeResolved(Type clrType, TypeContext context)
237245
{
238-
if (_clrTypes.TryGetValue(
239-
unresolvedType.ClrType,
246+
if (_clrTypes.TryGetValue(clrType,
240247
out HashSet<NameString> associated))
241248
{
242249
foreach (NameString name in associated)
243250
{
244-
switch (unresolvedType.Context)
251+
switch (context)
245252
{
246253
case TypeContext.Input:
247254
if (IsInputType(name))

src/Types/Configuration/TypeRegistry.RegisterType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ private void RegisterType(Type type, TypeContext context)
4747
{
4848
if (typeof(INamedType).IsAssignableFrom(typeInfo.ClrType))
4949
{
50-
INamedType namedType = (INamedType)_serviceFactory
50+
var namedType = (INamedType)_serviceFactory
5151
.CreateInstance(typeInfo.ClrType);
5252
TryUpdateNamedType(namedType);
5353
}
54-
else if (!_clrTypes.ContainsKey(typeInfo.ClrType))
54+
else if (!IsTypeResolved(typeInfo.ClrType, context))
5555
{
5656
_unresolvedTypes.Add(
5757
new TypeReference(typeInfo.ClrType, context));

src/Types/Configuration/TypeRegistry.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ internal partial class TypeRegistry
2525

2626
public TypeRegistry(ServiceFactory serviceFactory)
2727
{
28-
if (serviceFactory == null)
29-
{
30-
throw new ArgumentNullException(nameof(serviceFactory));
31-
}
32-
33-
_serviceFactory = serviceFactory;
28+
_serviceFactory = serviceFactory
29+
?? throw new ArgumentNullException(nameof(serviceFactory));
3430
}
3531

3632
public void CompleteRegistartion()

src/Types/Resolvers/FieldReferenceBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using HotChocolate.Utilities;
32

43
namespace HotChocolate.Resolvers
54
{

src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public InputObjectTypeDescriptor(Type clrType)
102102
// this convention will fix most type colisions where the
103103
// .net type is and input and an output type.
104104
// It is still possible to opt out via the descriptor.Name("Foo").
105-
if (!ObjectDescription.Name.EndsWith("Input"))
105+
if (!ObjectDescription.Name.EndsWith("Input",
106+
StringComparison.Ordinal))
106107
{
107108
ObjectDescription.Name = ObjectDescription.Name + "Input";
108109
}

src/Types/Types/FieldBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected override void OnRegisterDependencies(
6363

6464
if (TypeReference != null)
6565
{
66+
string s = TypeReference.ToString();
6667
context.RegisterType(TypeReference);
6768
}
6869
}

src/Types/Types/TypeReference.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,12 @@ public override int GetHashCode()
7171
return (ClrType.GetHashCode() * 397)
7272
^ (Context.GetHashCode() * 97);
7373
}
74-
else
75-
{
76-
return Type.GetHashCode();
77-
}
74+
return Type.GetHashCode();
7875
}
7976
}
8077

81-
public override string ToString()
82-
{
83-
if (ClrType == null)
84-
{
85-
return Type.ToString();
86-
}
87-
return ClrType.GetTypeName();
88-
}
78+
public override string ToString() =>
79+
ClrType == null ? Type.ToString() : ClrType.GetTypeName();
8980
}
9081

9182
internal static class TypeReferenceExtensions

0 commit comments

Comments
 (0)