= graphql-kotlin-dsl
:sectanchors:
Kotlin DSL to generate GraphQL Instance and Schema IDL for https://github.com/graphql-java/graphql-java
Use a Kotlin DSL to configure your schema : query, mutation, type, enum Add/remove field on any kotlin Class ** Add descriptions
Add DataFetchers inside your DSL for : query, mutation Fields inside a type
Or just use extensions for graphql-java library
<<RuntimeWiring, GraphQL and RuntimeWiring>>
<
== Download
== Sample
data class User( val name: String, val email: String, val deleteField: Int )
enum class Right { read, write, execute }
schemaDsl {
// Scalar
scalar
// Types and Enums
type<User>(typeDescription = "An User") {
desc("email", "User Email")
addField<String>(name = "otherName") {
asyncDataFetcher<String>("otherName") {
"MyOtherName"
}
}
addField<Right> {
description = "User Right"
nullable = true
asyncDataFetcher<Right>("right") {
Right.execute
}
}
dropField("deleteField")
}
enum<Right>(enumDescription = "An enum") { }
// Queries
query<User>(queryDescription = "User By Id") {
arg<UUID> { name = "id" }
asyncDataFetcher { env ->
Stubs.users.firstOrNull { it.id == env.arguments["id"] }
}
}
query<Unit> {
name = "users"
description = "All Users"
returnType = "[User]"
staticDataFetcher {
Stubs.users
}
}
// Mutations
mutation<User>(mutationDescription = "Update a user") {
name = "updateUser"
arg<Int> {
name = "count"
nullable = true
}
arg<String> { name = "name" }
asyncDataFetcher { env ->
User(UUID.randomUUID(), env.arguments["name"] as String, "email@gql.io", 5)
}
}
}.graphQL { queryExecutionStrategy(AsyncExecutionStrategy())
== How that work
:sectnums:
1. Generate this GraphQl String schema :
schema { query: QueryType mutation: MutationType }
type QueryType {
user(id: UUID!): User!
# All Users
users: [User]!
}
type MutationType {
updateUser(count: Int, name: String!): User!
}
scalar Double scalar UUID
enum Right { read write execute }
type User {
email: String!
id: UUID!
name: String!
otherName: String!
# User Right
right: Right
2. Use the DataFetcher from the DSL to generate RuntimeWiring :
SchemaParser.parse()
and SchemaGenerator.makeExecutableSchema()
dataFetcher { ... }
DSL3. Configure GraphQl.Builder()
GraphQl.Builder()
:sectnums!:
[[Extension]] == graphql-java Extension functions and DSL
link:src/test/kotlin/com/tgirard12/graphqlkotlindsl/execution/ExecutionTest.kt#L39[Show graphQL sample]
[[RuntimeWiring]] === GraphQL and RuntimeWiring DSL
schemaDsl { ... }.graphQL({ // Define RuntimeWiring.Builder
scalarUUID() scalarDouble()
[[GraphQLSchema]] === GraphQLSchema instance
[[Scalar]] === Scalar DSL
link:src/main/kotlin/com/tgirard12/graphqlkotlindsl/graphqljava/GqlJavaScalars.kt#L12[Show scalars sample]
Custom scalars : GqlJavaScalars.uuid
, GqlJavaScalars.double