Skip to content

Commit e139907

Browse files
committed
More extensions
1 parent bb8b158 commit e139907

2 files changed

Lines changed: 125 additions & 9 deletions

File tree

lib/src/main/java/graphql/nadel/engine/util/NadelPseudoSealedType.kt

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import graphql.language.ScalarTypeExtensionDefinition
2121
import graphql.language.SchemaDefinition
2222
import graphql.language.SchemaExtensionDefinition
2323
import graphql.language.Type
24+
import graphql.language.TypeDefinition
2425
import graphql.language.TypeName
2526
import graphql.language.UnionTypeDefinition
2627
import graphql.language.UnionTypeExtensionDefinition
@@ -48,7 +49,7 @@ inline fun <T> GraphQLFieldsContainer.whenType(
4849
return when (this) {
4950
is GraphQLInterfaceType -> interfaceType(this)
5051
is GraphQLObjectType -> objectType(this)
51-
else -> throw IllegalStateException("Should never happen")
52+
else -> throw IllegalStateException(javaClass.name)
5253
}
5354
}
5455

@@ -61,7 +62,7 @@ inline fun <T> GraphQLNamedInputType.whenType(
6162
is GraphQLEnumType -> enumType(this)
6263
is GraphQLInputObjectType -> inputObjectType(this)
6364
is GraphQLScalarType -> scalarType(this)
64-
else -> throw IllegalStateException("Should never happen")
65+
else -> throw IllegalStateException(javaClass.name)
6566
}
6667
}
6768

@@ -78,7 +79,7 @@ inline fun <T> GraphQLNamedOutputType.whenType(
7879
is GraphQLObjectType -> objectType(this)
7980
is GraphQLScalarType -> scalarType(this)
8081
is GraphQLUnionType -> unionType(this)
81-
else -> throw IllegalStateException("Should never happen")
82+
else -> throw IllegalStateException(javaClass.name)
8283
}
8384
}
8485

@@ -97,7 +98,7 @@ inline fun <T> GraphQLNamedType.whenType(
9798
is GraphQLObjectType -> objectType(this)
9899
is GraphQLScalarType -> scalarType(this)
99100
is GraphQLUnionType -> unionType(this)
100-
else -> throw IllegalStateException("Should never happen")
101+
else -> throw IllegalStateException(javaClass.name)
101102
}
102103
}
103104

@@ -114,7 +115,7 @@ inline fun <T> GraphQLOutputType.whenUnmodifiedType(
114115
is GraphQLObjectType -> objectType(unmodifiedType)
115116
is GraphQLScalarType -> scalarType(unmodifiedType)
116117
is GraphQLUnionType -> unionType(unmodifiedType)
117-
else -> throw IllegalStateException("Should never happen")
118+
else -> throw IllegalStateException(javaClass.name)
118119
}
119120
}
120121

@@ -127,7 +128,7 @@ inline fun <T> GraphQLInputType.whenUnmodifiedType(
127128
is GraphQLEnumType -> enumType(unmodifiedType)
128129
is GraphQLInputObjectType -> inputObjectType(unmodifiedType)
129130
is GraphQLScalarType -> scalarType(unmodifiedType)
130-
else -> throw IllegalStateException("Should never happen")
131+
else -> throw IllegalStateException(javaClass.name)
131132
}
132133
}
133134

@@ -140,7 +141,7 @@ inline fun <T> GraphQLType.whenType(
140141
is GraphQLList -> listType(this)
141142
is GraphQLNonNull -> nonNull(this)
142143
is GraphQLUnmodifiedType -> unmodifiedType(this)
143-
else -> throw IllegalStateException("Should never happen")
144+
else -> throw IllegalStateException(javaClass.name)
144145
}
145146
}
146147

@@ -153,7 +154,7 @@ inline fun <T> Type<*>.whenType(
153154
is ListType -> listType(this)
154155
is NonNullType -> nonNull(this)
155156
is TypeName -> unmodifiedType(this)
156-
else -> throw IllegalStateException("Should never happen")
157+
else -> throw IllegalStateException(javaClass.name)
157158
}
158159
}
159160

