- ❤️ Sponsor Javalin
- The main project webpage is javalin.io
- Chat on Discord: https://discord.gg/sgak4e5NKv
- License summary: https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
This plugin allows implementing the GraphQL specification with some easy steps.
Add the dependencies:
Gradle setup for Javalin 5.x
repositories {
maven {
url "https://maven.reposilite.com/releases"
}
}
dependencies {
implementation "io.javalin.community.graphql:javalin-graphql:5.0.1"
}
Maven setup for Javalin 5.x
<project>
<repositories>
<repository>
<id>reposilite-repository</id>
<url>https://maven.reposilite.com/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.javalin.community.graphql</groupId>
<artifactId>javalin-graphql</artifactId>
<version>5.0.1</version>
</dependency>
</dependencies>
</project>
Register the plugin:
val app = Javalin.create {
val graphQLOption = GraphQLOptions("/graphql", ContextExample())
.addPackage("io.javalin.examples")
.register(QueryExample(message))
.register(MutationExample(message))
.register(SubscriptionExample())
.context()
it.registerPlugin(GraphQLPlugin(graphQLOption))
}
app.start()
The GraphQL is now available under the /graphql
endpoint.
This section contains an overview of all the available to create queries.
@GraphQLDescription("Query Example")
class QueryExample : QueryGraphql {
fun hello(): String = "Hello world"
fun demoData(@GraphQLDescription("awesome input") data: DemoData): DemoData = data
}
After creating this class is necessary to register the class at the start of the plugin.
This section contains an overview of all the available to create commands.
@GraphQLDescription("Command Example")
class CommandExample : CommandGraphql {
fun hello(): String = "Hello world"
fun demoData(@GraphQLDescription("awesome input") data: DemoData): DemoData = data
}
After creating this class is necessary to register the class at the start of the plugin.
This section contains an overview of all the available to create a subscription.
@GraphQLDescription("Subscription Example")
class SubscriptionExample: SubscriptionGraphql {
fun counter(): Flux<Int> = Flux.interval(Duration.ofMillis(100)).map { 1 }
}
After creating this class is necessary to register the class at the start of the plugin.
Sometimes it is necessary to pass the context in the method. You can create this context with this class.
class ContextExample {
val globalEnvironment = "globalEnvironment"
}
After creating this class is necessary to register the class at the start of the plugin.
Then is possible to access this context with this annotation @GraphQLContext.
class QueryExample() : QueryGraphql {
fun context(@GraphQLContext context: ContextExample): ContextExample {
return context
}
}