Skip to content

javalin/javalin-graphql

Repository files navigation

Chat at https://discord.gg/sgak4e5NKv Test all JDKs on all OSes License Maven

About Javalin

Javalin GraphQL

This plugin allows implementing the GraphQL specification with some easy steps.

Getting Started

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.

Create Query

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.

Create Command

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.

Create Subscription

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.

Pass context

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
    }
}

About

Artifact/module for enabling graphql support in Javalin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published