Skip to content

Commit 38a4f94

Browse files
smyrickShane Myrick
andauthored
Make SchemaGenerator closeable (#643)
Extracting separate changes from #641 that cleans up the resources used when creating a new schema generator. Hopefully this will help with our unit test out of memory issues Co-authored-by: Shane Myrick <[email protected]>
1 parent f92faba commit 38a4f94

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/toFederatedSchema.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ fun toFederatedSchema(
3838
subscriptions: List<TopLevelObject> = emptyList()
3939
): GraphQLSchema {
4040
val generator = FederatedSchemaGenerator(config)
41-
return generator.generateSchema(queries, mutations, subscriptions)
41+
return generator.use {
42+
it.generateSchema(queries, mutations, subscriptions)
43+
}
4244
}

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/SchemaGenerator.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import graphql.schema.GraphQLCodeRegistry
2828
import graphql.schema.GraphQLDirective
2929
import graphql.schema.GraphQLSchema
3030
import graphql.schema.GraphQLType
31+
import java.io.Closeable
3132
import java.util.concurrent.ConcurrentHashMap
3233
import kotlin.reflect.KClass
3334
import kotlin.reflect.KType
@@ -39,7 +40,7 @@ import kotlin.reflect.full.createType
3940
* This class maintains the state of the schema while generation is taking place. It is passed into the internal functions
4041
* so they can use the cache and add additional types and directives into the schema as they parse the Kotlin code.
4142
*/
42-
open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
43+
open class SchemaGenerator(internal val config: SchemaGeneratorConfig) : Closeable {
4344

4445
internal val additionalTypes = mutableSetOf<KType>()
4546
internal val classScanner = ClassScanner(config.supportedPackages)
@@ -100,4 +101,20 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
100101

101102
return graphqlTypes.toSet()
102103
}
104+
105+
/**
106+
* Clear the generator type cache, reflection scan, additional types,
107+
* and the saved directives. You may want call this after you have
108+
* called [generateSchema] and performed some other actions which is why
109+
* we have a separate method to explicitly clear.
110+
*
111+
* If you use the built in [com.expediagroup.graphql.toSchema], we will handle
112+
* clean up of resources for you.
113+
*/
114+
override fun close() {
115+
classScanner.close()
116+
cache.clear()
117+
additionalTypes.clear()
118+
directives.clear()
119+
}
103120
}

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/state/ClassScanner.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ package com.expediagroup.graphql.generator.state
1919
import io.github.classgraph.ClassGraph
2020
import io.github.classgraph.ClassInfo
2121
import io.github.classgraph.ClassInfoList
22+
import java.io.Closeable
2223
import kotlin.reflect.KClass
2324
import kotlin.reflect.jvm.jvmName
2425

25-
internal class ClassScanner(supportedPackages: List<String>) {
26+
internal class ClassScanner(supportedPackages: List<String>) : Closeable {
2627

2728
@Suppress("Detekt.SpreadOperator")
2829
private val scanResult = ClassGraph()
@@ -41,7 +42,7 @@ internal class ClassScanner(supportedPackages: List<String>) {
4142

4243
fun getClassesWithAnnotation(annotation: KClass<*>) = scanResult.getClassesWithAnnotation(annotation.jvmName).map { it.loadClass().kotlin }
4344

44-
fun close() = scanResult.close()
45+
override fun close() = scanResult.close()
4546

4647
@Suppress("Detekt.SwallowedException")
4748
private fun getImplementingClasses(classInfo: ClassInfo) =

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/toSchema.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ fun toSchema(
3838
subscriptions: List<TopLevelObject> = emptyList()
3939
): GraphQLSchema {
4040
val generator = SchemaGenerator(config)
41-
return generator.generateSchema(queries, mutations, subscriptions)
41+
return generator.use {
42+
it.generateSchema(queries, mutations, subscriptions)
43+
}
4244
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/testSchemaConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
2222
import io.mockk.every
2323
import io.mockk.spyk
2424

25-
val defaultSupportedPackages = listOf("com.expediagroup")
25+
val defaultSupportedPackages = listOf("com.expediagroup.graphql")
2626
val testSchemaConfig = SchemaGeneratorConfig(defaultSupportedPackages)
2727

2828
fun getTestSchemaConfigWithHooks(hooks: SchemaGeneratorHooks) = SchemaGeneratorConfig(defaultSupportedPackages, hooks = hooks)

0 commit comments

Comments
 (0)