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 this GraphQL query to list all notes:
If you are not familiar with GraphQL syntax, it is recommended to read the
GraphQL introduction for an overview of how Graphql
works. Usually, you should be able to use queries from other tools (e.g.
GraphiQL) with a few tweaks.
First, let's see what Kraph provides for you.
Simple GraphQL
query and mutation represents the Query and Mutation operations of GraphQL.
The name of the query or mutaton can be passed as a string.
GraphQL:
queryGetUsers {
...
}
Kraph:
Kraph {
query("GetUsers") {
...
}
}
GraphQL:
mutationUpdateUserProfile {
...
}
Kraph:
Kraph {
mutation("UpdateUserProfile") {
...
}
}
field and fieldObject represent accessors for fields. Though there are
technically no differences, fieldObject may be chosen for clarity to indicate
that a field must contain another set of nested fields as an argument.
Both of them take a Map<String, Any> that maps Kotlin data types to the
GraphQL data types for input objects. You can also specify an alias using alias to change
the name of the returned field.
Kraph {
query {
fieldObject("users") {
field("name", alias ="nick")
field("email")
field("avatarUrl", args =mapOf("size" to 100))
}
}
}
fragment provides a mechanism for creating GraphQL Fragments. To use a fragment
in a query requires two steps. The first is to define the fragment, letting
Kraph know how to handle it later:
Kraph.defineFragment("UserFragment") {
field("name")
field("email")
field("avatarUrl", mapOf("size" to 100))
}
Then, when you are creating your query, you can simply use the fragment and
its fields will be expanded:
query {
users {
...UserFragment
}
}
fragment provides a mechanism for creating GraphQL Fragments. To use a fragment
in a query requires two steps. The first is to define the fragment, letting
Kraph know how to handle it later:
requestQueryString(), requestVariableString() and requestOperationName()
provide more fine grained access to the components of the full request string,
which are sometimes necessary depending on your HTTP request builder and
GraphQL server setup. They provide the values for the query, variables,
and operationName parameters, respectively, and so are good for creating
GET requests. Please note that requestVariableString() will always return
null until variable support is implemented.
Contributing to Kraph
We use Github issues for tracking bugs and requests.
Any feedback and/or PRs is welcome.
请发表评论