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

feature: add support for object input default values and add support for default inputs to kotlin2 #680

Merged
merged 5 commits into from
Jun 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class PersonFilter @JsonCreator constructor(
@JsonProperty("email")
public val email: String? = default<PersonFilter, String?>("email"),
public val email: String? = default<PersonFilter, String?>("email", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("email" to email)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import kotlin.collections.List

public class PersonFilter @JsonCreator constructor(
@JsonProperty("email")
public val email: String? = default<PersonFilter, String?>("email"),
public val email: String? = default<PersonFilter, String?>("email", null),
@JsonProperty("birthYear")
public val birthYear: Int? = default<PersonFilter, Int?>("birthYear"),
public val birthYear: Int? = default<PersonFilter, Int?>("birthYear", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("email" to email, "birthYear" to
birthYear)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.collections.List
*/
public class MovieFilter @JsonCreator constructor(
@JsonProperty("titleFilter")
public val titleFilter: String? = default<MovieFilter, String?>("titleFilter"),
public val titleFilter: String? = default<MovieFilter, String?>("titleFilter", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("titleFilter" to titleFilter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class MovieFilter @JsonCreator constructor(
@JsonProperty("titleFilter")
public val titleFilter: String? = default<MovieFilter, String?>("titleFilter"),
public val titleFilter: String? = default<MovieFilter, String?>("titleFilter", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("titleFilter" to titleFilter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class MovieFilter @JsonCreator constructor(
@JsonProperty("genre")
public val genre: String? = default<MovieFilter, String?>("genre"),
public val genre: String? = default<MovieFilter, String?>("genre", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("genre" to genre)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import kotlin.collections.List

public class SomeType @JsonCreator constructor(
@JsonProperty("colors")
public val colors: List<Color?>? = default<SomeType, List<Color?>?>("colors"),
public val colors: List<Color?>? = default<SomeType, List<Color?>?>("colors",
listOf(com.netflix.graphql.dgs.codegen.cases.inputWithDefaultEnumValueForArray.expected.types.Color.red)),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("colors" to colors)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.collections.List

public class SomeType @JsonCreator constructor(
@JsonProperty("numbers")
public val numbers: List<Int?>? = default<SomeType, List<Int?>?>("numbers"),
public val numbers: List<Int?>? = default<SomeType, List<Int?>?>("numbers", listOf(1, 2, 3)),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("numbers" to numbers)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class SomeType @JsonCreator constructor(
@JsonProperty("names")
public val names: List<String?>? = default<SomeType, List<String?>?>("names"),
public val names: List<String?>? = default<SomeType, List<String?>?>("names", listOf("A", "B")),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("names" to names)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class SomeType @JsonCreator constructor(
@JsonProperty("names")
public val names: List<String?>? = default<SomeType, List<String?>?>("names"),
public val names: List<String?>? = default<SomeType, List<String?>?>("names", emptyList()),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("names" to names)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import kotlin.collections.List

public class ColorFilter @JsonCreator constructor(
@JsonProperty("color")
public val color: Color? = default<ColorFilter, Color?>("color"),
public val color: Color? = default<ColorFilter, Color?>("color",
com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForEnum.expected.types.Color.red),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("color" to color)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected

public object DgsClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected

import kotlin.String

public object DgsConstants {
public object PERSON {
public const val TYPE_NAME: String = "Person"

public const val Name: String = "name"

public const val Age: String = "age"

public const val Car: String = "car"

public const val Hobbies: String = "hobbies"

public const val IsHappy: String = "isHappy"
}

public object CAR {
public const val TYPE_NAME: String = "Car"

public const val Brand: String = "brand"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected.types

import com.fasterxml.jackson.`annotation`.JsonCreator
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.netflix.graphql.dgs.codegen.GraphQLInput
import kotlin.Any
import kotlin.Pair
import kotlin.String
import kotlin.collections.List

public class Car @JsonCreator constructor(
@JsonProperty("brand")
public val brand: String = default<Car, String>("brand", "BMW"),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("brand" to brand)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected.types

public enum class Hobby {
Football,
Hokey,
;

public companion object
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected.types

import com.fasterxml.jackson.`annotation`.JsonCreator
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.netflix.graphql.dgs.codegen.GraphQLInput
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.Pair
import kotlin.String
import kotlin.collections.List

public class Person @JsonCreator constructor(
@JsonProperty("name")
public val name: String = default<Person, String>("name", "Damian"),
@JsonProperty("age")
public val age: Int = default<Person, Int>("age", 18),
@JsonProperty("car")
public val car: Car = default<Person, Car>("car",
com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected.types.Car(brand
= "Ford")),
@JsonProperty("hobbies")
public val hobbies: List<Hobby> = default<Person, List<Hobby>>("hobbies",
listOf(com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForNonNullableFields.expected.types.Hobby.Hokey)),
@JsonProperty("isHappy")
public val isHappy: Boolean = default<Person, Boolean>("isHappy", true),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("name" to name, "age" to age, "car" to
car, "hobbies" to hobbies, "isHappy" to isHappy)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
input Person {
name: String! = "Damian"
age: Int! = 18
car: Car! = { brand: "Ford" }
hobbies: [Hobby!]! = [Hokey]
isHappy: Boolean! = true
}

enum Hobby {
Football
Hokey
}

input Car {
brand: String! = "BMW"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected

public object DgsClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected

import kotlin.String

public object DgsConstants {
public object PERSON {
public const val TYPE_NAME: String = "Person"

public const val Name: String = "name"

public const val Age: String = "age"

public const val Car: String = "car"
}

public object CAR {
public const val TYPE_NAME: String = "Car"

public const val Brand: String = "brand"
}

public object MOVIEFILTER {
public const val TYPE_NAME: String = "MovieFilter"

public const val Director: String = "director"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types

import com.fasterxml.jackson.`annotation`.JsonCreator
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.netflix.graphql.dgs.codegen.GraphQLInput
import kotlin.Any
import kotlin.Pair
import kotlin.String
import kotlin.collections.List

public class Car @JsonCreator constructor(
@JsonProperty("brand")
public val brand: String = default<Car, String>("brand", "BMW"),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("brand" to brand)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types

import com.fasterxml.jackson.`annotation`.JsonCreator
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.netflix.graphql.dgs.codegen.GraphQLInput
import kotlin.Any
import kotlin.Pair
import kotlin.String
import kotlin.collections.List

public class MovieFilter @JsonCreator constructor(
@JsonProperty("director")
public val director: Person? = default<MovieFilter, Person?>("director",
com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types.Person(name
= "Damian", car =
com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types.Car(brand
= "Tesla"))),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("director" to director)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types

import com.fasterxml.jackson.`annotation`.JsonCreator
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.netflix.graphql.dgs.codegen.GraphQLInput
import kotlin.Any
import kotlin.Int
import kotlin.Pair
import kotlin.String
import kotlin.collections.List

public class Person @JsonCreator constructor(
@JsonProperty("name")
public val name: String? = default<Person, String?>("name", "John"),
@JsonProperty("age")
public val age: Int? = default<Person, Int?>("age", 23),
@JsonProperty("car")
public val car: Car? = default<Person, Car?>("car",
com.netflix.graphql.dgs.codegen.cases.inputWithDefaultValueForObject.expected.types.Car(brand
= "Ford")),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("name" to name, "age" to age, "car" to
car)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
input Person {
name: String = "John"
age: Int = 23
car: Car = { brand: "Ford" }
}

input Car {
brand: String! = "BMW"
}

input MovieFilter {
director: Person = { name: "Damian", car: { brand: "Tesla" } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import kotlin.collections.List

public class MovieFilter @JsonCreator constructor(
@JsonProperty("genre")
public val genre: String? = default<MovieFilter, String?>("genre"),
public val genre: String? = default<MovieFilter, String?>("genre", null),
@JsonProperty("releaseYear")
public val releaseYear: Int? = default<MovieFilter, Int?>("releaseYear"),
public val releaseYear: Int? = default<MovieFilter, Int?>("releaseYear", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("genre" to genre, "releaseYear" to
releaseYear)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import kotlin.collections.List

public class I1 @JsonCreator constructor(
@JsonProperty("arg1")
public val arg1: I1? = default<I1, I1?>("arg1"),
public val arg1: I1? = default<I1, I1?>("arg1", null),
@JsonProperty("arg2")
public val arg2: I2? = default<I1, I2?>("arg2"),
public val arg2: I2? = default<I1, I2?>("arg2", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("arg1" to arg1, "arg2" to arg2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import kotlin.collections.List

public class I2 @JsonCreator constructor(
@JsonProperty("arg1")
public val arg1: String? = default<I2, String?>("arg1"),
public val arg1: String? = default<I2, String?>("arg1", null),
@JsonProperty("arg2")
public val arg2: String? = default<I2, String?>("arg2"),
public val arg2: String? = default<I2, String?>("arg2", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("arg1" to arg1, "arg2" to arg2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class I @JsonCreator constructor(
@JsonProperty("arg")
public val arg: String? = default<I, String?>("arg"),
public val arg: String? = default<I, String?>("arg", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("arg" to arg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.collections.List

public class I @JsonCreator constructor(
@JsonProperty("arg")
public val arg: String? = default<I, String?>("arg"),
public val arg: String? = default<I, String?>("arg", null),
) : GraphQLInput() {
override fun fields(): List<Pair<String, Any?>> = listOf("arg" to arg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,15 @@ class CodeGen(private val config: CodeGenConfig) {
}

private fun generateJavaInputType(definitions: Collection<Definition<*>>): CodeGenResult {
val inputTypes = definitions.asSequence()
val inputTypeDefinitions = definitions
.filterIsInstance<InputObjectTypeDefinition>()
val inputTypes = inputTypeDefinitions.asSequence()
.excludeSchemaTypeExtension()
.filter { config.generateDataTypes || it.name in requiredTypeCollector.requiredTypes }

return inputTypes
.map { d ->
InputTypeGenerator(config, document).generate(d, findInputExtensions(d.name, definitions))
InputTypeGenerator(config, document).generate(d, findInputExtensions(d.name, definitions), inputTypeDefinitions)
}.fold(CodeGenResult()) { t: CodeGenResult, u: CodeGenResult -> t.merge(u) }
}

Expand Down Expand Up @@ -411,12 +412,13 @@ class CodeGen(private val config: CodeGenConfig) {
}

private fun generateKotlinInputTypes(definitions: Collection<Definition<*>>): CodeGenResult {
return definitions.asSequence()
val inputTypeDefinitions = definitions
.filterIsInstance<InputObjectTypeDefinition>()
return inputTypeDefinitions.asSequence()
.excludeSchemaTypeExtension()
.filter { config.generateDataTypes || it.name in requiredTypeCollector.requiredTypes }
.map {
KotlinInputTypeGenerator(config, document).generate(it, findInputExtensions(it.name, definitions))
KotlinInputTypeGenerator(config, document).generate(it, findInputExtensions(it.name, definitions), inputTypeDefinitions)
}
.fold(CodeGenResult()) { t: CodeGenResult, u: CodeGenResult -> t.merge(u) }
}
Expand Down
Loading
Loading