This will bring up a GraphQL endpoint at http://localhost:8080/graphql and the GraphiQL query tool at http://localhost:8080/graphiql if you run ./gradlew bootRun
build.gradle.kts
plugins {
id("io.spring.dependency-management") version ("1.0.6.RELEASE") // Pull in dependencies automatically.
id("org.jetbrains.kotlin.jvm") version ("1.3.10")
id("org.jetbrains.kotlin.plugin.spring") version ("1.3.10")
id("org.springframework.boot") version ("2.1.0.RELEASE")
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).all {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict") // Enable strict null safety.
jvmTarget = "1.8"
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.expedia.www:graphql-kotlin:0.0.23") // Generate GraphQL schema directly from code.
implementation("com.graphql-java-kickstart:graphiql-spring-boot-starter:5.1") // Get the /graphiql page for free.
implementation("com.graphql-java-kickstart:graphql-spring-boot-starter:5.1")
implementation("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
Application.kt
package {yourpackagehere}
import com.expedia.graphql.schema.SchemaGeneratorConfig
import com.expedia.graphql.TopLevelObjectDef
import com.expedia.graphql.toSchema
import com.fasterxml.jackson.module.kotlin.KotlinModule
import graphql.schema.GraphQLSchema
import graphql.schema.idl.SchemaPrinter
import graphql.servlet.GraphQLErrorHandler
import graphql.servlet.GraphQLInvocationInputFactory
import graphql.servlet.GraphQLObjectMapper
import graphql.servlet.GraphQLQueryInvoker
import graphql.servlet.ObjectMapperConfigurer
import graphql.servlet.SimpleGraphQLHttpServlet
import javax.servlet.http.HttpServlet
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.ServletRegistrationBean
import org.springframework.context.annotation.Bean
@SpringBootApplication
class Application {
private val logger = LoggerFactory.getLogger(Application::class.java)
@Bean
fun schema(): GraphQLSchema {
val schemaConfig = SchemaGeneratorConfig(
supportedPackages = listOf("{yourpackagehere}"),
topLevelQueryName = "YourQuery",
topLevelMutationName = "YourMutation"
)
val schema = toSchema(
queries = listOf(TopLevelObjectDef(YourQuery())),
mutations = emptyList(),
config = schemaConfig
)
println(SchemaPrinter().print(schema))
return schema
}
@Bean
fun graphQLObjectMapper(): GraphQLObjectMapper = GraphQLObjectMapper.newBuilder()
.withObjectMapperConfigurer(ObjectMapperConfigurer { it.registerModule(KotlinModule()) })
.withGraphQLErrorHandler(GraphQLErrorHandler { it })
.build()
@Bean
fun graphQLServlet(
invocationInputFactory: GraphQLInvocationInputFactory,
queryInvoker: GraphQLQueryInvoker,
objectMapper: GraphQLObjectMapper
): SimpleGraphQLHttpServlet = SimpleGraphQLHttpServlet.newBuilder(invocationInputFactory)
.withQueryInvoker(queryInvoker)
.withObjectMapper(objectMapper)
.build()
@Bean
fun graphQLServletRegistration(graphQLServlet: HttpServlet) = ServletRegistrationBean(graphQLServlet, "/graphql")
}
fun main(args: Array) {
runApplication(*args)
}
