Skip to content

Ktor

If you are running a ktor server. There is a separate package that makes it easy to set up a fully functional GraphQL server.

You first need to add the KGraphQL-ktor package to your dependency

implementation("com.apurebase:kgraphql-ktor:$KGraphQLVersion")
implementation 'com.apurebase:kgraphql-ktor:${KGraphQLVersion}'
<dependency>
    <groupId>com.apurebase</groupId>
    <artifactId>kgraphql-ktor</artifactId>
    <version>${KGraphQLVersion}</version>
</dependency>

Initial setup

To set up KGraphQL you'll need to install the GraphQL feature like you would any other ktor feature.

fun Application.module() {
  install(GraphQL) {
    playground = true 
    schema {
      query("hello") {
        resolver { -> "World!" }
      }
    }
  }
}

Now you have a fully working GraphQL server. We have also set playground = true, so when running this you will be able to open http://localhost:8080/graphql (your port number may vary) in your browser and test it out directly within the browser.

Configuration options

The GraphQL feature is extending the standard KGraphQL configuration and providing its own set of configuration as described in the table below.

Property Description Default value
playground Gives you out of the box access to a GraphQL playground false
endpoint This specifies what route will be delivering the GraphQL endpoint. When playground is enabled, it will use this endpoint also. /graphql
context Refer to example below
wrap If you want to wrap the route into something before KGraphQL will install the GraphQL route. You can use this wrapper. See example below for a more in depth on how to use it.
schema This is where you are defining your schema. Please refer to KGraphQL References for further documentation on this. required

Wrap

Sometimes you would need to wrap your route within something. A good example of this would be the authenticate function provided by ktor Authentication feature.

wrap {
  authenticate(optional = true, build = it)
}

This works great alongside the context to provide a context to the KGraphQL resolvers.

Context

To get access to the context

context { call ->
  // access to authentication is only available if this is wrapped inside a `authenticate` before hand. 
  call.authentication.principal<User>()?.let {
    +it
  }
}
schema {
  query("hello") {
    resolver { ctx: Context ->
      val user = ctx.get<User>()!!
      "Hello ${user.name}"
    }
  }  
}