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.
- Host: GitHub
- URL: https://github.com/cufyorg/graphkt
- Owner: cufyorg
- License: apache-2.0
- Created: 2022-06-19T20:10:35.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-14T06:16:54.000Z (about 2 years ago)
- Last Synced: 2024-05-06T22:33:58.840Z (almost 2 years ago)
- Topics: graphql, graphql-server, kotlin, ktor, ktor-server, subscription, websocket, websocket-server
- Language: Kotlin
- Homepage:
- Size: 408 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-graphql - Graphkt - A DSL based graphql server library for kotlin, backed by graphql-java. (Implementations / Kotlin)
- fucking-awesome-graphql - Graphkt - A DSL based graphql server library for kotlin, backed by graphql-java. (Implementations / Kotlin)
README
# Graphkt [](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 }
}
}
}
}
}
```