@@ -16,14 +16,14 @@ import com.expedia.graphql.schema.extensions.isGraphQLContext
1616import com.expedia.graphql.schema.extensions.isGraphQLID
1717import com.expedia.graphql.schema.extensions.throwIfUnathorizedInterface
1818import com.expedia.graphql.schema.extensions.wrapInNonNull
19+ import com.expedia.graphql.schema.generator.state.SchemaGeneratorState
1920import com.expedia.graphql.schema.generator.types.defaultGraphQLScalars
2021import com.expedia.graphql.schema.generator.types.enumType
2122import com.expedia.graphql.schema.generator.types.getInputClassName
2223import com.expedia.graphql.schema.models.KGraphQLType
2324import graphql.TypeResolutionEnvironment
2425import graphql.schema.DataFetcher
2526import graphql.schema.GraphQLArgument
26- import graphql.schema.GraphQLDirective
2727import graphql.schema.GraphQLFieldDefinition
2828import graphql.schema.GraphQLInputObjectField
2929import graphql.schema.GraphQLInputObjectType
@@ -54,10 +54,8 @@ internal class SchemaGenerator(
5454 private val config : SchemaGeneratorConfig
5555) {
5656
57- private val cache = TypesCache (config.supportedPackages)
57+ private val state = SchemaGeneratorState (config.supportedPackages)
5858 private val subTypeMapper = SubTypeMapper (config.supportedPackages)
59- private val additionTypes = mutableSetOf<GraphQLType >()
60- private val directives = mutableSetOf<GraphQLDirective >()
6159
6260 internal fun generate (): GraphQLSchema {
6361 val builder = generateWithReflection()
@@ -74,12 +72,10 @@ internal class SchemaGenerator(
7472 }
7573
7674 private fun addAdditionalTypes (builder : GraphQLSchema .Builder ) {
77- additionTypes
78- .filter { cache.doesNotContainGraphQLType(it) }
79- .forEach { builder.additionalType(it) }
75+ state.getValidAdditionalTypes().forEach { builder.additionalType(it) }
8076 }
8177
82- private fun addDirectives (builder : GraphQLSchema .Builder ) = builder.additionalDirectives(directives)
78+ private fun addDirectives (builder : GraphQLSchema .Builder ) = builder.additionalDirectives(state. directives)
8379
8480 private fun addQueries (builder : GraphQLSchema .Builder ) {
8581 val queryBuilder = GraphQLObjectType .Builder ()
@@ -126,7 +122,7 @@ internal class SchemaGenerator(
126122
127123 fn.directives().forEach {
128124 builder.withDirective(it)
129- directives.add(it)
125+ state. directives.add(it)
130126 }
131127
132128 val args = mutableMapOf<String , Parameter >()
@@ -141,6 +137,7 @@ internal class SchemaGenerator(
141137 throw IllegalArgumentException (" argument name is null or blank, $it " )
142138 } else {
143139 // Kotlin 1.3 will support contracts, until then we need to force non-null
140+ @Suppress(" Detekt.UnsafeCallOnNullableType" )
144141 args[name!! ] = Parameter (it.type.javaType as Class <* >, it.annotations)
145142 }
146143 }
@@ -191,7 +188,7 @@ internal class SchemaGenerator(
191188
192189 private fun objectFromReflection (type : KType , inputType : Boolean ): GraphQLType {
193190 val cacheKey = TypesCacheKey (type, inputType)
194- val cachedType = cache.get(cacheKey)
191+ val cachedType = state. cache.get(cacheKey)
195192
196193 if (cachedType != null ) {
197194 return cachedType
@@ -201,7 +198,7 @@ internal class SchemaGenerator(
201198 val graphQLType = getGraphQLType(kClass, inputType, type)
202199 val kGraphQLType = KGraphQLType (kClass, graphQLType)
203200
204- cache.put(cacheKey, kGraphQLType)
201+ state. cache.put(cacheKey, kGraphQLType)
205202
206203 return graphQLType
207204 }
@@ -218,15 +215,15 @@ internal class SchemaGenerator(
218215 GraphQLList .list(graphQLTypeOf(type.getTypeOfFirstArgument(), inputType))
219216
220217 private fun objectType (kClass : KClass <* >, interfaceType : GraphQLInterfaceType ? = null): GraphQLType {
221- return cache.buildIfNotUnderConstruction(kClass) { _ ->
218+ return state. cache.buildIfNotUnderConstruction(kClass) { _ ->
222219 val builder = GraphQLObjectType .newObject()
223220
224221 builder.name(kClass.simpleName)
225222 builder.description(kClass.graphQLDescription())
226223
227- kClass.directives().map {
224+ kClass.directives().forEach {
228225 builder.withDirective(it)
229- directives.add(it)
226+ state. directives.add(it)
230227 }
231228
232229 if (interfaceType != null ) {
@@ -274,7 +271,7 @@ internal class SchemaGenerator(
274271 }
275272
276273 private fun interfaceType (kClass : KClass <* >): GraphQLType {
277- return cache.buildIfNotUnderConstruction(kClass) { _ ->
274+ return state. cache.buildIfNotUnderConstruction(kClass) { _ ->
278275 val builder = GraphQLInterfaceType .newInterface()
279276
280277 builder.name(kClass.simpleName)
@@ -296,16 +293,16 @@ internal class SchemaGenerator(
296293 val objectType = objectType(it.kotlin, interfaceType)
297294 val key = TypesCacheKey (it.kotlin.createType(), false )
298295
299- additionTypes .add(objectType)
300- cache.put(key, KGraphQLType (it.kotlin, objectType))
296+ state.additionalTypes .add(objectType)
297+ state. cache.put(key, KGraphQLType (it.kotlin, objectType))
301298 }
302299
303300 interfaceType
304301 }
305302 }
306303
307304 private fun unionType (kClass : KClass <* >): GraphQLType {
308- return cache.buildIfNotUnderConstruction(kClass) { _ ->
305+ return state. cache.buildIfNotUnderConstruction(kClass) { _ ->
309306 val builder = GraphQLUnionType .newUnionType()
310307
311308 builder.name(kClass.simpleName)
@@ -325,7 +322,7 @@ internal class SchemaGenerator(
325322 builder.possibleType(objectType as GraphQLObjectType )
326323 }
327324
328- cache.put(key, KGraphQLType (it.kotlin, objectType))
325+ state. cache.put(key, KGraphQLType (it.kotlin, objectType))
329326 }
330327
331328 builder.build()
0 commit comments