@@ -4,13 +4,16 @@ import com.expedia.graphql.TopLevelObjectDef
44import com.expedia.graphql.schema.KotlinDataFetcher
55import com.expedia.graphql.schema.Parameter
66import com.expedia.graphql.schema.SchemaGeneratorConfig
7- import com.expedia.graphql.schema.exceptions.InvalidInputFieldTypeException
7+ import com.expedia.graphql.schema.extensions.canBeGraphQLInterface
8+ import com.expedia.graphql.schema.extensions.canBeGraphQLUnion
89import com.expedia.graphql.schema.extensions.directives
910import com.expedia.graphql.schema.extensions.getDeprecationReason
11+ import com.expedia.graphql.schema.extensions.getTypeOfFirstArgument
1012import com.expedia.graphql.schema.extensions.getValidFunctions
1113import com.expedia.graphql.schema.extensions.getValidProperties
1214import com.expedia.graphql.schema.extensions.graphQLDescription
1315import com.expedia.graphql.schema.extensions.isGraphQLContext
16+ import com.expedia.graphql.schema.extensions.throwIfUnathorizedInterface
1417import com.expedia.graphql.schema.extensions.wrapInNonNull
1518import com.expedia.graphql.schema.generator.types.defaultGraphQLScalars
1619import com.expedia.graphql.schema.generator.types.enumType
@@ -26,7 +29,6 @@ import graphql.schema.GraphQLInputObjectType
2629import graphql.schema.GraphQLInputType
2730import graphql.schema.GraphQLInterfaceType
2831import graphql.schema.GraphQLList
29- import graphql.schema.GraphQLNonNull
3032import graphql.schema.GraphQLObjectType
3133import graphql.schema.GraphQLOutputType
3234import graphql.schema.GraphQLSchema
@@ -39,13 +41,10 @@ import kotlin.reflect.KParameter
3941import kotlin.reflect.KProperty
4042import kotlin.reflect.KType
4143import kotlin.reflect.full.createType
42- import kotlin.reflect.full.declaredMemberFunctions
43- import kotlin.reflect.full.declaredMemberProperties
4444import kotlin.reflect.full.isSubclassOf
4545import kotlin.reflect.full.superclasses
4646import kotlin.reflect.full.valueParameters
4747import kotlin.reflect.jvm.javaType
48- import kotlin.reflect.jvm.jvmErasure
4948
5049@Suppress(" Detekt.UnsafeCast" )
5150internal class SchemaGenerator (
@@ -84,6 +83,7 @@ internal class SchemaGenerator(
8483 private fun addQueries (builder : GraphQLSchema .Builder ) {
8584 val queryBuilder = GraphQLObjectType .Builder ()
8685 queryBuilder.name(config.topLevelQueryName)
86+
8787 for (query in queries) {
8888 query.klazz.getValidFunctions(config.hooks)
8989 .forEach {
@@ -92,6 +92,7 @@ internal class SchemaGenerator(
9292 queryBuilder.field(functionFromHook)
9393 }
9494 }
95+
9596 builder.query(queryBuilder.build())
9697 }
9798
@@ -117,6 +118,7 @@ internal class SchemaGenerator(
117118 val builder = GraphQLFieldDefinition .newFieldDefinition()
118119 builder.name(fn.name)
119120 builder.description(fn.graphQLDescription())
121+
120122 fn.getDeprecationReason()?.let {
121123 builder.deprecate(it)
122124 }
@@ -163,23 +165,14 @@ internal class SchemaGenerator(
163165 .deprecate(prop.getDeprecationReason())
164166
165167 return if (config.dataFetcherFactory != null && prop.isLateinit) {
166- updatePropertyFieldBuilder(propertyType, fieldBuilder)
168+ updatePropertyFieldBuilder(propertyType, fieldBuilder, config.dataFetcherFactory )
167169 } else {
168170 fieldBuilder
169171 }.build()
170172 }
171173
172- private fun updatePropertyFieldBuilder (propertyType : GraphQLOutputType , fieldBuilder : GraphQLFieldDefinition .Builder ): GraphQLFieldDefinition .Builder {
173- val updatedFieldBuilder = if (propertyType is GraphQLNonNull ) {
174- fieldBuilder.type(propertyType.wrappedType as GraphQLOutputType )
175- } else {
176- fieldBuilder
177- }
178- return updatedFieldBuilder.dataFetcherFactory(config.dataFetcherFactory)
179- }
180-
181174 private fun argument (parameter : KParameter ): GraphQLArgument {
182- throwIfInterfaceIsNotAuthorized( parameter)
175+ parameter.throwIfUnathorizedInterface( )
183176 return GraphQLArgument .newArgument()
184177 .name(parameter.name)
185178 .description(parameter.graphQLDescription() ? : parameter.type.graphQLDescription())
@@ -220,21 +213,11 @@ internal class SchemaGenerator(
220213 else -> if (inputType) inputObjectType(kClass) else objectType(kClass)
221214 }
222215
223- private fun KClass <* >.canBeGraphQLInterface (): Boolean = this .java.isInterface
224-
225- private fun KClass <* >.canBeGraphQLUnion (): Boolean =
226- this .canBeGraphQLInterface() && this .declaredMemberProperties.isEmpty() && this .declaredMemberFunctions.isEmpty()
227-
228- @Throws(InvalidInputFieldTypeException ::class )
229- private fun throwIfInterfaceIsNotAuthorized (parameter : KParameter ) {
230- if (parameter.type.jvmErasure.java.isInterface) throw InvalidInputFieldTypeException ()
231- }
232-
233216 private fun listType (type : KType , inputType : Boolean ): GraphQLList =
234- GraphQLList .list(graphQLTypeOf(type.arguments.first().type !! , inputType))
217+ GraphQLList .list(graphQLTypeOf(type.getTypeOfFirstArgument() , inputType))
235218
236219 private fun objectType (kClass : KClass <* >, interfaceType : GraphQLInterfaceType ? = null): GraphQLType {
237- return cache.buildIfNotUnderConstruction(kClass) {
220+ return cache.buildIfNotUnderConstruction(kClass) { _ ->
238221 val builder = GraphQLObjectType .newObject()
239222
240223 builder.name(kClass.simpleName)
@@ -290,7 +273,7 @@ internal class SchemaGenerator(
290273 }
291274
292275 private fun interfaceType (kClass : KClass <* >): GraphQLType {
293- return cache.buildIfNotUnderConstruction(kClass) {
276+ return cache.buildIfNotUnderConstruction(kClass) { _ ->
294277 val builder = GraphQLInterfaceType .newInterface()
295278
296279 builder.name(kClass.simpleName)
@@ -311,6 +294,7 @@ internal class SchemaGenerator(
311294 .forEach {
312295 val objectType = objectType(it.kotlin, interfaceType)
313296 val key = TypesCacheKey (it.kotlin.createType(), false )
297+
314298 additionTypes.add(objectType)
315299 cache.put(key, KGraphQLType (it.kotlin, objectType))
316300 }
@@ -320,7 +304,7 @@ internal class SchemaGenerator(
320304 }
321305
322306 private fun unionType (kClass : KClass <* >): GraphQLType {
323- return cache.buildIfNotUnderConstruction(kClass) {
307+ return cache.buildIfNotUnderConstruction(kClass) { _ ->
324308 val builder = GraphQLUnionType .newUnionType()
325309
326310 builder.name(kClass.simpleName)
@@ -333,11 +317,13 @@ internal class SchemaGenerator(
333317 .forEach {
334318 val objectType = objectType(it.kotlin)
335319 val key = TypesCacheKey (it.kotlin.createType(), false )
320+
336321 if (objectType is GraphQLTypeReference ) {
337322 builder.possibleType(objectType)
338323 } else {
339324 builder.possibleType(objectType as GraphQLObjectType )
340325 }
326+
341327 cache.put(key, KGraphQLType (it.kotlin, objectType))
342328 }
343329
0 commit comments