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

Generate interface for input types #766

Open
wants to merge 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,34 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy

logger.info("Generating input type {}", definition.name)

var useInterfaceType = false
var overrideGetter = false
var interfaceCodeGenResult = CodeGenResult.EMPTY
val name = definition.name
var implements: List<String> = emptyList()

if (config.generateInterfaces) {
useInterfaceType = true

overrideGetter = true
val fieldDefinitions = definition.inputValueDefinitions
.asSequence()
.map {
Field(it.name, typeUtils.findReturnType(it.type, useInterfaceType, true))
}
.plus(
extensions
.asSequence()
.flatMap { it.inputValueDefinitions }
.map { Field(it.name, typeUtils.findReturnType(it.type, useInterfaceType, true)) }
)
.toList()

val interfaceName = "I$name"
implements = implements + listOf(interfaceName)
interfaceCodeGenResult = generateInterface(interfaceName, emptyList(), fieldDefinitions)
}

val fieldDefinitions = definition.inputValueDefinitions.asSequence().map {
val type = typeUtils.findReturnType(it.type)
val defaultValue = it.defaultValue?.let { defVal ->
Expand All @@ -178,12 +205,13 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy
name = it.name,
type = type,
initialValue = defaultValue,
overrideGetter = overrideGetter,
description = it.description,
directives = it.directives
)
}.plus(extensions.asSequence().flatMap { it.inputValueDefinitions }.map { Field(it.name, typeUtils.findReturnType(it.type)) })
.toList()
return generate(name, emptyList(), fieldDefinitions, definition.description, definition.directives)
return generate(name, implements, fieldDefinitions, definition.description, definition.directives).merge(interfaceCodeGenResult)
}

private fun generateCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3168,7 +3168,7 @@ class CodeGenTest {
val interfaces = result.javaInterfaces
val dataTypes = result.javaDataTypes

assertThat(interfaces).hasSize(4) // IMovie, IMoviePage, IGenre, IRating
assertThat(interfaces).hasSize(5) // IMovie, IMoviePage, IGenre, IRating, IMovieFilter
assertThat(dataTypes).hasSize(5) // Movie, MoviePage, Genre, Rating, MovieFilter

val iMovie = interfaces[0]
Expand Down Expand Up @@ -3199,6 +3199,10 @@ class CodeGenTest {
assertThat(iRating.typeSpec.name).isEqualTo("IRating")
assertThat(iRating.typeSpec.methodSpecs).extracting("name").containsExactly("getName")

val iMovieFilter = interfaces[4]
assertThat(iMovieFilter.typeSpec.name).isEqualTo("IMovieFilter")
assertThat(iMovieFilter.typeSpec.methodSpecs).extracting("name").containsExactly("getTitle", "getGenre", "getLanguage", "getTags", "getRating")

val movie = dataTypes[0]
assertThat(movie.typeSpec.name).isEqualTo("Movie")
assertThat(movie.typeSpec.superinterfaces).extracting("simpleName").containsExactly("IMovie")
Expand Down Expand Up @@ -3233,7 +3237,7 @@ class CodeGenTest {

val movieFilter = dataTypes[4]
assertThat(movieFilter.typeSpec.name).isEqualTo("MovieFilter")
assertThat(movieFilter.typeSpec.superinterfaces.size).isEqualTo(0)
assertThat(movieFilter.typeSpec.superinterfaces.size).isEqualTo(1) // IMovieFilter
assertThat(movieFilter.typeSpec.fieldSpecs).extracting("name").containsExactly("title", "genre", "language", "tags", "rating")
assertThat(movieFilter.typeSpec.fieldSpecs[0].type).extracting("simpleName").isEqualTo("String")
assertThat(movieFilter.typeSpec.fieldSpecs[1].type).extracting("simpleName").isEqualTo("Genre")
Expand Down
Loading