Skip to content

Commit 2094d3b

Browse files
author
Shane Myrick
committed
Reuse schema generator for tests
In an effort to save memory and build time, I made a common testGenerator that just uses the basic SchemaGeneratorConfig we already we using. Previously, every call to toSchema would create a new SchemaGenerator class which creates a new type cache and class scanner. Maybe this will help with the Github actions?
1 parent f92faba commit 2094d3b

20 files changed

+140
-153
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
6767
builder.codeRegistry(codeRegistry.build())
6868
val schema = config.hooks.willBuildSchema(builder).build()
6969

70-
classScanner.close()
70+
cleanUpResources()
7171

7272
return schema
7373
}
@@ -100,4 +100,10 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
100100

101101
return graphqlTypes.toSet()
102102
}
103+
104+
private fun cleanUpResources() {
105+
additionalTypes.clear()
106+
cache.clear()
107+
directives.clear()
108+
}
103109
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ internal class ClassScanner(supportedPackages: List<String>) {
4141

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

44-
fun close() = scanResult.close()
45-
4644
@Suppress("Detekt.SwallowedException")
4745
private fun getImplementingClasses(classInfo: ClassInfo) =
4846
try {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package com.expediagroup.graphql.directives
1818

1919
import com.expediagroup.graphql.TopLevelObject
2020
import com.expediagroup.graphql.annotations.GraphQLDirective
21+
import com.expediagroup.graphql.generator.SchemaGenerator
2122
import com.expediagroup.graphql.getTestSchemaConfigWithHooks
2223
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
23-
import com.expediagroup.graphql.testSchemaConfig
24-
import com.expediagroup.graphql.toSchema
24+
import com.expediagroup.graphql.testGenerator
2525
import graphql.Scalars
2626
import graphql.schema.GraphQLNonNull
2727
import graphql.schema.GraphQLObjectType
@@ -34,7 +34,7 @@ class DirectiveTests {
3434

3535
@Test
3636
fun `SchemaGenerator marks deprecated fields in the return objects`() {
37-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
37+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())))
3838
val topLevelQuery = schema.getObjectType("Query")
3939
val query = topLevelQuery.getFieldDefinition("deprecatedFieldQuery")
4040
val result = (query.type as? GraphQLNonNull)?.wrappedType as? GraphQLObjectType
@@ -46,7 +46,7 @@ class DirectiveTests {
4646

4747
@Test
4848
fun `SchemaGenerator marks deprecated queries and documents replacement`() {
49-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
49+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())))
5050
val topLevelQuery = schema.getObjectType("Query")
5151
val query = topLevelQuery.getFieldDefinition("deprecatedQueryWithReplacement")
5252

@@ -56,7 +56,7 @@ class DirectiveTests {
5656

5757
@Test
5858
fun `SchemaGenerator marks deprecated queries`() {
59-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
59+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())))
6060
val topLevelQuery = schema.getObjectType("Query")
6161
val query = topLevelQuery.getFieldDefinition("deprecatedQuery")
6262
assertTrue(query.isDeprecated)
@@ -70,7 +70,8 @@ class DirectiveTests {
7070
override val wiringFactory: KotlinDirectiveWiringFactory
7171
get() = KotlinDirectiveWiringFactory(manualWiring = mapOf("dummyDirective" to wiring, "RightNameDirective" to wiring))
7272
})
73-
val schema = toSchema(queries = listOf(TopLevelObject(QueryObject())), config = config)
73+
val generator = SchemaGenerator(config)
74+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(QueryObject())))
7475

7576
val query = schema.queryType.getFieldDefinition("query")
7677
assertNotNull(query)
@@ -84,7 +85,8 @@ class DirectiveTests {
8485
override val wiringFactory: KotlinDirectiveWiringFactory
8586
get() = KotlinDirectiveWiringFactory(manualWiring = mapOf("dummyDirective" to wiring, "RightNameDirective" to wiring))
8687
})
87-
val schema = toSchema(queries = listOf(TopLevelObject(QueryObject())), config = config)
88+
val generator = SchemaGenerator(config)
89+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(QueryObject())))
8890

