Skip to content

Commit 8187f93

Browse files
dariuszkucsmyrick
authored andcommitted
Add new module graphql-kotlin-spring-server (ExpediaGroup#329)
* WIP: boot server * fix detekt * fix unit test + copyright notice * rename to graphql-kotlin-spring-server * fix packages * simplify package structure * add unit tests * include integration test in jacoco coverage calculation * fix javadoc link * subscription test * Fix subscription unit tests * additional unit tests * subscription handler tests are actually integration tests
1 parent 9e2f865 commit 8187f93

File tree

94 files changed

+1865
-2878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1865
-2878
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GraphQL Kotlin consists of number of libraries that aim to simplify GraphQL inte
1010

1111
* [graphql-kotlin-schema-generator](/graphql-kotlin-schema-generator) - Code only GraphQL schema generation for Kotlin
1212
* [graphql-kotlin-federation](/graphql-kotlin-federation) - Schema generator extension to build federated GraphQL schemas
13+
* [graphql-kotlin-spring-server](/graphql-kotlin-spring-server) - Spring Boot auto-configuration library to create GraphQL web app
1314
* [examples](/examples) - Example apps that use graphql-kotlin libraries to test and demonstrate usages
1415

1516
## ⌨️ Usage

examples/federation/base-app/pom.xml

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<packaging>jar</packaging>
1515

1616
<properties>
17-
<spring-boot.version>2.1.2.RELEASE</spring-boot.version>
17+
<spring-boot.version>2.2.0.M5</spring-boot.version>
1818

1919
<!-- skip release plugins -->
2020
<maven.source.skip>true</maven.source.skip>
@@ -23,19 +23,9 @@
2323
<dependencies>
2424
<dependency>
2525
<groupId>com.expediagroup</groupId>
26-
<artifactId>graphql-kotlin-schema-generator</artifactId>
26+
<artifactId>graphql-kotlin-spring-server</artifactId>
2727
<version>${project.parent.version}</version>
2828
</dependency>
29-
<dependency>
30-
<groupId>com.expediagroup</groupId>
31-
<artifactId>graphql-kotlin-federation</artifactId>
32-
<version>${project.parent.version}</version>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.springframework.boot</groupId>
36-
<artifactId>spring-boot-starter-webflux</artifactId>
37-
<version>${spring-boot.version}</version>
38-
</dependency>
3929
</dependencies>
4030

4131
<build>
@@ -81,11 +71,10 @@
8171
</dependency>
8272
</dependencies>
8373
</plugin>
84-
<!-- TODO enable detekt/ktlint -->
85-
<!-- <plugin>-->
86-
<!-- <groupId>org.apache.maven.plugins</groupId>-->
87-
<!-- <artifactId>maven-antrun-plugin</artifactId>-->
88-
<!-- </plugin>-->
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-antrun-plugin</artifactId>
77+
</plugin>
8978
<plugin>
9079
<groupId>org.jetbrains.dokka</groupId>
9180
<artifactId>dokka-maven-plugin</artifactId>

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/Application.kt

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,13 @@
1616

1717
package com.expediagroup.graphql.sample
1818

19-
import com.expediagroup.graphql.TopLevelObject
20-
import com.expediagroup.graphql.execution.KotlinDataFetcherFactoryProvider
21-
import com.expediagroup.graphql.extensions.print
22-
import com.expediagroup.graphql.federation.FederatedSchemaGeneratorConfig
23-
import com.expediagroup.graphql.federation.FederatedSchemaGeneratorHooks
24-
import com.expediagroup.graphql.federation.execution.FederatedTypeRegistry
25-
import com.expediagroup.graphql.federation.toFederatedSchema
26-
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
27-
import com.expediagroup.graphql.sample.datafetchers.CustomDataFetcherFactoryProvider
28-
import com.expediagroup.graphql.sample.datafetchers.SpringDataFetcherFactory
29-
import com.expediagroup.graphql.sample.exceptions.CustomDataFetcherExceptionHandler
30-
import com.expediagroup.graphql.sample.query.Query
31-
import graphql.GraphQL
32-
import graphql.execution.AsyncExecutionStrategy
33-
import graphql.execution.AsyncSerialExecutionStrategy
34-
import graphql.execution.DataFetcherExceptionHandler
35-
import graphql.execution.SubscriptionExecutionStrategy
36-
import graphql.schema.GraphQLSchema
37-
import org.slf4j.LoggerFactory
3819
import org.springframework.boot.autoconfigure.SpringBootApplication
3920
import org.springframework.boot.runApplication
40-
import org.springframework.context.annotation.Bean
4121

4222
@SpringBootApplication
43-
class Application {
44-
45-
private val logger = LoggerFactory.getLogger(Application::class.java)
46-
47-
@Bean
48-
fun hooks() = FederatedSchemaGeneratorHooks(FederatedTypeRegistry())
49-
50-
@Bean
51-
fun dataFetcherFactoryProvider(springDataFetcherFactory: SpringDataFetcherFactory, hooks: SchemaGeneratorHooks) =
52-
CustomDataFetcherFactoryProvider(springDataFetcherFactory, hooks)
53-
54-
@Bean
55-
fun schemaConfig(hooks: FederatedSchemaGeneratorHooks, dataFetcherFactoryProvider: KotlinDataFetcherFactoryProvider) = FederatedSchemaGeneratorConfig(
56-
supportedPackages = listOf("com.expediagroup"),
57-
hooks = hooks,
58-
dataFetcherFactoryProvider = dataFetcherFactoryProvider
59-
)
60-
61-
@Bean
62-
fun schema(
63-
queries: List<Query>,
64-
schemaConfig: FederatedSchemaGeneratorConfig
65-
): GraphQLSchema {
66-
fun List<Any>.toTopLevelObjects() = this.map {
67-
TopLevelObject(it)
68-
}
69-
70-
val schema = toFederatedSchema(
71-
config = schemaConfig,
72-
queries = queries.toTopLevelObjects()
73-
)
74-
75-
logger.info(schema.print())
76-
77-
return schema
78-
}
79-
80-
@Bean
81-
fun dataFetcherExceptionHandler(): DataFetcherExceptionHandler = CustomDataFetcherExceptionHandler()
82-
83-
@Bean
84-
fun graphQL(
85-
schema: GraphQLSchema,
86-
dataFetcherExceptionHandler: DataFetcherExceptionHandler
87-
): GraphQL = GraphQL.newGraphQL(schema)
88-
.queryExecutionStrategy(AsyncExecutionStrategy(dataFetcherExceptionHandler))
89-
.mutationExecutionStrategy(AsyncSerialExecutionStrategy(dataFetcherExceptionHandler))
90-
.subscriptionExecutionStrategy(SubscriptionExecutionStrategy(dataFetcherExceptionHandler))
91-
.build()
92-
}
23+
class Application
9324

25+
@Suppress("SpreadOperator")
9426
fun main(args: Array<String>) {
9527
runApplication<Application>(*args)
9628
}

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/GraphQLResponse.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/QueryHandler.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/RoutesConfiguration.kt

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,23 @@
1616

1717
package com.expediagroup.graphql.sample
1818

19-
import com.expediagroup.graphql.extensions.print
20-
import com.fasterxml.jackson.databind.ObjectMapper
21-
import com.fasterxml.jackson.databind.type.MapType
22-
import com.fasterxml.jackson.databind.type.TypeFactory
23-
import graphql.schema.GraphQLSchema
2419
import org.springframework.beans.factory.annotation.Value
2520
import org.springframework.context.annotation.Bean
2621
import org.springframework.context.annotation.Configuration
2722
import org.springframework.core.io.Resource
28-
import org.springframework.http.HttpMethod
29-
import org.springframework.http.MediaType
30-
import org.springframework.web.reactive.function.server.ServerRequest
31-
import org.springframework.web.reactive.function.server.bodyToMono
23+
import org.springframework.web.reactive.function.server.bodyAndAwait
24+
import org.springframework.web.reactive.function.server.coRouter
3225
import org.springframework.web.reactive.function.server.html
33-
import org.springframework.web.reactive.function.server.json
34-
import org.springframework.web.reactive.function.server.router
35-
import reactor.core.publisher.Mono
3626

3727
@Configuration
3828
class RoutesConfiguration(
39-
val schema: GraphQLSchema,
40-
private val queryHandler: QueryHandler,
41-
private val objectMapper: ObjectMapper,
4229
@Value("classpath:/graphql-playground.html") private val playgroundHtml: Resource
4330
) {
4431

45-
private val mapTypeReference: MapType = TypeFactory.defaultInstance().constructMapType(HashMap::class.java, String::class.java, Any::class.java)
46-
47-
@Bean
48-
fun graphQLRoutes() = router {
49-
(POST("/graphql") or GET("/graphql")).invoke { serverRequest ->
50-
createGraphQLRequest(serverRequest)
51-
.flatMap { graphQLRequest -> queryHandler.executeQuery(graphQLRequest) }
52-
.flatMap { result -> ok().json().syncBody(result) }
53-
.switchIfEmpty(badRequest().build())
54-
}
55-
}
56-
57-
@Bean
58-
fun sdlRoute() = router {
59-
GET("/sdl") {
60-
ok().contentType(MediaType.TEXT_PLAIN).syncBody(schema.print())
61-
}
62-
}
63-
6432
@Bean
65-
fun graphQLToolRoutes() = router {
33+
fun playgroundRoute() = coRouter {
6634
GET("/playground") {
67-
ok().html().syncBody(playgroundHtml)
68-
}
69-
}
70-
71-
private fun createGraphQLRequest(serverRequest: ServerRequest): Mono<GraphQLRequest> = when {
72-
serverRequest.method() == HttpMethod.POST -> serverRequest.bodyToMono()
73-
serverRequest.queryParam("query").isPresent -> {
74-
val query = serverRequest.queryParam("query").get()
75-
val operationName: String? = serverRequest.queryParam("operationName").orElseGet { null }
76-
val variables: String? = serverRequest.queryParam("variables").orElseGet { null }
77-
val graphQLVariables: Map<String, Any>? = variables?.let {
78-
objectMapper.readValue(it, mapTypeReference)
79-
}
80-
Mono.just(GraphQLRequest(query = query, operationName = operationName, variables = graphQLVariables))
35+
ok().html().bodyAndAwait(playgroundHtml)
8136
}
82-
else -> Mono.empty()
8337
}
8438
}

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/context/MyGraphQLContext.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/sample/context/MyGraphQLContextWebFilter.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)