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

https://github.com/cufyorg/graphkt

A DSL based graphql server library for kotlin backed by graphql-java.
https://github.com/cufyorg/graphkt

graphql graphql-server kotlin ktor ktor-server subscription websocket websocket-server

Last synced: 3 months ago
JSON representation

A DSL based graphql server library for kotlin backed by graphql-java.

Awesome Lists containing this project

README

          

# Graphkt [![](https://jitpack.io/v/org.cufy/graphkt.svg)](https://jitpack.io/#org.cufy/graphkt)

A GraphQL library based on
[graphql-java](http://github.com/graphql-java/graphql-java)
with kotlin-friendly builders and ktor routing extensions.

### Install

The main way of installing this library is
using `jitpack.io`

```kts
repositories {
// ...
mavenCentral()
maven { url = uri("https://jitpack.io") }
}

dependencies {
// Replace TAG with the desired version
val graphkt_version = "TAG"
implementation("org.cufy.graphkt:graphkt-core:$graphkt_version")
implementation("org.cufy.graphkt:graphkt-ktor:$graphkt_version")
implementation("org.cufy.graphkt:graphkt-graphql-java:$graphkt_version")
}
```

### How to set up on ktor

The following is an example of using it.

```kotlin
data class Entity(
val name: String
)

val EntityObjectType: GraphQLObjectType = GraphQLObjectType {
name = "Entity"
description = "Some entity."

field(Entity::name) {
description = "The name of the entity."
type { GraphQLStringType }
}

field("nameWithCustomVar") {
description = "The name of the entity with the customVar in the context."
type { GraphQLStringType.Nullable }

get {
it.name + context["myCustomVar"]
}
}
}

val EntitiesFlow = MutableSharedFlow()

fun Application.configureGraphQL() {
// you can choose any of these IDEs
// graphiql()
// apolloSandbox()
graphqlPlayground() // recommended

graphql {
// add import org.cufy.graphkt.java.`graphql-java`
`graphql-java` {
// engine-specific configuration
}

before {
context["myCustomVar"] = Math.random()
}

schema {
query {
description = "The root query."

field("getEntityWithName") {
description = "Get an entity instance."
type { EntityObjectType }

val nameArg = argument("name") {
description = "The name of the entity."
type { GraphQLStringType }
}

get { Entity(nameArg()) }
}
}
mutation {
description = "The root mutation"

field("pushEntity") {
description = "Push an entity to subscribers"
type { EntityObjectType }

val nameArg = argument("name") {
description = "The name of the entity."
type { GraphQLStringType }
}

get {
val entity = Entity(nameArg())
EntitiesFlow.emit(entity)
entity
}
}
}
subscription {
description = "The root subscription"

field("subscribeToEntities") {
description = "Subscribe to pushed entities"
type { EntityObjectType }

getFlow { EntitiesFlow }
}
}
}
}
}
```