8991
val directive = assertNotNull(
9092
(schema.getType("Location") as? GraphQLObjectType)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.expediagroup.graphql.execution
1919
import com.expediagroup.graphql.SchemaGeneratorConfig
2020
import com.expediagroup.graphql.TopLevelObject
2121
import com.expediagroup.graphql.extensions.deepName
22-
import com.expediagroup.graphql.toSchema
22+
import com.expediagroup.graphql.generator.SchemaGenerator
2323
import graphql.GraphQL
2424
import graphql.schema.DataFetcher
2525
import graphql.schema.DataFetcherFactories
@@ -34,7 +34,8 @@ class CustomDataFetcherTests {
3434
@Test
3535
fun `Custom DataFetcher can be used on functions`() {
3636
val config = SchemaGeneratorConfig(supportedPackages = listOf("com.expediagroup"), dataFetcherFactoryProvider = CustomDataFetcherFactoryProvider())
37-
val schema = toSchema(queries = listOf(TopLevelObject(AnimalQuery())), config = config)
37+
val generator = SchemaGenerator(config)
38+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(AnimalQuery())))
3839

3940
val animalType = schema.getObjectType("Animal")
4041
assertEquals("AnimalDetails!", animalType.getFieldDefinition("details").type.deepName)

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import com.expediagroup.graphql.annotations.GraphQLDirective
2222
import com.expediagroup.graphql.annotations.GraphQLID
2323
import com.expediagroup.graphql.annotations.GraphQLIgnore
2424
import com.expediagroup.graphql.annotations.GraphQLName
25+
import com.expediagroup.graphql.generator.SchemaGenerator
2526
import com.expediagroup.graphql.getTestSchemaConfigWithMockedDirectives
26-
import com.expediagroup.graphql.testSchemaConfig
27-
import com.expediagroup.graphql.toSchema
27+
import com.expediagroup.graphql.testGenerator
2828
import graphql.introspection.Introspection
2929
import graphql.schema.GraphQLSchema
3030
import org.junit.jupiter.api.Test
@@ -42,7 +42,7 @@ class GraphQLSchemaExtensionsTest {
4242

4343
@Test
4444
fun `verify print result of a simple schema`() {
45-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)
45+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(SimpleQuery())))
4646

4747
val sdl = schema.print(includeDirectives = false).trim()
4848
val expected = """
@@ -60,7 +60,7 @@ class GraphQLSchemaExtensionsTest {
6060

6161
@Test
6262
fun `verify print result of a simple schema with extended scalars`() {
63-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)
63+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(SimpleQuery())))
6464

6565
val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = true).trim()
6666
val expected = """
@@ -78,7 +78,7 @@ class GraphQLSchemaExtensionsTest {
7878

7979
@Test
8080
fun `verify print result of a simple schema with no scalars`() {
81-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)
81+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(SimpleQuery())))
8282

8383
val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = false).trim()
8484
val expected = """
@@ -107,7 +107,7 @@ class GraphQLSchemaExtensionsTest {
107107

108108
@Test
109109
fun `verify print result of a schema with renamed fields`() {
110-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(RenamedQuery())), config = testSchemaConfig)
110+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(RenamedQuery())))
111111

112112
val sdl = schema.print(includeDefaultSchemaDefinition = false, includeDirectives = false).trim()
113113
val expected = """
@@ -134,7 +134,7 @@ class GraphQLSchemaExtensionsTest {
134134

135135
@Test
136136
fun `verify print result of a schema with GraphQL ID`() {
137-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(QueryWithId())), config = testSchemaConfig)
137+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithId())))
138138

139139
val sdl = schema.print(includeDefaultSchemaDefinition = false, includeDirectives = false).trim()
140140
val expected = """
@@ -168,7 +168,7 @@ class GraphQLSchemaExtensionsTest {
168168

169169
@Test
170170
fun `verify print result of a schema with ignored elements`() {
171-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(QueryWithExcludedFields())), config = testSchemaConfig)
171+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithExcludedFields())))
172172

173173
val sdl = schema.print(includeDefaultSchemaDefinition = false, includeDirectives = false).trim()
174174
val expected = """
@@ -196,7 +196,7 @@ class GraphQLSchemaExtensionsTest {
196196

197197
@Test
198198
fun `verify print result of a documented schema`() {
199-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(DocumentedQuery())), config = testSchemaConfig)
199+
val schema: GraphQLSchema = testGenerator.generateSchema(queries = listOf(TopLevelObject(DocumentedQuery())))
200200

