Quickstart
This guide will get you up and running with Viaduct in just a few minutes. You’ll clone the CLI starter demo, run your first GraphQL query, and understand the basics of how Viaduct applications work.Prerequisites
Before you begin, make sure you have:- Java JDK 21 or higher installed
JAVA_HOMEenvironment variable set correctly orjavain your PATH- Git installed
- Basic familiarity with GraphQL (see GraphQL Foundation learning materials)
Viaduct is compatible with Gradle 8.10+ and Kotlin 1.9.x or 2.x. The CLI starter comes pre-configured with compatible versions.
Clone the CLI starter
Clone the repository
Clone the CLI starter demo application from GitHub:The CLI starter is a minimal Viaduct application that demonstrates the core concepts without the complexity of a web server.
Verify your environment
Run the tests to ensure everything is set up correctly:You should see output indicating that the build and tests completed successfully:
Run your first query
Execute the default GraphQL query:You should see the following JSON response:Congratulations! You’ve just executed your first Viaduct query.
Understanding the application
Now that you have a working Viaduct application, let’s understand how it works.Project structure
The CLI starter has a simple structure:The schema
The GraphQL schema is defined insrc/main/viaduct/schema/schema.graphqls:
src/main/viaduct/schema/schema.graphqls
The
@resolver directive tells Viaduct that you’ll provide a custom function to compute the value of this field. All fields of Query must have @resolver applied to them.Query and Mutation, so you extend them rather than define them from scratch.
The resolvers
Resolvers contain the business logic for your schema. Here’s thegreeting resolver from src/main/kotlin/com/example/viadapp/resolvers/HelloWorldResolvers.kt:
src/main/kotlin/com/example/viadapp/resolvers/HelloWorldResolvers.kt
- Extends a generated base class (e.g.,
QueryResolvers.Greeting) - Is annotated with
@Resolver - Overrides the
resolvefunction to return the field value - Uses Kotlin coroutines (
suspend fun) for asynchronous execution
The application
The main application filesrc/main/kotlin/com/example/viadapp/ViaductApplication.kt ties everything together:
src/main/kotlin/com/example/viadapp/ViaductApplication.kt
- Creates a Viaduct engine using
BasicViaductFactory - Creates an
ExecutionInputfrom the command-line arguments - Executes the query with
viaduct.execute() - Prints the result as JSON
The Gradle configuration
Viaduct requires two Gradle plugins inbuild.gradle.kts:
build.gradle.kts
Next steps
Now that you have a working Viaduct application, you can:Add more fields
Extend the schema with new types and fields
Installation guide
Set up Viaduct in your own Gradle project
Core concepts
Learn about resolvers, GRTs, and code generation
Star Wars tutorial
Build a complete GraphQL API with advanced features
Troubleshooting
Error: JAVA_HOME not set
Error: JAVA_HOME not set
Make sure you have Java JDK 21 or higher installed and the
JAVA_HOME environment variable is set:Build fails with 'incompatible Kotlin version'
Build fails with 'incompatible Kotlin version'
The CLI starter uses Kotlin 2.2.21 by default. If you see Kotlin version errors, check that your Gradle version is compatible:
- Gradle 8.x: Kotlin 1.9.x or 2.x
- Gradle 9.x: Kotlin 2.0+
gradle/viaduct.versions.toml to use a compatible Kotlin version.Query returns empty or null data
Query returns empty or null data
Make sure your query syntax is correct and the field exists in the schema. Check
src/main/viaduct/schema/schema.graphqls for available fields.You can also run ./gradlew build to see if there are any schema validation errors.