aPureBase / KGraphQL

Pure Kotlin GraphQL implementation
https://kgraphql.io
MIT License
298 stars 58 forks source link

Add ID scalar type to BUILT_IN_TYPE #173

Open ima9dan opened 2 years ago

ima9dan commented 2 years ago

The graphql specification states that id scalar type should be supported by all graphql servers. Therefore, I think that it is necessary to add the ID type to "BUILT_IN_TYPE" even in kgraphql.

[graphql specification] http://spec.graphql.org/October2021/

At present, it can be realized in each project by the following method, but I think that it should be provided by default of kgraphql, but what about?

// define
class ID(var value:String) {
    override fun toString():String {
        return value
    }
}

// make scalar type
fun SchemaBuilder.schemaScalar() {
    stringScalar<ID> {
        description = "ID"
        deserialize = { it:String -> ID(it) }
        serialize = { it: ID -> it.toString() }
    }
}

// option. convenient to have
fun String.toID():ID {
    return ID(this)
}

//  make data class
data class User(var id:ID, var name:String)

val user = User("user01".toID(), "Hathaway Noa")