201201
val sdl = schema.print(includeDefaultSchemaDefinition = false, includeDirectives = false).trim()
202202
val expected = """
@@ -253,7 +253,8 @@ class GraphQLSchemaExtensionsTest {
253253

254254
@Test
255255
fun `verify print result of a schema with directives`() {
256-
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(QueryWithDirectives())), config = getTestSchemaConfigWithMockedDirectives())
256+
val generator = SchemaGenerator(getTestSchemaConfigWithMockedDirectives())
257+
val schema: GraphQLSchema = generator.generateSchema(queries = listOf(TopLevelObject(QueryWithDirectives())))
257258

258259
val sdl = schema.print(includeDefaultSchemaDefinition = false).trim()
259260
val expected = """

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import com.expediagroup.graphql.TopLevelObject
2020
import com.expediagroup.graphql.annotations.GraphQLIgnore
2121
import com.expediagroup.graphql.annotations.GraphQLName
2222
import com.expediagroup.graphql.exceptions.InvalidInputFieldTypeException
23-
import com.expediagroup.graphql.testSchemaConfig
24-
import com.expediagroup.graphql.toSchema
23+
import com.expediagroup.graphql.testGenerator
2524
import graphql.TypeResolutionEnvironment
2625
import graphql.schema.GraphQLInterfaceType
2726
import graphql.schema.GraphQLObjectType
@@ -35,11 +34,11 @@ import kotlin.test.assertNotNull
3534
import kotlin.test.assertNull
3635
import kotlin.test.assertTrue
3736

38-
internal class PolymorphicTests {
37+
class PolymorphicTests {
3938

4039
@Test
4140
fun `Schema generator creates union types from marked up interface`() {
42-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithUnion())), config = testSchemaConfig)
41+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithUnion())))
4342

4443
val graphqlType = schema.getType("BodyPart") as? GraphQLUnionType
4544
assertNotNull(graphqlType)
@@ -56,7 +55,7 @@ internal class PolymorphicTests {
5655

5756
@Test
5857
fun `SchemaGenerator can expose an interface and its implementations`() {
59-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInterface())), config = testSchemaConfig)
58+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithInterface())))
6059

6160
val interfaceType = schema.getType("AnInterface") as? GraphQLInterfaceType
6261
assertNotNull(interfaceType)
@@ -70,20 +69,20 @@ internal class PolymorphicTests {
7069
@Test
7170
fun `Interfaces cannot be used as input field types`() {
7271
assertThrows(InvalidInputFieldTypeException::class.java) {
73-
toSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedInterfaceArgument())), config = testSchemaConfig)
72+
testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedInterfaceArgument())))
7473
}
7574
}
7675

7776
@Test
7877
fun `Union cannot be used as input field types`() {
7978
assertThrows(InvalidInputFieldTypeException::class.java) {
80-
toSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedUnionArgument())), config = testSchemaConfig)
79+
testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedUnionArgument())))
8180
}
8281
}
8382

8483
@Test
8584
fun `Object types implementing union and interfaces are only created once`() {
86-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInterfaceAndUnion())), config = testSchemaConfig)
85+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithInterfaceAndUnion())))
8786

8887
val carType = schema.getType("Car") as? GraphQLObjectType
8988
assertNotNull(carType)
@@ -97,15 +96,15 @@ internal class PolymorphicTests {
9796

9897
@Test
9998
fun `Interfaces can declare properties of their own type`() {
100-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithRecursiveType())), config = testSchemaConfig)
99+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithRecursiveType())))
101100

102101
val personType = schema.getType("Person")
103102
assertNotNull(personType)
104103
}
105104

106105
@Test
107106
fun `Abstract classes should be converted to interfaces`() {
108-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithAbstract())), config = testSchemaConfig)
107+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithAbstract())))
109108

