Skip to content

Commit f3623c9

Browse files
committed
Added Primary Constructors to Configuration Handlers
1 parent b8209ef commit f3623c9

8 files changed

+63
-83
lines changed

src/HotChocolate/Core/src/Types/Configuration/Handlers/DefaultTypeDiscoveryHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99

1010
namespace HotChocolate.Configuration;
1111

12-
internal sealed class DefaultTypeDiscoveryHandler : TypeDiscoveryHandler
12+
internal sealed class DefaultTypeDiscoveryHandler(ITypeInspector typeInspector) : TypeDiscoveryHandler
1313
{
14-
public DefaultTypeDiscoveryHandler(ITypeInspector typeInspector)
15-
{
16-
TypeInspector = typeInspector ?? throw new ArgumentNullException(nameof(typeInspector));
17-
}
18-
19-
private ITypeInspector TypeInspector { get; }
14+
private ITypeInspector TypeInspector { get; } =
15+
typeInspector ?? throw new ArgumentNullException(nameof(typeInspector));
2016

2117
public override bool TryInferType(
2218
TypeReference typeReference,

src/HotChocolate/Core/src/Types/Configuration/Handlers/DependantFactoryTypeReferenceHandler.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
namespace HotChocolate.Configuration;
66

7-
internal sealed class DependantFactoryTypeReferenceHandler : ITypeRegistrarHandler
7+
internal sealed class DependantFactoryTypeReferenceHandler(IDescriptorContext context)
8+
: ITypeRegistrarHandler
89
{
9-
private readonly HashSet<DependantFactoryTypeReference> _handled = new();
10-
private readonly IDescriptorContext _context;
11-
12-
public DependantFactoryTypeReferenceHandler(IDescriptorContext context)
13-
{
14-
_context = context ?? throw new ArgumentNullException(nameof(context));
15-
}
10+
private readonly HashSet<DependantFactoryTypeReference> _handled = [];
11+
private readonly IDescriptorContext _context = context ?? throw new ArgumentNullException(nameof(context));
1612

1713
public TypeReferenceKind Kind => TypeReferenceKind.DependantFactory;
1814

src/HotChocolate/Core/src/Types/Configuration/Handlers/ExtendedTypeDirectiveReferenceHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44

55
namespace HotChocolate.Configuration;
66

7-
internal sealed class ExtendedTypeDirectiveReferenceHandler : ITypeRegistrarHandler
7+
internal sealed class ExtendedTypeDirectiveReferenceHandler(ITypeInspector typeInspector)
8+
: ITypeRegistrarHandler
89
{
9-
private readonly ITypeInspector _typeInspector;
10-
11-
public ExtendedTypeDirectiveReferenceHandler(ITypeInspector typeInspector)
12-
=> _typeInspector = typeInspector;
13-
1410
public TypeReferenceKind Kind => TypeReferenceKind.DirectiveExtendedType;
1511

1612
public void Handle(ITypeRegistrar typeRegistrar, TypeReference typeReference)
1713
{
1814
var typeRef = (ExtendedTypeDirectiveReference)typeReference;
1915

20-
if (_typeInspector.TryCreateTypeInfo(typeRef.Type, out var typeInfo) &&
16+
if (typeInspector.TryCreateTypeInfo(typeRef.Type, out var typeInfo) &&
2117
!ExtendedType.Tools.IsSchemaType(typeInfo.NamedType) &&
2218
!typeRegistrar.IsResolved(typeRef))
2319
{

src/HotChocolate/Core/src/Types/Configuration/Handlers/ExtendedTypeReferenceHandler.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,40 @@ public void Handle(ITypeRegistrar typeRegistrar, TypeReference typeReference)
1717
{
1818
var typeRef = (ExtendedTypeReference)typeReference;
1919

20-
if (typeInspector.TryCreateTypeInfo(typeRef.Type, out var typeInfo) &&
21-
!ExtendedType.Tools.IsNonGenericBaseType(typeInfo.NamedType))
20+
if (!typeInspector.TryCreateTypeInfo(typeRef.Type, out var typeInfo) ||
21+
ExtendedType.Tools.IsNonGenericBaseType(typeInfo.NamedType))
2222
{
23-
if (typeInfo.NamedType == typeof(IExecutable))
24-
{
25-
throw ThrowHelper.NonGenericExecutableNotAllowed();
26-
}
23+
return;
24+
}
2725

28-
var namedType = typeInfo.NamedType;
26+
if (typeInfo.NamedType == typeof(IExecutable))
27+
{
28+
throw ThrowHelper.NonGenericExecutableNotAllowed();
29+
}
2930

30-
if (IsTypeSystemObject(namedType))
31-
{
32-
var extendedType = typeInspector.GetType(namedType);
33-
var namedTypeReference = typeRef.With(extendedType);
31+
var namedType = typeInfo.NamedType;
3432

35-
if (!typeRegistrar.IsResolved(namedTypeReference))
36-
{
37-
typeRegistrar.Register(
38-
typeRegistrar.CreateInstance(namedType),
39-
typeReference.Scope,
40-
ExtendedType.Tools.IsGenericBaseType(namedType));
41-
}
42-
}
43-
else
33+
if (IsTypeSystemObject(namedType))
34+
{
35+
var extendedType = typeInspector.GetType(namedType);
36+
var namedTypeReference = typeRef.With(extendedType);
37+
38+
if (!typeRegistrar.IsResolved(namedTypeReference))
4439
{
45-
TryMapToExistingRegistration(
46-
typeRegistrar,
47-
typeInfo,
48-
typeReference.Context,
49-
typeReference.Scope);
40+
typeRegistrar.Register(
41+
typeRegistrar.CreateInstance(namedType),
42+
typeReference.Scope,
43+
ExtendedType.Tools.IsGenericBaseType(namedType));
5044
}
5145
}
46+
else
47+
{
48+
TryMapToExistingRegistration(
49+
typeRegistrar,
50+
typeInfo,
51+
typeReference.Context,
52+
typeReference.Scope);
53+
}
5254
}
5355

5456
private static void TryMapToExistingRegistration(

src/HotChocolate/Core/src/Types/Configuration/Handlers/FactoryTypeReferenceHandler.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
namespace HotChocolate.Configuration;
66

7-
internal sealed class FactoryTypeReferenceHandler : ITypeRegistrarHandler
7+
internal sealed class FactoryTypeReferenceHandler(IDescriptorContext context)
8+
: ITypeRegistrarHandler
89
{
9-
private readonly HashSet<string> _handled = new();
10-
private readonly IDescriptorContext _context;
11-
12-
public FactoryTypeReferenceHandler(IDescriptorContext context)
13-
{
14-
_context = context ?? throw new ArgumentNullException(nameof(context));
15-
}
10+
private readonly HashSet<string> _handled = [];
11+
private readonly IDescriptorContext _context = context ?? throw new ArgumentNullException(nameof(context));
1612

1713
public TypeReferenceKind Kind => TypeReferenceKind.Factory;
1814

src/HotChocolate/Core/src/Types/Configuration/Handlers/ScalarTypeDiscoveryHandler.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
namespace HotChocolate.Configuration;
1010

11-
internal sealed class ScalarTypeDiscoveryHandler : TypeDiscoveryHandler
11+
internal sealed class ScalarTypeDiscoveryHandler(ITypeInspector typeInspector)
12+
: TypeDiscoveryHandler
1213
{
13-
public ScalarTypeDiscoveryHandler(ITypeInspector typeInspector)
14-
{
15-
TypeInspector = typeInspector ?? throw new ArgumentNullException(nameof(typeInspector));
16-
}
17-
18-
private ITypeInspector TypeInspector { get; }
14+
private ITypeInspector TypeInspector { get; } =
15+
typeInspector ?? throw new ArgumentNullException(nameof(typeInspector));
1916

2017
public override bool TryInferType(
2118
TypeReference typeReference,
@@ -25,7 +22,7 @@ public override bool TryInferType(
2522
if (Scalars.TryGetScalar(typeInfo.RuntimeType, out var scalarType))
2623
{
2724
var schemaTypeRef = TypeInspector.GetTypeRef(scalarType);
28-
schemaTypeRefs = new TypeReference[] { schemaTypeRef };
25+
schemaTypeRefs = [schemaTypeRef];
2926
return true;
3027
}
3128

src/HotChocolate/Core/src/Types/Configuration/Handlers/SchemaTypeReferenceHandler.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ public void Handle(ITypeRegistrar typeRegistrar, TypeReference typeReference)
1313
{
1414
var typeRef = (SchemaTypeReference)typeReference;
1515

16-
if (!typeRegistrar.IsResolved(typeReference))
16+
if (typeRegistrar.IsResolved(typeReference))
1717
{
18-
var tsm = typeRef.Type;
18+
return;
19+
}
20+
21+
var tsm = typeRef.Type;
1922

20-
// if it is a type object we will make sure it is unwrapped.
21-
if (typeRef.Type is IType type)
22-
{
23-
tsm = type.NamedType();
24-
}
23+
// if it is a type object we will make sure it is unwrapped.
24+
if (typeRef.Type is IType type)
25+
{
26+
tsm = type.NamedType();
27+
}
2528

26-
if (tsm is TypeSystemObjectBase tso)
27-
{
28-
typeRegistrar.Register(tso, typeReference.Scope);
29-
}
29+
if (tsm is TypeSystemObjectBase tso)
30+
{
31+
typeRegistrar.Register(tso, typeReference.Scope);
3032
}
3133
}
3234
}

src/HotChocolate/Core/src/Types/Configuration/Handlers/SyntaxTypeReferenceHandler.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77

88
namespace HotChocolate.Configuration;
99

10-
internal sealed class SyntaxTypeReferenceHandler : ITypeRegistrarHandler
10+
internal sealed class SyntaxTypeReferenceHandler(ITypeInspector typeInspector) : ITypeRegistrarHandler
1111
{
12-
private readonly HashSet<string> _handled = new();
13-
private readonly ITypeInspector _typeInspector;
14-
15-
public SyntaxTypeReferenceHandler(ITypeInspector typeInspector)
16-
{
17-
_typeInspector = typeInspector ??
18-
throw new ArgumentNullException(nameof(typeInspector));
19-
}
12+
private readonly HashSet<string> _handled = [];
13+
private readonly ITypeInspector _typeInspector = typeInspector ??
14+
throw new ArgumentNullException(nameof(typeInspector));
2015

2116
public TypeReferenceKind Kind => TypeReferenceKind.Syntax;
2217

0 commit comments

Comments
 (0)