https://github.com/excitement-engineer/graphql-klient
A simple to use graphQL client for kotlin
https://github.com/excitement-engineer/graphql-klient
client graphql kotlin
Last synced: 2 months ago
JSON representation
A simple to use graphQL client for kotlin
- Host: GitHub
- URL: https://github.com/excitement-engineer/graphql-klient
- Owner: excitement-engineer
- Created: 2019-03-23T10:23:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-08T21:17:46.000Z (about 6 years ago)
- Last Synced: 2025-03-23T05:13:10.471Z (over 1 year ago)
- Topics: client, graphql, kotlin
- Language: Kotlin
- Size: 65.4 KB
- Stars: 1
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# GraphQL Klient
An easy to use GraphQL client for Kotlin.
## Installation
## Maven
Add bintray repository:
```xml
bintray-excitement-engineer-graphql-klient
bintray
https://dl.bintray.com/excitement-engineer/graphql-klient
```
Add dependency:
```xml
com.github.excitement-engineer
graphql-klient
{version}
```
## Gradle
Add bintray repository:
```
repositories {
maven {
url "https://dl.bintray.com/excitement-engineer/graphql-klient"
}
}
```
Add dependency:
```
compile 'com.github.excitement-engineer:graphql-klient:{version}'
```
## Quickstart
In order to make requests to a graphQL server we first need to defined a `GraphQLClient`.
```kt
val client = GraphQLClient("http://example.com/graphql)
```
Next we define data classes corresponding to the query
```kt
data class Response(
user: User
)
data class User(
val id: String,
val name: String
)
```
Finally we make the request
```kt
val query = """
{
user {
id
name
}
}
"""
val request = GraphQLRequest(query)
val response = client.performRequest(request)
// Retrieve the data
println(response.data?.user)
// Check for any errors
if (response.hasErrors) {
println(response.errors)
}
```
## Examples
### Authentication via HTTP header
```kt
val client = GraphQLClient(
endpoint = "http://example.com/graphql",
headers = mapOf(
"Authorization" to "Bearer TOKEN"
)
)
val query = """
{
user {
id
name
}
}
"""
val request = GraphQLRequest(query)
val response = client.performRequest(request)
```
### Using variables
```kt
val client = GraphQLClient("http://example.com/graphql")
val query = """
query getUser($id: ID!) {
user(id: $id) {
id
name
}
}
"""
val request = GraphQLRequest(
query = query,
variables = mapOf(
"id" to "1"
)
)
val response = client.performRequest(request)
```
### Using an operation name
```kt
val client = GraphQLClient("http://example.com/graphql")
val query = """
query singleUser {
user {
id
name
}
}
query allUsers {
users {
id
name
}
}
"""
val response = client.performRequest(
GraphQLRequest(
query = query,
operationName = "singleUser"
)
)
```