An open API service indexing awesome lists of open source software.

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

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"
)
)

```