@@ -190,6 +191,70 @@ inline fun <T> AnySDLDefinition.whenType(
190191
is SchemaDefinition -> schemaDefinition(this)
191192
is UnionTypeExtensionDefinition -> unionTypeExtensionDefinition(this)
192193
is UnionTypeDefinition -> unionTypeDefinition(this)
193-
else -> throw IllegalStateException("Should never happen")
194+
else -> throw IllegalStateException(javaClass.name)
195+
}
196+
}
197+
198+
inline fun <T> AnySDLNamedDefinition.whenType(
199+
directiveDefinition: (DirectiveDefinition) -> T,
200+
enumTypeDefinition: (EnumTypeDefinition) -> T,
201+
enumTypeExtensionDefinition: (EnumTypeExtensionDefinition) -> T,
202+
inputObjectTypeDefinition: (InputObjectTypeDefinition) -> T,
203+
inputObjectTypeExtensionDefinition: (InputObjectTypeExtensionDefinition) -> T,
204+
interfaceTypeDefinition: (InterfaceTypeDefinition) -> T,
205+
interfaceTypeExtensionDefinition: (InterfaceTypeExtensionDefinition) -> T,
206+
objectTypeDefinition: (ObjectTypeDefinition) -> T,
207+
objectTypeExtensionDefinition: (ObjectTypeExtensionDefinition) -> T,
208+
scalarTypeDefinition: (ScalarTypeDefinition) -> T,
209+
scalarTypeExtensionDefinition: (ScalarTypeExtensionDefinition) -> T,
210+
unionTypeDefinition: (UnionTypeDefinition) -> T,
211+
unionTypeExtensionDefinition: (UnionTypeExtensionDefinition) -> T,
212+
): T {
213+
return when (this) {
214+
is DirectiveDefinition -> directiveDefinition(this)
215+
is EnumTypeExtensionDefinition -> enumTypeExtensionDefinition(this)
216+
is EnumTypeDefinition -> enumTypeDefinition(this)
217+
is InputObjectTypeExtensionDefinition -> inputObjectTypeExtensionDefinition(this)
218+
is InputObjectTypeDefinition -> inputObjectTypeDefinition(this)
219+
is InterfaceTypeExtensionDefinition -> interfaceTypeExtensionDefinition(this)
220+
is InterfaceTypeDefinition -> interfaceTypeDefinition(this)
221+
is ObjectTypeExtensionDefinition -> objectTypeExtensionDefinition(this)
222+
is ObjectTypeDefinition -> objectTypeDefinition(this)
223+
is ScalarTypeExtensionDefinition -> scalarTypeExtensionDefinition(this)
224+
is ScalarTypeDefinition -> scalarTypeDefinition(this)
225+
is UnionTypeExtensionDefinition -> unionTypeExtensionDefinition(this)
226+
is UnionTypeDefinition -> unionTypeDefinition(this)
227+
else -> throw IllegalStateException(javaClass.name)
228+
}
229+
}
230+
231+
inline fun <T> TypeDefinition<*>.whenType(
232+
enumTypeDefinition: (EnumTypeDefinition) -> T,
233+
enumTypeExtensionDefinition: (EnumTypeExtensionDefinition) -> T,
234+
inputObjectTypeDefinition: (InputObjectTypeDefinition) -> T,
235+
inputObjectTypeExtensionDefinition: (InputObjectTypeExtensionDefinition) -> T,
236+
interfaceTypeDefinition: (InterfaceTypeDefinition) -> T,
237+
interfaceTypeExtensionDefinition: (InterfaceTypeExtensionDefinition) -> T,
238+
objectTypeDefinition: (ObjectTypeDefinition) -> T,
239+
objectTypeExtensionDefinition: (ObjectTypeExtensionDefinition) -> T,
240+
scalarTypeDefinition: (ScalarTypeDefinition) -> T,
241+
scalarTypeExtensionDefinition: (ScalarTypeExtensionDefinition) -> T,
242+
unionTypeDefinition: (UnionTypeDefinition) -> T,
243+
unionTypeExtensionDefinition: (UnionTypeExtensionDefinition) -> T,
244+
): T {
245+
return when (this) {
246+
is EnumTypeExtensionDefinition -> enumTypeExtensionDefinition(this)
247+
is EnumTypeDefinition -> enumTypeDefinition(this)
248+
is InputObjectTypeExtensionDefinition -> inputObjectTypeExtensionDefinition(this)
249+
is InputObjectTypeDefinition -> inputObjectTypeDefinition(this)
250+
is InterfaceTypeExtensionDefinition -> interfaceTypeExtensionDefinition(this)
251+
is InterfaceTypeDefinition -> interfaceTypeDefinition(this)
252+
is ObjectTypeExtensionDefinition -> objectTypeExtensionDefinition(this)
253+
is ObjectTypeDefinition -> objectTypeDefinition(this)
254+
is ScalarTypeExtensionDefinition -> scalarTypeExtensionDefinition(this)
255+
is ScalarTypeDefinition -> scalarTypeDefinition(this)
256+
is UnionTypeExtensionDefinition -> unionTypeExtensionDefinition(this)
257+
is UnionTypeDefinition -> unionTypeDefinition(this)
258+
else -> throw IllegalStateException(javaClass.name)
194259
}
195260
}

