Skip to content

Commit 346e462

Browse files
smyrickShane Myrick
andauthored
[generator] Nullability additional input checks (#829)
* Add additional input test * Add required list check * Fix linter Co-authored-by: Shane Myrick <[email protected]>
1 parent ff2a70a commit 346e462

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/types/generateInputProperty.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.expediagroup.graphql.generator.types
1818

19-
import com.expediagroup.graphql.extensions.unwrapType
2019
import com.expediagroup.graphql.generator.SchemaGenerator
2120
import com.expediagroup.graphql.generator.extensions.getPropertyDescription
2221
import com.expediagroup.graphql.generator.extensions.getPropertyName
@@ -30,9 +29,7 @@ internal fun generateInputProperty(generator: SchemaGenerator, prop: KProperty<*
3029
val builder = GraphQLInputObjectField.newInputObjectField()
3130

3231
// Verfiy that the unwrapped GraphQL type is a valid input type
33-
val graphQLInputType = generateGraphQLType(generator = generator, type = prop.returnType, inputType = true)
34-
.unwrapType()
35-
.safeCast<GraphQLInputType>()
32+
val graphQLInputType = generateGraphQLType(generator = generator, type = prop.returnType, inputType = true).safeCast<GraphQLInputType>()
3633

3734
builder.description(prop.getPropertyDescription(parentClass))
3835
builder.name(prop.getPropertyName(parentClass))
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.test.integration
18+
19+
import com.expediagroup.graphql.TopLevelObject
20+
import com.expediagroup.graphql.extensions.print
21+
import com.expediagroup.graphql.generator.SchemaGenerator
22+
import com.expediagroup.graphql.testSchemaConfig
23+
import org.junit.jupiter.api.Test
24+
import kotlin.reflect.full.createType
25+
import kotlin.test.assertEquals
26+
27+
class OptionalInputSchemaTest {
28+
29+
@Test
30+
fun `SchemaGenerator generates a simple GraphQL schema`() {
31+
val generator = SchemaGenerator(testSchemaConfig)
32+
val schema = generator.generateSchema(
33+
queries = listOf(TopLevelObject(Query())),
34+
additionalInputTypes = setOf(MyAdditionalInput::class.createType())
35+
)
36+
val sdl =
37+
"""
38+
schema {
39+
query: Query
40+
}
41+
42+
type Query {
43+
printList(input: [String]!): [String]!
44+
printMessage(input: String): String
45+
printOptionalList(input: [String]): [String]
46+
printType(input: MyObjectInput): String!
47+
printTypeList(input: [MyObjectInput]): String!
48+
}
49+
50+
input MyAdditionalInput {
51+
listOptional: [String]
52+
listRequired: [String!]!
53+
optional: String
54+
required: String!
55+
}
56+
57+
input MyObjectInput {
58+
id: String!
59+
value: String
60+
}
61+
62+
""".trimIndent()
63+
64+
assertEquals(sdl, schema.print(includeDirectiveDefinitions = false))
65+
}
66+
67+
class Query {
68+
69+
fun printMessage(input: String?) = input
70+
fun printList(input: List<String?>) = input
71+
fun printOptionalList(input: List<String?>?) = input
72+
fun printType(input: MyObject?) = input.toString()
73+
fun printTypeList(input: List<MyObject?>?) = input.toString()
74+
}
75+
76+
data class MyObject(
77+
val id: String,
78+
val value: String?
79+
)
80+
81+
data class MyAdditionalInput(
82+
val required: String,
83+
val optional: String?,
84+
val listOptional: List<String?>?,
85+
val listRequired: List<String>
86+
)
87+
}

0 commit comments

Comments
 (0)