In short, this is a GraphQL request JSON body builder for Kotlin. It will generate the JSON string for request body that work with GraphQL Server. For example, we have the GraphQL query for list all the notes.
query {
notes {
id
createdDate
content
author {
name
avatarUrl(size: 100)
}
}
}
And here is how we write it in Kotlin using Kraph.
Kraph {
query {
fieldObject("notes") {
field("id")
field("createdDate")
field("content")
fieldObject("author") {
field("name")
field("avatarUrl", mapOf("size" to 100))
}
}
}
}
As you can see, we can achieve our goal with just a few tweaks from the original query.
NOTE: Kraph is still in an early stage. The usage may change in further development.
- DSL builder style. Make it easier to read and use.
- Support CursorConnection field and Input object for mutation in Relay.
Adding Kraph to build.gradle
repositories {
jcenter()
}
dependencies {
compile "com.taskworld.kraph:kraph:x.y.z"
}
If you are not familiar with GraphQL syntax, We recommended to read on this specification to have an overview of how to write Graphql. Usually, you should be able to use the query from other tools(e.g. GraphiQL) with a few tweaks. First, let's see what Kraph provided for you.
query
represents QUERY operation in GraphQL. It can be named by passing String as a parameter. Thequery
block can only containsfield
orfieldObject
.
/*
* query GetUsers {
* ...
* }
*/
Kraph {
query("GetUsers") {
...
}
}
field
andfieldObject
represents FIELD in SELECTION SET. The different is thatfieldObject
allow you to have it owns SELECTION SET, which represent data object in GraphQL. Both of them have a parameter namedargs
for arguments in paritcular field.
/*
* query {
* users {
* name
* email
* avatarUrl(size: 100)
* }
* }
*/
Kraph {
query {
fieldObject("users") {
field("name")
field("email")
field("avatarUrl", args = mapOf("size" to 100))
}
}
}
mutation
represents MUTATION operation in GraphQL. Themutation
block can have only containsfunc
.
/*
* mutation updateUserProfile {
* ...
* }
*/
Kraph {
mutation("UpdateUserProfile") {
...
}
}
func
represents as FIELD inside MUTATION block. The reason we did not usedfieldObject
We use Github issues for tracking bugs and requests. Any feedback and/or PRs is welcome.