All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.darvld.wireframe.extensions.Resolution.kt Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
@file:Suppress("NOTHING_TO_INLINE")

package io.github.darvld.wireframe.extensions

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import graphql.Scalars
import graphql.schema.*
import io.github.darvld.wireframe.ProcessingEnvironment

public val IdAlias: ClassName = ClassName("io.github.darvld.wireframe.mapping", "ID")

public fun ProcessingEnvironment.idTypeAlias(): ClassName {
    return ClassName(basePackage, "ID")
}

public fun generateNameFor(type: GraphQLNamedType): String = when (type) {
    is GraphQLEnumType -> type.name
    else -> "${type.name}DTO"
}

public fun ProcessingEnvironment.typeNameFor(type: GraphQLType): TypeName {
    return when (type) {
        is GraphQLNonNull -> typeNameFor(type.originalWrappedType).nonNullable()
        is GraphQLList -> listTypeNameFor(type)
        is GraphQLScalarType -> scalarTypeNameFor(type, this)
        is GraphQLNamedType -> resolve(type).nullable()
        else -> throw IllegalArgumentException("Unsupported type: $type.")
    }
}

private fun ProcessingEnvironment.listTypeNameFor(type: GraphQLList): TypeName {
    return LIST.parameterizedBy(typeNameFor(type.originalWrappedType)).nullable()
}

private fun scalarTypeNameFor(type: GraphQLScalarType, environment: ProcessingEnvironment): TypeName = when (type) {
    Scalars.GraphQLBoolean -> BOOLEAN
    Scalars.GraphQLFloat -> FLOAT
    Scalars.GraphQLID -> environment.idTypeAlias()
    Scalars.GraphQLInt -> INT
    Scalars.GraphQLString -> STRING
    else -> throw NotImplementedError("Custom scalar types are not supported yet.")
}.nullable()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy