Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kotlinx serialization for GraphQLServerRequest #1937

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.expediagroup.graalvm.spring.schema

import com.expediagroup.graalvm.schema.model.InputOnly
import com.expediagroup.graphql.server.types.GraphQLRequest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
Expand All @@ -35,7 +34,7 @@ class TypesQueryTest(@Autowired private val testClient: WebTestClient) {
val request = GraphQLRequest(
query = "query InputOnlyQuery(\$inputArg: InputOnlyInput){ inputTypeQuery(arg: \$inputArg) }",
operationName = "InputOnlyQuery",
variables = mapOf("inputArg" to InputOnly(id = 123))
variables = mapOf("inputArg" to mapOf("id" to 123))
)

testClient.post()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@
"allDeclaredFields": true,
"queryAllDeclaredMethods": true
},
{
"name":"com.expediagroup.graphql.server.types.GraphQLRequest",
"fields":[{"name":"Companion"}]
},
{
"name":"com.expediagroup.graphql.server.types.GraphQLRequest$Companion",
"methods":[{"name":"serializer","parameterTypes":[] }]
},
{
"name":"com.expediagroup.graphql.server.types.GraphQLBatchRequest",
"fields":[{"name":"Companion"}]
},
{
"name":"com.expediagroup.graphql.server.types.GraphQLBatchRequest$Companion",
"methods":[{"name":"serializer","parameterTypes":[] }]
},
{
"name": "com.expediagroup.graphql.server.types.GraphQLServerRequestDeserializer",
"methods": [
Expand Down
7 changes: 5 additions & 2 deletions servers/graphql-kotlin-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ description = "Common code for running a GraphQL server in any HTTP server frame
plugins {
id("com.expediagroup.graphql.conventions")
alias(libs.plugins.benchmark)
alias(libs.plugins.kotlin.serialization)
}

dependencies {
api(projects.graphqlKotlinSchemaGenerator)
api(projects.graphqlKotlinDataloaderInstrumentation)
api(projects.graphqlKotlinAutomaticPersistedQueries)
api(libs.jackson)
api(libs.kotlinx.serialization.json)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.logback)
}
Expand Down Expand Up @@ -41,15 +43,16 @@ tasks {
jacocoTestCoverageVerification {
violationRules {
rule {
excludes = listOf("com.expediagroup.graphql.server.testtypes.*")
limit {
counter = "INSTRUCTION"
value = "COVEREDRATIO"
minimum = "0.88".toBigDecimal()
minimum = "0.84".toBigDecimal()
}
limit {
counter = "BRANCH"
value = "COVEREDRATIO"
minimum = "0.84".toBigDecimal()
minimum = "0.72".toBigDecimal()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.server

import com.expediagroup.graphql.server.testtypes.GraphQLServerRequest
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.json.Json
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@Fork(5)
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
open class GraphQLServerRequestDeserializationBenchmark {
private val mapper = jacksonObjectMapper()
private lateinit var request: String
private lateinit var batchRequest: String

@Setup
fun setUp() {
val loader = this::class.java.classLoader
val operation = loader.getResource("StarWarsDetails.graphql")!!.readText().replace("\n", "\\n")
val variables = loader.getResource("StarWarsDetailsVariables.json")!!.readText()
request = """
{
"operationName": "StarWarsDetails",
"query": "$operation",
"variables": $variables
}
""".trimIndent()
batchRequest = """
[
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables },
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables },
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables },
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables }
]
""".trimIndent()
}

@Benchmark
fun JacksonDeserializeGraphQLRequest(): GraphQLServerRequest = mapper.readValue(request)

@Benchmark
fun JacksonDeserializeGraphQLBatchRequest(): GraphQLServerRequest = mapper.readValue(batchRequest)

@Benchmark
fun KSerializationDeserializeGraphQLRequest(): GraphQLServerRequest = Json.decodeFromString(request)

@Benchmark
fun KSerializationDeserializeGraphQLBatchRequest(): GraphQLServerRequest = Json.decodeFromString(batchRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.server

import com.expediagroup.graphql.server.testtypes.GraphQLBatchRequest
import com.expediagroup.graphql.server.testtypes.GraphQLRequest
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@Fork(5)
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
open class GraphQLServerRequestSerializationBenchmark {
private val mapper = jacksonObjectMapper()
private lateinit var request: GraphQLRequest
private lateinit var batchRequest: GraphQLBatchRequest

@Setup
fun setUp() {
val loader = this::class.java.classLoader
val operation = loader.getResource("StarWarsDetails.graphql")!!.readText().replace("\n", "\\n")
val variables = mapper.readValue<Map<String, Any?>>(
loader.getResourceAsStream("StarWarsDetailsVariables.json")!!
)
request = GraphQLRequest(operation, "StarWarsDetails", variables)
batchRequest = GraphQLBatchRequest(
GraphQLRequest(operation, "StarWarsDetails", variables),
GraphQLRequest(operation, "StarWarsDetails", variables),
GraphQLRequest(operation, "StarWarsDetails", variables),
GraphQLRequest(operation, "StarWarsDetails", variables)
)
}

@Benchmark
fun JacksonSerializeGraphQLRequest(): String = mapper.writeValueAsString(request)

@Benchmark
fun JacksonSerializeGraphQLBatchRequest(): String = mapper.writeValueAsString(batchRequest)

@Benchmark
fun KSerializationSerializeGraphQLRequest(): String = Json.encodeToString(request)

@Benchmark
fun KSerializationSerializeGraphQLBatchRequest(): String = Json.encodeToString(batchRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.server

import com.expediagroup.graphql.server.testtypes.GraphQLServerResponse
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.json.Json
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@Fork(5)
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
open class GraphQLServerResponseDeserializationBenchmark {
private val mapper = jacksonObjectMapper()
private lateinit var response: String
private lateinit var batchResponse: String

@Setup
fun setUp() {
response = this::class.java.classLoader.getResource("StarWarsDetailsResponse.json")!!.readText()
batchResponse = """
[
$response,
$response,
$response,
$response
]
""".trimIndent()
}

@Benchmark
fun JacksonDeserializeGraphQLResponse(): GraphQLServerResponse = mapper.readValue(response)

@Benchmark
fun JacksonDeserializeGraphQLBatchResponse(): GraphQLServerResponse = mapper.readValue(batchResponse)

@Benchmark
fun KSerializationDeserializeGraphQLResponse(): GraphQLServerResponse = Json.decodeFromString(response)

@Benchmark
fun KSerializationDeserializeGraphQLBatchResponse(): GraphQLServerResponse = Json.decodeFromString(batchResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.server

import com.expediagroup.graphql.server.testtypes.GraphQLBatchResponse
import com.expediagroup.graphql.server.testtypes.GraphQLResponse
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@Fork(5)
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
open class GraphQLServerResponseSerializationBenchmark {
private val mapper = jacksonObjectMapper()
private lateinit var response: GraphQLResponse
private lateinit var batchResponse: GraphQLBatchResponse

@Setup
fun setUp() {
val data = mapper.readValue<Map<String, Any?>>(
this::class.java.classLoader.getResourceAsStream("StarWarsDetailsResponse.json")!!
)
response = GraphQLResponse(
mapper.readValue<Map<String, Any?>>(
this::class.java.classLoader.getResourceAsStream("StarWarsDetailsResponse.json")!!
)
)
batchResponse = GraphQLBatchResponse(
GraphQLResponse(data),
GraphQLResponse(data),
GraphQLResponse(data),
GraphQLResponse(data)
)
}

@Benchmark
fun JacksonSerializeGraphQLResponse(): String = mapper.writeValueAsString(response)

@Benchmark
fun JacksonSerializeGraphQLBatchResponse(): String = mapper.writeValueAsString(batchResponse)

@Benchmark
fun KSerializationSerializeGraphQLResponse(): String = Json.encodeToString(response)

@Benchmark
fun KSerializationSerializeGraphQLBatchResponse(): String = Json.encodeToString(batchResponse)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2024 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@ package com.expediagroup.graphql.server

import com.expediagroup.graphql.server.extensions.isMutation
import com.expediagroup.graphql.server.types.GraphQLRequest
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Scope
Expand All @@ -32,7 +31,7 @@ import kotlin.random.Random
@Fork(1)
@Warmup(iterations = 2)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
open class GraphQLRequestBenchmark {
open class IsMutationBenchmark {
private val requests = mutableListOf<GraphQLRequest>()

@Setup
Expand Down Expand Up @@ -68,7 +67,7 @@ open class GraphQLRequestBenchmark {
requests.add(GraphQLRequest(mutation))
}

@Benchmark
// @Benchmark
fun isMutationBenchmark(): Boolean {
return requests.any(GraphQLRequest::isMutation)
}
Expand Down
Loading
Loading