/
KotlinTypesSchemaTransformer.kt
48 lines (44 loc) · 2.13 KB
/
KotlinTypesSchemaTransformer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.alediaferia.graphql.spqr.kotlin
import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLNonNull
import io.leangen.graphql.generator.BuildContext
import io.leangen.graphql.generator.OperationMapper
import io.leangen.graphql.generator.mapping.SchemaTransformer
import io.leangen.graphql.metadata.Operation
import io.leangen.graphql.metadata.OperationArgument
import java.lang.reflect.Method
import java.lang.reflect.Parameter
import kotlin.reflect.full.instanceParameter
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.jvm.kotlinFunction
class KotlinTypesSchemaTransformer : SchemaTransformer {
override fun transformField(
field: GraphQLFieldDefinition,
operation: Operation,
operationMapper: OperationMapper,
buildContext: BuildContext
): GraphQLFieldDefinition {
val kotlinFunction = (operation.typedElement.element as? Method)?.kotlinFunction
if (kotlinFunction?.instanceParameter?.type?.jvmErasure?.java?.isKotlinClass() == true)
if (!kotlinFunction.returnType.isMarkedNullable)
return field.transform { it.type(GraphQLNonNull(field.type)) }
return super.transformField(field, operation, operationMapper, buildContext)
}
override fun transformArgument(
argument: GraphQLArgument,
operationArgument: OperationArgument,
operationMapper: OperationMapper,
buildContext: BuildContext
): GraphQLArgument {
val kotlinFunction = ((operationArgument.typedElement.element as? Parameter)?.declaringExecutable as? Method)?.kotlinFunction
if (kotlinFunction?.instanceParameter?.type?.jvmErasure?.java?.isKotlinClass() == true)
for (arg in kotlinFunction.parameters)
if (arg.name == operationArgument.name) {
if (!arg.type.isMarkedNullable)
return argument.transform { builder -> builder.type(GraphQLNonNull.nonNull(argument.type)) }
break
}
return super.transformArgument(argument, operationArgument, operationMapper, buildContext)
}
}