Skip to content

Commit 99ee0f0

Browse files
authored
graphql-kotlin-types (#729)
### 📝 Description Create new package `graphql-kotlin-types` which contains the common classes used by both client and server. * Moved `GraphQLRequest` and `GraphQLResponse` previously in the spring package to `com.expediagroup.graphql.types` * Created new `GraphQLError` that is not dependent on graphql-java types This also removed the types that were previously defined in `graphql-kotlin-spring-server` so this is a breaking change. We don't expect other developers to be using this package directly but allows us to shared code across modules. ### 🔗 Related Issues n|A
1 parent d60e439 commit 99ee0f0

File tree

60 files changed

+698
-254
lines changed

Some content is hidden

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

60 files changed

+698
-254
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ GraphQL Kotlin is a collection of libraries built on top of [graphql-java](https
99

1010
## 📦 Modules
1111

12+
* [examples](/examples) - Example apps that use graphql-kotlin libraries to test and demonstrate usages
1213
* [graphql-kotlin-client](/graphql-kotlin-client) - Lightweight GraphQL Kotlin HTTP client
13-
* [graphql-kotlin-federation](/graphql-kotlin-federation) - Schema generator extension to build federated GraphQL schemas
14+
* [graphql-kotlin-federation](/graphql-kotlin-federation) - Schema generator extension to build Apollo Federation GraphQL schemas
1415
* [graphql-kotlin-schema-generator](/graphql-kotlin-schema-generator) - Code only GraphQL schema generation for Kotlin
15-
* [graphql-kotlin-spring-server](/graphql-kotlin-spring-server) - Spring Boot auto-configuration library to create a GraphQL web app
16-
* [examples](/examples) - Example apps that use graphql-kotlin libraries to test and demonstrate usages
16+
* [graphql-kotlin-spring-server](/graphql-kotlin-spring-server) - Spring Boot auto-configuration library to create a GraphQL server
17+
* [graphql-kotlin-types](/graphql-kotlin-types) - Core types used by both client and server
1718
* [plugins](/plugins) - GraphQL Kotlin Gradle and Maven plugins
1819

1920
## ⌨️ Usage

docs/client/client-overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ Plugins will generate following client code
131131
package com.example.generated
132132

133133
import com.expediagroup.graphql.client.GraphQLClient
134-
import com.expediagroup.graphql.client.GraphQLResult
134+
import com.expediagroup.graphql.types.GraphQLResponse
135135
import kotlin.String
136136

137137
const val HELLO_WORLD_QUERY: String = "query HelloWorldQuery {\n helloWorld\n}"
138138

139139
class HelloWorldQuery(
140140
private val graphQLClient: GraphQLClient
141141
) {
142-
suspend fun execute(): GraphQLResult<HelloWorldQuery.Result> =
142+
suspend fun execute(): GraphQLResponse<HelloWorldQuery.Result> =
143143
graphQLClient.execute(HELLO_WORLD_QUERY, "HelloWorldQuery", null)
144144

145145
data class Result(

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/DataAndErrorsQuery.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ import java.util.concurrent.CompletableFuture
2828
class DataAndErrorsQuery : Query {
2929

3030
fun returnDataAndErrors(): DataFetcherResult<String> {
31-
val error = SimpleKotlinGraphQLError(RuntimeException(), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
31+
val error = SimpleKotlinGraphQLError(RuntimeException("data and errors"), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
3232
return DataFetcherResult.newResult<String>()
3333
.data("Hello from data fetcher")
3434
.error(error)
3535
.build()
3636
}
3737

3838
fun completableFutureDataAndErrors(): CompletableFuture<DataFetcherResult<String>> {
39-
val error = SimpleKotlinGraphQLError(RuntimeException(), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
39+
val error = SimpleKotlinGraphQLError(RuntimeException("data and errors"), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
4040
val dataFetcherResult = DataFetcherResult.newResult<String>()
4141
.data("Hello from data fetcher")
4242
.error(error)
4343
.build()
44-
4544
return CompletableFuture.completedFuture(dataFetcherResult)
4645
}
4746
}

examples/spring/src/test/kotlin/com/expediagroup/graphql/examples/query/DataAndErrorsQueryIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DataAndErrorsQueryIT(@Autowired private val testClient: WebTestClient) {
4040
@ValueSource(strings = ["returnDataAndErrors", "completableFutureDataAndErrors"])
4141
fun `verify data and errors queries`(query: String) {
4242
val expectedData = "Hello from data fetcher"
43-
val expectedError = "Exception while fetching data () : null"
43+
val expectedError = "Exception while fetching data () : data and errors"
4444

4545
testClient.post()
4646
.uri(GRAPHQL_ENDPOINT)

examples/spring/src/test/kotlin/com/expediagroup/graphql/examples/subscriptions/SimpleSubscriptionIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package com.expediagroup.graphql.examples.subscriptions
1818

1919
import com.expediagroup.graphql.examples.SUBSCRIPTION_ENDPOINT
20-
import com.expediagroup.graphql.spring.model.GraphQLRequest
2120
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage
2221
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ClientMessages.GQL_START
22+
import com.expediagroup.graphql.types.GraphQLRequest
2323
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
2424
import com.fasterxml.jackson.module.kotlin.readValue
2525
import com.fasterxml.jackson.module.kotlin.registerKotlinModule

graphql-kotlin-client/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ val ktorVersion: String by project
44
val kotlinCoroutinesVersion: String by project
55

66
dependencies {
7+
api(project(path = ":graphql-kotlin-types"))
78
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
89
api("io.ktor:ktor-client-cio:$ktorVersion")
910
api("io.ktor:ktor-client-json:$ktorVersion")

graphql-kotlin-client/src/main/kotlin/com/expediagroup/graphql/client/GraphQLClient.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.expediagroup.graphql.client
1818

19+
import com.expediagroup.graphql.types.GraphQLResponse
1920
import com.fasterxml.jackson.databind.DeserializationFeature
2021
import com.fasterxml.jackson.databind.JavaType
2122
import com.fasterxml.jackson.databind.ObjectMapper
@@ -48,6 +49,7 @@ class GraphQLClient<in T : HttpClientEngineConfig>(
4849
) : Closeable {
4950

5051
private val typeCache = mutableMapOf<Class<*>, JavaType>()
52+
5153
init {
5254
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
5355
}
@@ -68,9 +70,9 @@ class GraphQLClient<in T : HttpClientEngineConfig>(
6870
* default serialization would attempt to serialize results back to Any object. As a workaround we get raw results as String which we then
6971
* manually deserialize using passed in result type Class information.
7072
*/
71-
suspend fun <T> executeOperation(query: String, operationName: String? = null, variables: Any? = null, resultType: Class<T>): GraphQLResult<T> {
72-
// variables are simple data classes which will be serialized as map
73-
// by using map instead of typed object we can eliminate the need to explicitly convert variables to a map
73+
suspend fun <T> execute(query: String, operationName: String? = null, variables: Any? = null, resultType: Class<T>): GraphQLResponse<T> {
74+
// Variables are simple data classes which will be serialized as map.
75+
// By using map instead of typed object we can eliminate the need to explicitly convert variables to a map
7476
val graphQLRequest = mapOf(
7577
"query" to query,
7678
"operationName" to operationName,
@@ -90,13 +92,13 @@ class GraphQLClient<in T : HttpClientEngineConfig>(
9092
/**
9193
* Executes specified GraphQL query or mutation operation.
9294
*/
93-
suspend inline fun <reified T> execute(query: String, operationName: String? = null, variables: Any? = null): GraphQLResult<T> {
94-
return executeOperation(query, operationName, variables, T::class.java)
95+
suspend inline fun <reified T> execute(query: String, operationName: String? = null, variables: Any? = null): GraphQLResponse<T> {
96+
return execute(query, operationName, variables, T::class.java)
9597
}
9698

9799
private fun <T> parameterizedType(resultType: Class<T>): JavaType {
98100
return typeCache.computeIfAbsent(resultType) {
99-
mapper.typeFactory.constructParametricType(GraphQLResult::class.java, resultType)
101+
mapper.typeFactory.constructParametricType(GraphQLResponse::class.java, resultType)
100102
}
101103
}
102104

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/extensions/GraphQLSchemaExtensionsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import com.expediagroup.graphql.annotations.GraphQLDirective
2222
import com.expediagroup.graphql.annotations.GraphQLIgnore
2323
import com.expediagroup.graphql.annotations.GraphQLName
2424
import com.expediagroup.graphql.getTestSchemaConfigWithMockedDirectives
25+
import com.expediagroup.graphql.scalars.ID
2526
import com.expediagroup.graphql.testSchemaConfig
2627
import com.expediagroup.graphql.toSchema
27-
import com.expediagroup.graphql.scalars.ID
2828
import graphql.introspection.Introspection
2929
import graphql.schema.GraphQLSchema
3030
import org.junit.jupiter.api.Test

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/ToSchemaTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import com.expediagroup.graphql.annotations.GraphQLName
2424
import com.expediagroup.graphql.exceptions.ConflictingTypesException
2525
import com.expediagroup.graphql.exceptions.GraphQLKotlinException
2626
import com.expediagroup.graphql.extensions.deepName
27+
import com.expediagroup.graphql.scalars.ID
2728
import com.expediagroup.graphql.testSchemaConfig
2829
import com.expediagroup.graphql.toSchema
29-
import com.expediagroup.graphql.scalars.ID
3030
import graphql.ExceptionWhileDataFetching
3131
import graphql.GraphQL
3232
import graphql.Scalars

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/types/GenerateArgumentTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package com.expediagroup.graphql.generator.types
1919
import com.expediagroup.graphql.annotations.GraphQLDescription
2020
import com.expediagroup.graphql.annotations.GraphQLName
2121
import com.expediagroup.graphql.exceptions.InvalidInputFieldTypeException
22-
import com.expediagroup.graphql.test.utils.SimpleDirective
2322
import com.expediagroup.graphql.scalars.ID
23+
import com.expediagroup.graphql.test.utils.SimpleDirective
2424
import graphql.Scalars
2525
import graphql.Scalars.GraphQLString
2626
import graphql.schema.GraphQLList

0 commit comments

Comments
 (0)