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

Clean up ClientApiGenerator #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion graphql-dgs-codegen-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ application {
mainClassName = 'com.netflix.graphql.dgs.codegen.CodeGenCliKt'
}

tasks.lintKotlinIntegTest.enabled = false
tasks.named("lintKotlinIntegTest") {
exclude("**/cases/**/*.kt")
}

tasks.named("formatKotlinIntegTest") {
exclude("**/cases/**/*.kt")
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,20 @@ class CodeGen(private val config: CodeGenConfig) {
}

private fun generateJavaClientApi(definitions: Collection<Definition<*>>): CodeGenResult {
val methodNames = mutableSetOf<String>()
return if (config.generateClientApi || config.generateClientApiv2) {
definitions.asSequence()
.filterIsInstance<ObjectTypeDefinition>()
.filter { it.name == "Query" || it.name == "Mutation" || it.name == "Subscription" }
.sortedBy { it.name.length }
.map {
ClientApiGenerator(config, document).generate(it, methodNames)
}
.fold(CodeGenResult.EMPTY) { result, next -> result.merge(next) }
} else CodeGenResult.EMPTY
if (!(config.generateClientApi || config.generateClientApiv2)) {
return CodeGenResult.EMPTY
}
var result = CodeGenResult.EMPTY
for (definition in definitions) {
if (definition !is ObjectTypeDefinition) {
continue
}
if (!(definition.name == "Query" || definition.name == "Mutation" || definition.name == "Subscription")) {
continue
}
result = result.merge(ClientApiGenerator(config, document).generate(definition))
}
return result
}

private fun generateJavaClientEntitiesApi(definitions: Collection<Definition<*>>): CodeGenResult {
Expand Down Expand Up @@ -631,15 +634,19 @@ data class CodeGenResult(
}
}

fun List<FieldDefinition>.filterSkipped(): List<FieldDefinition> {
fun Iterable<FieldDefinition>.filterSkipped(): Iterable<FieldDefinition> {
return this.filter { it.directives.none { d -> d.name == "skipcodegen" } }
}

fun Sequence<FieldDefinition>.filterSkipped(): Sequence<FieldDefinition> {
return this.filter { it.directives.none { d -> d.name == "skipcodegen" } }
}

fun List<FieldDefinition>.filterIncludedInConfig(definitionName: String, config: CodeGenConfig): List<FieldDefinition> {
fun Iterable<FieldDefinition>.filterIncludedInConfig(definitionName: String, config: CodeGenConfig): Iterable<FieldDefinition> {
return asSequence().filterIncludedInConfig(definitionName, config).toList()
}

fun Sequence<FieldDefinition>.filterIncludedInConfig(definitionName: String, config: CodeGenConfig): Sequence<FieldDefinition> {
return when (definitionName) {
"Query" -> {
if (config.includeQueries.isEmpty()) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ package com.netflix.graphql.dgs.codegen

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
import org.junit.Ignore
import org.junit.jupiter.api.Test

@Ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't do anything since it's a junit 4 annotation

class EntitiesClientApiGenTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.*
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.ValueSource
import java.util.stream.Stream
import java.util.stream.Stream.of

class KotlinCodeGenTest {

Expand Down Expand Up @@ -1115,7 +1118,7 @@ class KotlinCodeGenTest {
}

class MappedTypesTestCases : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = of(
override fun provideArguments(context: ExtensionContext): Stream<Arguments> = Stream.of(
arguments("java.time.LocalDateTime", "java.time.LocalDateTime"),
arguments("String", "kotlin.String"),
arguments("BigDecimal", "java.math.BigDecimal"),
Expand Down Expand Up @@ -1862,7 +1865,7 @@ class KotlinCodeGenTest {
companion object {
@JvmStatic
fun generateConstantsArguments(): Stream<Arguments> {
return of(
return Stream.of(
Arguments.of(
true,
listOf(
Expand Down Expand Up @@ -3614,17 +3617,24 @@ It takes a title and such.
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(9)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("ShowsGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[1].typeSpec.name).isEqualTo("MovieGraphQLQuery")

assertThat(codeGenResult.javaQueryTypes[2].typeSpec.name).isEqualTo("ShowsGraphQLMutation")
assertThat(codeGenResult.javaQueryTypes[3].typeSpec.name).isEqualTo("MovieGraphQLMutation")
assertThat(codeGenResult.javaQueryTypes[4].typeSpec.name).isEqualTo("FooGraphQLQuery")

assertThat(codeGenResult.javaQueryTypes[5].typeSpec.name).isEqualTo("ShowsGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[6].typeSpec.name).isEqualTo("MovieGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[7].typeSpec.name).isEqualTo("FooGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[8].typeSpec.name).isEqualTo("BarGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLQuery") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLQuery") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("FooGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("FooGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", com.squareup.javapoet.TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("BarGraphQLSubscription") }

assertCompilesJava(codeGenResult.javaQueryTypes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ClientApiGenMutationTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieGraphQLMutation")

assertCompilesJava(codeGenResult.clientProjections + codeGenResult.javaQueryTypes)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ class ClientApiGenMutationTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieGraphQLMutation")

assertCompilesJava(
codeGenResult.clientProjections + codeGenResult.javaQueryTypes + codeGenResult.javaDataTypes
Expand Down Expand Up @@ -162,15 +162,18 @@ class ClientApiGenMutationTest {
val expected = """
|super("mutation", queryName);
|if (movie != null || fieldsSet.contains("movie")) {
| getInput().put("movie", movie);
|}if (reviews != null || fieldsSet.contains("reviews")) {
| getInput().put("reviews", reviews);
|}if (uuid != null || fieldsSet.contains("uuid")) {
| getInput().put("uuid", uuid);
| getInput().put("movie", movie);
|}
|if (reviews != null || fieldsSet.contains("reviews")) {
| getInput().put("reviews", reviews);
|}
|if (uuid != null || fieldsSet.contains("uuid")) {
| getInput().put("uuid", uuid);
|}
|
""".trimMargin()

assert(initMethod.contains(expected))
assertThat(initMethod).isEqualTo(expected)
assertCompilesJava(
codeGenResult.clientProjections + codeGenResult.javaQueryTypes + codeGenResult.javaDataTypes
)
Expand All @@ -197,11 +200,8 @@ class ClientApiGenMutationTest {
)
).generate()

assert(
codeGenResult.javaQueryTypes[0].typeSpec.methodSpecs
.find { it.name == "<init>" }?.code.toString()
.contains("super(\"mutation\", queryName);\ngetInput().put(\"movieId\", movieId);")
)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.methodSpecs.find { it.name == "<init>" }?.code.toString())
.contains("super(\"mutation\", queryName);\ngetInput().put(\"movieId\", movieId);")

assertCompilesJava(
codeGenResult.clientProjections + codeGenResult.javaQueryTypes + codeGenResult.javaDataTypes
Expand Down Expand Up @@ -269,7 +269,7 @@ class ClientApiGenMutationTest {
assertThat(codeGenResult.javaEnumTypes)
.extracting("typeSpec").extracting("name").containsExactly("ShowType", "SourceType")
assertThat(codeGenResult.javaQueryTypes)
.extracting("typeSpec").extracting("name").containsExactly("ShowsGraphQLQuery")
.extracting("typeSpec").extracting("name").containsExactly("ShowsGraphQLMutation")
assertThat(codeGenResult.clientProjections)
.extracting("typeSpec").extracting("name").containsExactly("ShowsProjectionRoot", "BooleanProjection")

Expand All @@ -295,7 +295,7 @@ class ClientApiGenMutationTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieTitleGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("UpdateMovieTitleGraphQLMutation")

assertCompilesJava(codeGenResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.netflix.graphql.dgs.codegen.CodeGen
import com.netflix.graphql.dgs.codegen.CodeGenConfig
import com.netflix.graphql.dgs.codegen.assertCompilesJava
import com.netflix.graphql.dgs.codegen.basePackageName
import com.squareup.javapoet.TypeVariableName
import com.squareup.javapoet.TypeName
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail
Expand Down Expand Up @@ -754,11 +754,11 @@ class ClientApiGenProjectionTest {
).generate()

val methodSpecs = codeGenResult.clientProjections[1].typeSpec.methodSpecs
val methodWithArgs = methodSpecs.find { !it.isConstructor && it.parameters.size > 0 }
val methodWithArgs = methodSpecs.find { !it.isConstructor && it.parameters.isNotEmpty() }
?: fail("Method not found")
assertThat(methodWithArgs.returnType).extracting { (it as TypeVariableName).name }
.isEqualTo("AwardProjection<ActorProjection<PARENT, ROOT>, ROOT>")
assertThat(methodWithArgs.returnType).extracting { it.toString() }
.isEqualTo("com.netflix.graphql.dgs.codegen.tests.generated.client.AwardProjection<com.netflix.graphql.dgs.codegen.tests.generated.client.ActorProjection<PARENT, ROOT>, ROOT>")
assertThat(methodWithArgs.parameters[0].name).isEqualTo("oscarsOnly")
assertThat(methodWithArgs.parameters[0].type.toString()).isEqualTo("java.lang.Boolean")
assertThat(methodWithArgs.parameters[0].type).isEqualTo(TypeName.BOOLEAN.box())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package com.netflix.graphql.dgs.codegen.clientapi

import com.netflix.graphql.dgs.codegen.*
import com.squareup.javapoet.TypeSpec
import org.assertj.core.api.Assertions.assertThat
import org.junit.Ignore
import org.junit.jupiter.api.Test

@Ignore
class ClientApiGenQueryTest {
@Test
fun generateQueryType() {
Expand Down Expand Up @@ -916,17 +915,24 @@ class ClientApiGenQueryTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(9)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("ShowsGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[1].typeSpec.name).isEqualTo("MovieGraphQLQuery")

assertThat(codeGenResult.javaQueryTypes[2].typeSpec.name).isEqualTo("ShowsGraphQLMutation")
assertThat(codeGenResult.javaQueryTypes[3].typeSpec.name).isEqualTo("MovieGraphQLMutation")
assertThat(codeGenResult.javaQueryTypes[4].typeSpec.name).isEqualTo("FooGraphQLQuery")

assertThat(codeGenResult.javaQueryTypes[5].typeSpec.name).isEqualTo("ShowsGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[6].typeSpec.name).isEqualTo("MovieGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[7].typeSpec.name).isEqualTo("FooGraphQLSubscription")
assertThat(codeGenResult.javaQueryTypes[8].typeSpec.name).isEqualTo("BarGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLQuery") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("ShowsGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLQuery") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("MovieGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("FooGraphQLMutation") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("FooGraphQLSubscription") }
assertThat(codeGenResult.javaQueryTypes).extracting("typeSpec", TypeSpec::class.java)
.satisfiesOnlyOnce { spec -> assertThat(spec.name).isEqualTo("BarGraphQLSubscription") }

assertCompilesJava(codeGenResult.javaQueryTypes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ClientApiGenSubscriptionTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieGraphQLSubscription")

assertCompilesJava(codeGenResult.clientProjections + codeGenResult.javaQueryTypes)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ class ClientApiGenSubscriptionTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieGraphQLMutation")

assertCompilesJava(
codeGenResult.clientProjections + codeGenResult.javaQueryTypes + codeGenResult.javaDataTypes
Expand All @@ -107,7 +107,7 @@ class ClientApiGenSubscriptionTest {
).generate()

assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieTitleGraphQLQuery")
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("MovieTitleGraphQLSubscription")

assertCompilesJava(codeGenResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ import com.netflix.graphql.dgs.codegen.Language
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import java.io.File
import java.nio.file.Paths
import java.util.*
import java.util.Locale
import javax.inject.Inject

open class GenerateJavaTask @Inject constructor(
Expand Down Expand Up @@ -166,20 +170,24 @@ open class GenerateJavaTask @Inject constructor(

@TaskAction
fun generate() {
val schemaJarFilesFromDependencies = dgsCodegenClasspath.files.toList()
val schemaPaths = schemaPaths.map { Paths.get(it.toString()).toFile() }.sorted().toSet()
schemaPaths.filter { !it.exists() }.forEach {
logger.warn("Schema location ${it.absolutePath} does not exist")
}
val schemaPaths = schemaPaths.asSequence()
.map { Paths.get(it.toString()).toFile() }
.sorted()
.toSet()

logger.info("Processing schema files:")
schemaPaths.forEach {
logger.info("Processing $it")
for (schemaPath in schemaPaths) {
if (!schemaPath.exists()) {
logger.warn("Schema location {} does not exist", schemaPath.absolutePath)
} else {
logger.info("Processing {}", schemaPath)
}
}

val config = CodeGenConfig(
schemas = emptySet(),
schemaFiles = schemaPaths,
schemaJarFilesFromDependencies = schemaJarFilesFromDependencies,
schemaJarFilesFromDependencies = dgsCodegenClasspath.files.toList(),
outputDir = getOutputDir().toPath(),
examplesOutputDir = getExampleOutputDir().toPath(),
writeToFiles = true,
Expand Down
Loading