@@ -241,11 +241,7 @@ internal class SchemaGenerator(
241241 GraphQLList .list(graphQLTypeOf(type.arguments.first().type!! , inputType))
242242
243243 private fun objectType (kClass : KClass <* >, interfaceType : GraphQLInterfaceType ? = null): GraphQLType {
244- if (cache.isTypeUnderConstruction(kClass)) {
245- return GraphQLTypeReference .typeRef(kClass.simpleName)
246- } else {
247- cache.putTypeUnderConstruction(kClass)
248-
244+ return cache.buildIfNotUnderConstruction(kClass) {
249245 val builder = GraphQLObjectType .newObject()
250246
251247 builder.name(kClass.simpleName)
@@ -272,7 +268,7 @@ internal class SchemaGenerator(
272268 kClass.getValidFunctions(config.hooks)
273269 .forEach { builder.field(function(it)) }
274270
275- return builder.build()
271+ builder.build()
276272 }
277273 }
278274
@@ -301,51 +297,59 @@ internal class SchemaGenerator(
301297 }
302298
303299 private fun interfaceType (kClass : KClass <* >): GraphQLType {
304- val builder = GraphQLInterfaceType .newInterface()
300+ return cache.buildIfNotUnderConstruction(kClass) {
301+ val builder = GraphQLInterfaceType .newInterface()
305302
306- builder.name(kClass.simpleName)
307- builder.description(kClass.graphQLDescription())
303+ builder.name(kClass.simpleName)
304+ builder.description(kClass.graphQLDescription())
308305
309- kClass.getValidProperties(config.hooks)
310- .forEach { builder.field(property(it)) }
311-
312- kClass.getValidFunctions(config.hooks)
313- .forEach { builder.field(function(it, abstract = true )) }
314-
315- builder.typeResolver { env: TypeResolutionEnvironment -> env.schema.getObjectType(env.getObject<Any >().javaClass.simpleName) }
316- val interfaceType = builder.build()
317-
318- val implementations = getSubTypesOf(kClass)
319- implementations
320- .filterNot { it.kotlin.isAbstract }
321- .forEach {
322- val objectType = objectType(it.kotlin, interfaceType)
323- val key = TypesCacheKey (it.kotlin.createType(), false )
324- additionTypes.add(objectType)
325- cache.put(key, KGraphQLType (it.kotlin, objectType))
326- }
306+ kClass.getValidProperties(config.hooks)
307+ .forEach { builder.field(property(it)) }
308+
309+ kClass.getValidFunctions(config.hooks)
310+ .forEach { builder.field(function(it, abstract = true )) }
311+
312+ builder.typeResolver { env: TypeResolutionEnvironment -> env.schema.getObjectType(env.getObject<Any >().javaClass.simpleName) }
313+ val interfaceType = builder.build()
327314
328- return interfaceType
315+ val implementations = getSubTypesOf(kClass)
316+ implementations
317+ .filterNot { it.kotlin.isAbstract }
318+ .forEach {
319+ val objectType = objectType(it.kotlin, interfaceType)
320+ val key = TypesCacheKey (it.kotlin.createType(), false )
321+ additionTypes.add(objectType)
322+ cache.put(key, KGraphQLType (it.kotlin, objectType))
323+ }
324+
325+ interfaceType
326+ }
329327 }
330328
331329 private fun unionType (kClass : KClass <* >): GraphQLType {
332- val builder = GraphQLUnionType .newUnionType()
330+ return cache.buildIfNotUnderConstruction(kClass) {
331+ val builder = GraphQLUnionType .newUnionType()
333332
334- builder.name(kClass.simpleName)
335- builder.description(kClass.graphQLDescription())
336- builder.typeResolver { env: TypeResolutionEnvironment -> env.schema.getObjectType(env.getObject<Any >().javaClass.simpleName) }
337-
338- val implementations = getSubTypesOf(kClass)
339- implementations
340- .filterNot { it.kotlin.isAbstract }
341- .forEach {
342- val objectType = objectType(it.kotlin)
343- val key = TypesCacheKey (it.kotlin.createType(), false )
344- builder.possibleType(objectType(it.kotlin) as GraphQLObjectType )
345- cache.put(key, KGraphQLType (it.kotlin, objectType))
346- }
333+ builder.name(kClass.simpleName)
334+ builder.description(kClass.graphQLDescription())
335+ builder.typeResolver { env: TypeResolutionEnvironment -> env.schema.getObjectType(env.getObject<Any >().javaClass.simpleName) }
347336
348- return builder.build()
337+ val implementations = getSubTypesOf(kClass)
338+ implementations
339+ .filterNot { it.kotlin.isAbstract }
340+ .forEach {
341+ val objectType = objectType(it.kotlin)
342+ val key = TypesCacheKey (it.kotlin.createType(), false )
343+ if (objectType is GraphQLTypeReference ) {
344+ builder.possibleType(objectType)
345+ } else {
346+ builder.possibleType(objectType as GraphQLObjectType )
347+ }
348+ cache.put(key, KGraphQLType (it.kotlin, objectType))
349+ }
350+
351+ builder.build()
352+ }
349353 }
350354
351355 private fun getSubTypesOf (kclass : KClass <* >): MutableSet <out Class <out Any >> =
0 commit comments