110109
val abstractInterface = schema.getType("MyAbstract") as? GraphQLInterfaceType
111110
assertNotNull(abstractInterface)
@@ -118,7 +117,7 @@ internal class PolymorphicTests {
118117

119118
@Test
120119
fun `Interface types can be correctly resolved`() {
121-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithRenamedAbstracts())), config = testSchemaConfig)
120+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithRenamedAbstracts())))
122121

123122
val cakeInterface = schema.getType("Cake") as? GraphQLInterfaceType
124123
assertNotNull(cakeInterface)
@@ -134,7 +133,7 @@ internal class PolymorphicTests {
134133

135134
@Test
136135
fun `Union types can be correctly resolved`() {
137-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithRenamedAbstracts())), config = testSchemaConfig)
136+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithRenamedAbstracts())))
138137

139138
val dessertUnion = schema.getType("Dessert") as? GraphQLUnionType
140139
assertNotNull(dessertUnion)
@@ -150,7 +149,7 @@ internal class PolymorphicTests {
150149

151150
@Test
152151
fun `Interface implementations are not computed when marked with GraphQLIgnore annotation`() {
153-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithIgnoredInfo())), config = testSchemaConfig)
152+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithIgnoredInfo())))
154153
val service = schema.getType("Service") as? GraphQLInterfaceType
155154
assertNotNull(service)
156155

@@ -163,7 +162,7 @@ internal class PolymorphicTests {
163162

164163
@Test
165164
fun `Ignored interface properties should not appear in the subtype`() {
166-
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithIgnoredInfo())), config = testSchemaConfig)
165+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(QueryWithIgnoredInfo())))
167166
val service = schema.getType("Service") as? GraphQLInterfaceType
168167
assertNotNull(service)
169168
val interfaceIgnoredField = service.getFieldDefinition("shouldNotBeInTheSchema")

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ package com.expediagroup.graphql.generator
1919
import com.expediagroup.graphql.TopLevelObject
2020
import com.expediagroup.graphql.getTestSchemaConfigWithHooks
2121
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
22-
import com.expediagroup.graphql.testSchemaConfig
23-
import com.expediagroup.graphql.toSchema
22+
import com.expediagroup.graphql.testGenerator
2423
import graphql.schema.GraphQLNamedType
2524
import graphql.schema.GraphQLNonNull
2625
import io.reactivex.rxjava3.core.Maybe
@@ -46,7 +45,7 @@ class SchemaGeneratorAsyncTests {
4645

4746
@Test
4847
fun `SchemaGenerator strips type argument from CompletableFuture to support async servlet`() {
49-
val schema = toSchema(queries = listOf(TopLevelObject(AsyncQuery())), config = testSchemaConfig)
48+
val schema = testGenerator.generateSchema(queries = listOf(TopLevelObject(AsyncQuery())))
5049
val returnType =
5150
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType
5251
assertNotNull(returnType)
@@ -56,7 +55,8 @@ class SchemaGeneratorAsyncTests {
5655

5756
@Test
5857
fun `SchemaGenerator strips type argument from RxJava2 Observable`() {
59-
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
58+
val generator = SchemaGenerator(configWithRxJavaMonads)
59+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(RxJava2Query())))
6060
val returnType =
6161
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType
6262
assertNotNull(returnType)
@@ -66,7 +66,8 @@ class SchemaGeneratorAsyncTests {
6666

6767
@Test
6868
fun `SchemaGenerator strips type argument from RxJava2 Single`() {
69-
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
69+
val generator = SchemaGenerator(configWithRxJavaMonads)
70+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(RxJava2Query())))
7071
val returnType =
7172
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDoSingle").type as? GraphQLNonNull)?.wrappedType
7273
assertNotNull(returnType)
@@ -76,7 +77,8 @@ class SchemaGeneratorAsyncTests {
7677

7778
@Test
7879
fun `SchemaGenerator strips type argument from RxJava2 Maybe`() {
79-
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
80+
val generator = SchemaGenerator(configWithRxJavaMonads)
81+
val schema = generator.generateSchema(queries = listOf(TopLevelObject(RxJava2Query())))
8082
val returnType =
8183
(schema.getObjectType("Query").getFieldDefinition("maybe").type as? GraphQLNonNull)?.wrappedType
8284
assertNotNull(returnType)

0 commit comments

Comments
 (0)