diff --git a/src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs b/src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs index 1e766dd8ace..b9826b32223 100644 --- a/src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs +++ b/src/Types.Tests/Discovery/SchemaTypeDiscoveryTests.cs @@ -19,10 +19,16 @@ public void DiscoverInputArgumentTypes() }); // assert - IInputType fooInput = schema.GetType("FooInput"); + var foo = schema.GetType("Foo"); + Assert.NotNull(foo); + + var bar = schema.GetType("Bar"); + Assert.NotNull(foo); + + var fooInput = schema.GetType("FooInput"); Assert.NotNull(fooInput); - IInputType barInput = schema.GetType("BarInput"); + var barInput = schema.GetType("BarInput"); Assert.NotNull(barInput); } @@ -129,12 +135,14 @@ public void InferCustomScalarTypes() var fooByte = schema.GetType("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; diff --git a/src/Types.Tests/Types/UuidTypeTests.cs b/src/Types.Tests/Types/UuidTypeTests.cs index ee5f57f2ea9..e7c8d2fdffd 100644 --- a/src/Types.Tests/Types/UuidTypeTests.cs +++ b/src/Types.Tests/Types/UuidTypeTests.cs @@ -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); @@ -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); @@ -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 @@ -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); @@ -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 diff --git a/src/Types/Configuration/TypeInitializers/TypeRegistrar.cs b/src/Types/Configuration/TypeInitializers/TypeRegistrar.cs index 094f4b53394..4b2932cccfe 100644 --- a/src/Types/Configuration/TypeInitializers/TypeRegistrar.cs +++ b/src/Types/Configuration/TypeInitializers/TypeRegistrar.cs @@ -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)) { @@ -107,7 +108,7 @@ private void ProcessUnresolvedTypes(ITypeRegistry typeRegistry) typeRegistry.RegisterType( new TypeReference(typeof(EnumType<>) .MakeGenericType(unresolvedType.ClrType))); - } + } } } diff --git a/src/Types/Configuration/TypeRegistry.GetType.cs b/src/Types/Configuration/TypeRegistry.GetType.cs index 43769dcb41e..36587206212 100644 --- a/src/Types/Configuration/TypeRegistry.GetType.cs +++ b/src/Types/Configuration/TypeRegistry.GetType.cs @@ -67,7 +67,11 @@ private bool TryGetTypeFromClrType( 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; } @@ -233,15 +237,18 @@ public IEnumerable 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 associated)) { foreach (NameString name in associated) { - switch (unresolvedType.Context) + switch (context) { case TypeContext.Input: if (IsInputType(name)) diff --git a/src/Types/Configuration/TypeRegistry.RegisterType.cs b/src/Types/Configuration/TypeRegistry.RegisterType.cs index 1b46caa805f..78fa07da6ac 100644 --- a/src/Types/Configuration/TypeRegistry.RegisterType.cs +++ b/src/Types/Configuration/TypeRegistry.RegisterType.cs @@ -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)); diff --git a/src/Types/Configuration/TypeRegistry.cs b/src/Types/Configuration/TypeRegistry.cs index 97196d9325a..4ad77acfa53 100644 --- a/src/Types/Configuration/TypeRegistry.cs +++ b/src/Types/Configuration/TypeRegistry.cs @@ -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() diff --git a/src/Types/Resolvers/FieldReferenceBase.cs b/src/Types/Resolvers/FieldReferenceBase.cs index 2f823ebb3d6..397ae638d7a 100644 --- a/src/Types/Resolvers/FieldReferenceBase.cs +++ b/src/Types/Resolvers/FieldReferenceBase.cs @@ -1,5 +1,4 @@ using System; -using HotChocolate.Utilities; namespace HotChocolate.Resolvers { diff --git a/src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs b/src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs index 1384e948252..89f6d3e2b9b 100644 --- a/src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs +++ b/src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs @@ -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"; } diff --git a/src/Types/Types/FieldBase.cs b/src/Types/Types/FieldBase.cs index 97aaa5fc406..8b04863bbc3 100644 --- a/src/Types/Types/FieldBase.cs +++ b/src/Types/Types/FieldBase.cs @@ -63,6 +63,7 @@ protected override void OnRegisterDependencies( if (TypeReference != null) { + string s = TypeReference.ToString(); context.RegisterType(TypeReference); } } diff --git a/src/Types/Types/TypeReference.cs b/src/Types/Types/TypeReference.cs index ace332694f0..45f555614aa 100644 --- a/src/Types/Types/TypeReference.cs +++ b/src/Types/Types/TypeReference.cs @@ -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 diff --git a/src/Types/Utilities/DotNetTypeInfoFactory.cs b/src/Types/Utilities/DotNetTypeInfoFactory.cs index 42304f9d3e1..0a0b61357d0 100644 --- a/src/Types/Utilities/DotNetTypeInfoFactory.cs +++ b/src/Types/Utilities/DotNetTypeInfoFactory.cs @@ -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; @@ -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 components, out TypeInfo typeInfo) { @@ -85,7 +88,7 @@ private static bool TryCreate2ComponentType( } private static bool TryCreate1ComponentType( - List components, out TypeInfo typeInfo) + List components, out TypeInfo typeInfo) { if (components.Count == 1) { @@ -108,7 +111,7 @@ private static bool TryCreate1ComponentType( private static List DecomposeType(Type type) { - List components = new List(); + var components = new List(); Type current = RemoveNonEssentialParts(type); do @@ -138,7 +141,7 @@ private static Type RemoveNonEssentialParts(Type type) current = GetInnerType(current); } - if (IsResoverResultType(current)) + if (IsResolverResultType(current)) { current = GetInnerType(current); } @@ -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(); } @@ -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()