Skip to content

Commit a0207c0

Browse files
committed
Added generic NamedType helper. (#7923)
1 parent 239719c commit a0207c0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/HotChocolate/Core/src/Types/Types/Extensions/TypeExtensions.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,54 @@ public static ListType ListType(this IType type)
459459
throw new ArgumentException(TypeResources.TypeExtensions_InvalidStructure);
460460
}
461461

462+
/// <summary>
463+
/// Gets the named type (the most inner type) from a type structure.
464+
/// </summary>
465+
/// <param name="type">
466+
/// The type from which the named type shall be extracted.
467+
/// </param>
468+
/// <typeparam name="T">
469+
/// The expected type of the named type.
470+
/// </typeparam>
471+
/// <returns>
472+
/// Returns the named type.
473+
/// </returns>
474+
/// <exception cref="ArgumentNullException">
475+
/// <paramref name="type"/> is <c>null</c>.
476+
/// </exception>
477+
/// <exception cref="ArgumentException">
478+
/// The type structure is invalid or
479+
/// the named type is not of the expected type.
480+
/// </exception>
481+
public static T NamedType<T>(this IType type) where T : INamedType
482+
{
483+
var namedType = type.NamedType();
484+
485+
if(namedType is T t)
486+
{
487+
return t;
488+
}
489+
490+
throw new ArgumentException(
491+
"The named type is not of the expected type.",
492+
nameof(type));
493+
}
494+
495+
/// <summary>
496+
/// Gets the named type (the most inner type) from a type structure.
497+
/// </summary>
498+
/// <param name="type">
499+
/// The type from which the named type shall be extracted.
500+
/// </param>
501+
/// <returns>
502+
/// Returns the named type.
503+
/// </returns>
504+
/// <exception cref="ArgumentNullException">
505+
/// <paramref name="type"/> is <c>null</c>.
506+
/// </exception>
507+
/// <exception cref="ArgumentException">
508+
/// The type structure is invalid.
509+
/// </exception>
462510
public static INamedType NamedType(this IType type)
463511
{
464512
if (type is null)

src/HotChocolate/Core/test/Types.Tests/Types/TypeExtensionsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ public static void NamedType()
103103
Assert.NotNull(stringType);
104104
}
105105

106+
[Fact]
107+
public static void NamedType_Of_T()
108+
{
109+
// arrange
110+
var type = new NonNullType(
111+
new ListType(
112+
new NonNullType(
113+
new StringType())));
114+
115+
// act
116+
var stringType = type.NamedType<StringType>();
117+
118+
// assert
119+
Assert.NotNull(stringType);
120+
}
121+
122+
[Fact]
123+
public static void NamedType_Of_T_Is_Not_Of_T()
124+
{
125+
// arrange
126+
var type = new NonNullType(
127+
new ListType(
128+
new NonNullType(
129+
new StringType())));
130+
131+
// act & assert
132+
Assert.Throws<ArgumentException>(() => type.NamedType<ObjectType>());
133+
}
134+
106135
[Fact]
107136
public static void NamedType_Type_Is_Null()
108137
{

0 commit comments

Comments
 (0)