Skip to content

build: migrate remaining gradle plugin integration tests #1658

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

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ icu = "71.1"
junit = "5.8.2"
logback = "1.2.1"
mockk = "1.12.5"
mustache = "0.9.10"
rxjava = "3.1.5"
wiremock = "2.33.2"

Expand Down Expand Up @@ -97,7 +96,6 @@ ktor-server-cio = { group = "io.ktor", name = "ktor-server-cio", version.ref = "
ktor-server-netty = { group = "io.ktor", name = "ktor-server-netty", version.ref = "ktor" }
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
mustache = { group = "com.github.spullara.mustache.java", name = "compiler", version.ref = "mustache" }
reactor-core = { group = "io.projectreactor", name = "reactor-core", version.ref = "reactor-core" }
reactor-extensions = { group = "io.projectreactor.kotlin", name = "reactor-kotlin-extensions", version.ref = "reactor-extensions" }
reactor-test = { group = "io.projectreactor", name = "reactor-test", version.ref = "reactor-core" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.expediagroup.scalars

import com.expediagroup.graphql.generator.SchemaGeneratorConfig
import com.expediagroup.graphql.generator.TopLevelObject
import com.expediagroup.graphql.generator.scalars.ID
import com.expediagroup.graphql.generator.scalars.IDValueUnboxer
import com.expediagroup.graphql.generator.toSchema
import com.expediagroup.graphql.server.execution.GraphQLContextFactory
Expand All @@ -13,8 +12,6 @@ import com.expediagroup.graphql.server.types.GraphQLServerRequest
import com.expediagroup.scalars.queries.ScalarQuery
import com.fasterxml.jackson.databind.ObjectMapper
import graphql.GraphQL
import graphql.execution.DefaultValueUnboxer
import graphql.execution.ValueUnboxer
import io.ktor.server.request.ApplicationRequest
import io.ktor.server.request.receiveText
import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class CustomScalarKotlinxTests {

@Test
fun `verify custom scalars are correctly serialized and deserialized`() {
val engine = embeddedServer(CIO, port = 8080, module = Application::graphQLModule)
val engine = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
try {
engine.start()
runBlocking {
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"))
val port = engine.resolvedConnectors().first().port
val client = GraphQLKtorClient(url = URL("http://localhost:$port/graphql"))

val undefinedLocaleQuery = LocaleQuery(variables = LocaleQuery.Variables())
val undefinedLocaleResult = client.execute(undefinedLocaleQuery)
Expand All @@ -56,11 +57,12 @@ class CustomScalarKotlinxTests {

@Test
fun `verify undefined optionals are correctly serialized and deserialized`() {
val engine = embeddedServer(CIO, port = 8080, module = Application::graphQLModule)
val engine = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
try {
engine.start()
runBlocking {
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"))
val port = engine.resolvedConnectors().first().port
val client = GraphQLKtorClient(url = URL("http://localhost:$port/graphql"))

val undefinedWrapperQuery = OptionalScalarQuery(variables = OptionalScalarQuery.Variables())
val undefinedWrapperResult = client.execute(undefinedWrapperQuery).data?.optionalScalarQuery
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer

plugins {
id 'com.expediagroup.graphql'
alias(libs.plugins.kotlin.jvm)
}

dependencies {
implementation "com.expediagroup:graphql-kotlin-spring-client"
implementation(libs.kotlin.stdlib)
}

graphql {
client {
schemaFile = file("${project.projectDir}/schema.graphql")
packageName = "com.expediagroup.generated"
// optional configuration
allowDeprecatedFields = true
headers = ["X-Custom-Header": "My-Custom-Header-Value"]
queryFiles = [
file("${project.projectDir}/src/main/resources/queries/HelloWorldQuery.graphql"),
file("${project.projectDir}/src/main/resources/queries/UpdateNameMutation.graphql")
]
serializer = GraphQLSerializer.JACKSON
timeout { t ->
t.connect = 10000
t.read = 30000
}
}
}

tasks.named("test", Test) {
dependsOn("graphqlGenerateClient")

doLast {
// verify files were generated
if (!new File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/HelloWorldQuery.kt").exists()) {
throw new RuntimeException("failed to generate client for HelloWorldQuery")
}
if (!new File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/UpdateNameMutation.kt").exists()) {
throw new RuntimeException("failed to generate client for UpdateNameMutation")
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query {
helloWorld(name: String): String! @deprecated(reason: "this is a test schema")
}

type Mutation {
updateName(name: String!): String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query HelloWorldQuery {
helloWorld
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation UpdateNameMutation($name: String!) {
updateName(name: $name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer

plugins {
id 'com.expediagroup.graphql'
alias(libs.plugins.kotlin.jvm)
}

dependencies {
implementation "com.expediagroup:graphql-kotlin-spring-client"
implementation(libs.kotlin.stdlib)
}

graphqlGenerateClient {
packageName = "com.expediagroup.generated"
schemaFile = file("${project.projectDir}/schema.graphql")
// optional config
allowDeprecatedFields = true
serializer = GraphQLSerializer.JACKSON
queryFiles.from("${project.projectDir}/src/main/resources/queries/HelloWorldQuery.graphql",
"${project.projectDir}/src/main/resources/queries/UpdateNameMutation.graphql")
}

tasks.named("test", Test) {
dependsOn("graphqlGenerateClient")

doLast {
// verify files were generated
if (!new File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/HelloWorldQuery.kt").exists()) {
throw new RuntimeException("failed to generate client for HelloWorldQuery")
}
if (!new File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/UpdateNameMutation.kt").exists()) {
throw new RuntimeException("failed to generate client for UpdateNameMutation")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query {
helloWorld(name: String): String! @deprecated(reason: "this is a test schema")
}

type Mutation {
updateName(name: String!): String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query HelloWorldQuery {
helloWorld
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation UpdateNameMutation($name: String!) {
updateName(name: $name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
import com.expediagroup.graphql.plugin.gradle.graphql

@Suppress("DSL_SCOPE_VIOLATION") // TODO: remove once KTIJ-19369 / Gradle#22797 is fixed
plugins {
id("com.expediagroup.graphql")
alias(libs.plugins.kotlin.jvm)
}

dependencies {
implementation("com.expediagroup:graphql-kotlin-spring-client")
implementation(libs.kotlin.stdlib)
}

graphql {
client {
schemaFile = file("${project.projectDir}/schema.graphql")
packageName = "com.expediagroup.generated"
// optional
allowDeprecatedFields = true
headers = mapOf("X-Custom-Header" to "My-Custom-Header-Value")
queryFiles = listOf(
file("${project.projectDir}/src/main/resources/queries/HelloWorldQuery.graphql"),
file("${project.projectDir}/src/main/resources/queries/UpdateNameMutation.graphql")
)
}
}

tasks {
named<Test>("test") {
dependsOn("graphqlGenerateClient")

doLast {
// verify files were generated
if (!File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/HelloWorldQuery.kt").exists()) {
throw RuntimeException("failed to generate client for HelloWorldQuery")
}
if (!File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/UpdateNameMutation.kt").exists()) {
throw RuntimeException("failed to generate client for UpdateNameMutation")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query {
helloWorld(name: String): String! @deprecated(reason: "this is a test schema")
}

type Mutation {
updateName(name: String!): String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query HelloWorldQuery {
helloWorld
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation UpdateNameMutation($name: String!) {
updateName(name: $name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
import com.expediagroup.graphql.plugin.gradle.tasks.GraphQLGenerateClientTask

@Suppress("DSL_SCOPE_VIOLATION") // TODO: remove once KTIJ-19369 / Gradle#22797 is fixed
plugins {
id("com.expediagroup.graphql")
alias(libs.plugins.kotlin.jvm)
}

dependencies {
implementation("com.expediagroup:graphql-kotlin-spring-client")
implementation(libs.kotlin.stdlib)
}

val graphqlGenerateClient by tasks.getting(GraphQLGenerateClientTask::class) {
packageName.set("com.example.generated")
schemaFile.set(file("${project.projectDir}/schema.graphql"))
// optional config
allowDeprecatedFields.set(true)
queryFiles.from(
"${project.projectDir}/src/main/resources/queries/HelloWorldQuery.graphql",
"${project.projectDir}/src/main/resources/queries/UpdateNameMutation.graphql"
)
}

tasks {
named<Test>("test") {
dependsOn("graphqlGenerateClient")

doLast {
// verify files were generated
if (!File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/HelloWorldQuery.kt").exists()) {
throw RuntimeException("failed to generate client for HelloWorldQuery")
}
if (!File(project.buildDir, "generated/source/graphql/main/com/expediagroup/generated/UpdateNameMutation.kt").exists()) {
throw RuntimeException("failed to generate client for UpdateNameMutation")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query {
helloWorld(name: String): String! @deprecated(reason: "this is a test schema")
}

type Mutation {
updateName(name: String!): String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query HelloWorldQuery {
helloWorld
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation UpdateNameMutation($name: String!) {
updateName(name: $name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import com.expediagroup.graphql.plugin.gradle.config.GraphQLSerializer
import com.expediagroup.graphql.plugin.gradle.tasks.GraphQLGenerateSDLTask
import com.expediagroup.graphql.plugin.gradle.tasks.GraphQLGenerateTestClientTask

@Suppress("DSL_SCOPE_VIOLATION") // TODO: remove once KTIJ-19369 / Gradle#22797 is fixed
plugins {
id("com.expediagroup.graphql")
alias(libs.plugins.kotlin.jvm)
application
}

dependencies {
implementation("com.expediagroup", "graphql-kotlin-ktor-client") {
exclude("com.expediagroup", "graphql-kotlin-client-serialization")
}
implementation("com.expediagroup", "graphql-kotlin-client-jackson")
implementation("com.expediagroup:graphql-kotlin-server")
implementation(libs.icu)
implementation(libs.ktor.server.core)
implementation(libs.ktor.server.cio)
implementation(libs.ktor.server.netty)
implementation(libs.logback)
testImplementation(libs.junit.api)
testImplementation(libs.junit.engine)
testImplementation(libs.kotlin.junit.test)
testImplementation(libs.ktor.server.test.host)
}

val graphqlGenerateSDL by tasks.getting(GraphQLGenerateSDLTask::class) {
packages.set(listOf("com.expediagroup.ktor.jackson"))
}
val graphqlGenerateTestClient by tasks.getting(GraphQLGenerateTestClientTask::class) {
packageName.set("com.expediagroup.generated")
schemaFile.set(graphqlGenerateSDL.schemaFile)
serializer.set(GraphQLSerializer.JACKSON)
useOptionalInputWrapper.set(true)
}

tasks {
test {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.expediagroup.ktor.jackson

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.routing

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

fun Application.graphQLModule() {
val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
val ktorGraphQLServer: KtorGraphQLServer = KtorGraphQLServer(jacksonObjectMapper)

install(Routing)
routing {
post("graphql") {
val result = ktorGraphQLServer.execute(call.request)
if (result != null) {
val json = jacksonObjectMapper.writeValueAsString(result)
call.response.call.respond(json)
} else {
call.response.call.respond(HttpStatusCode.BadRequest, "Invalid request")
}
}
get("playground") {
this.call.respondText(buildPlaygroundHtml("graphql", "subscriptions"), ContentType.Text.Html)
}
}
}

private fun buildPlaygroundHtml(graphQLEndpoint: String, subscriptionsEndpoint: String) =
Application::class.java.classLoader.getResource("graphql-playground.html")?.readText()
?.replace("\${graphQLEndpoint}", graphQLEndpoint)
?.replace("\${subscriptionsEndpoint}", subscriptionsEndpoint)
?: throw IllegalStateException("graphql-playground.html cannot be found in the classpath")
Loading