lib/src/test/kotlin/graphql/nadel/archunit/NadelPseudoSealedTypeKtTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import graphql.language.NonNullType
1515
import graphql.language.ObjectTypeDefinition
1616
import graphql.language.ObjectTypeExtensionDefinition
1717
import graphql.language.SDLDefinition
18+
import graphql.language.SDLNamedDefinition
1819
import graphql.language.ScalarTypeDefinition
1920
import graphql.language.ScalarTypeExtensionDefinition
2021
import graphql.language.SchemaDefinition
2122
import graphql.language.SchemaExtensionDefinition
2223
import graphql.language.Type
24+
import graphql.language.TypeDefinition
2325
import graphql.language.TypeName
2426
import graphql.language.UnionTypeDefinition
2527
import graphql.language.UnionTypeExtensionDefinition
@@ -194,4 +196,53 @@ class NadelPseudoSealedTypeKtTest {
194196
)
195197
.check(schemaClasses)
196198
}
199+
200+
@Test
201+
fun `whenType(AnySDLNamedDefinition)`() {
202+
classes()
203+
.that()
204+
.areAssignableTo(SDLNamedDefinition::class.java)
205+
.and()
206+
.areNotInterfaces()
207+
.equalsExactly(
208+
DirectiveDefinition::class,
209+
EnumTypeDefinition::class,
210+
EnumTypeExtensionDefinition::class,
211+
InputObjectTypeDefinition::class,
212+
InputObjectTypeExtensionDefinition::class,
213+
InterfaceTypeDefinition::class,
214+
InterfaceTypeExtensionDefinition::class,
215+
ObjectTypeDefinition::class,
216+
ObjectTypeExtensionDefinition::class,
217+
ScalarTypeDefinition::class,
218+
ScalarTypeExtensionDefinition::class,
219+
UnionTypeDefinition::class,
220+
UnionTypeExtensionDefinition::class,
221+
)
222+
.check(schemaClasses)
223+
}
224+
225+
@Test
226+
fun `whenType(TypeDefinition)`() {
227+
classes()
228+
.that()
229+
.areAssignableTo(TypeDefinition::class.java)
230+
.and()
231+
.areNotInterfaces()
232+
.equalsExactly(
233+
EnumTypeDefinition::class,
234+
EnumTypeExtensionDefinition::class,
235+
InputObjectTypeDefinition::class,
236+
InputObjectTypeExtensionDefinition::class,
237+
InterfaceTypeDefinition::class,
238+
InterfaceTypeExtensionDefinition::class,
239+
ObjectTypeDefinition::class,
240+
ObjectTypeExtensionDefinition::class,
241+
ScalarTypeDefinition::class,
242+
ScalarTypeExtensionDefinition::class,
243+
UnionTypeDefinition::class,
244+
UnionTypeExtensionDefinition::class,
245+
)
246+
.check(schemaClasses)
247+
}
197248
}

0 commit comments

Comments
 (0)