Skip to content

Commit 4a70469

Browse files
de55smyrick
authored andcommitted
fix non nullable nested object marked as nullable (#368)
* fix non nullable nested object marked as nullable * update federated schema nested test expected result * fix lint issues * return same type when GraphqlNonNull passed to wrapInNonNull
1 parent 03a881e commit 4a70469

File tree

6 files changed

+20
-39
lines changed

6 files changed

+20
-39
lines changed

graphql-kotlin-federation/src/test/kotlin/com/expediagroup/graphql/federation/FederatedSchemaGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class FederatedSchemaGeneratorTest {
157157
type SelfReferenceObject {
158158
description: String
159159
id: Int!
160-
nextObject: [SelfReferenceObject]
160+
nextObject: [SelfReferenceObject]!
161161
}
162162
163163
type _Service {

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/exceptions/NestingNonNullTypeException.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ internal open class TypeBuilder constructor(protected val generator: SchemaGener
4747

4848
// Do not call the hook on GraphQLTypeReference as we have not generated the type yet
4949
val unwrappedType = GraphQLTypeUtil.unwrapType(graphQLType).lastElement()
50+
val typeWithNullability = graphQLType.wrapInNonNull(type)
5051
if (unwrappedType !is GraphQLTypeReference) {
51-
val typeWithNullability = graphQLType.wrapInNonNull(type)
5252
return config.hooks.didGenerateGraphQLType(type, typeWithNullability)
5353
}
5454

55-
return graphQLType
55+
return typeWithNullability
5656
}
5757

5858
private fun objectFromReflection(type: KType, inputType: Boolean): GraphQLType {

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/extensions/graphQLExtensions.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.expediagroup.graphql.generator.extensions
1818

1919
import com.expediagroup.graphql.exceptions.CouldNotCastGraphQLType
20-
import com.expediagroup.graphql.exceptions.NestingNonNullTypeException
2120
import graphql.schema.GraphQLDirective
2221
import graphql.schema.GraphQLDirectiveContainer
2322
import graphql.schema.GraphQLFieldDefinition
@@ -27,11 +26,10 @@ import kotlin.reflect.KType
2726

2827
/**
2928
* Map null and non-null types.
30-
* Throws an exception on wrapping a non-null graphql type twice.
29+
* Returns same type when wrapping a non-null graphql type twice.
3130
*/
32-
@Throws(NestingNonNullTypeException::class)
3331
internal fun GraphQLType.wrapInNonNull(type: KType): GraphQLType = when {
34-
this is GraphQLNonNull -> throw NestingNonNullTypeException(this, type)
32+
this is GraphQLNonNull -> this
3533
type.isMarkedNullable -> this
3634
else -> GraphQLNonNull.nonNull(this)
3735
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/extensions/GraphQLExtensionsTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.expediagroup.graphql.generator.extensions
1818

1919
import com.expediagroup.graphql.exceptions.CouldNotCastGraphQLType
20-
import com.expediagroup.graphql.exceptions.NestingNonNullTypeException
2120
import graphql.schema.GraphQLArgument
2221
import graphql.schema.GraphQLDirective
2322
import graphql.schema.GraphQLDirectiveContainer
@@ -52,13 +51,10 @@ internal class GraphQLExtensionsTest {
5251
private val mockDirective: GraphQLDirective = mockk()
5352

5453
@Test
55-
fun `wrapInNonNull twice throws exception`() {
54+
fun `wrapInNonNull twice returns object`() {
5655
val nonNull = GraphQLNonNull(basicType)
5756
val mockKType: KType = mockk()
58-
59-
assertFailsWith(NestingNonNullTypeException::class) {
60-
nonNull.wrapInNonNull(mockKType)
61-
}
57+
assertEquals(nonNull, nonNull.wrapInNonNull(mockKType))
6258
}
6359

6460
@Test

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/types/FunctionBuilderTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ internal class FunctionBuilderTest : TypeTestHelper() {
5454

5555
internal interface MyInterface {
5656
fun printMessage(message: String): String
57+
fun nestedReturnType(): MyImplementation
5758
}
5859

5960
internal class MyImplementation : MyInterface {
6061
override fun printMessage(message: String): String = "message=$message"
62+
override fun nestedReturnType(): MyImplementation = MyImplementation()
6163
}
6264

6365
@GraphQLDirective(locations = [Introspection.DirectiveLocation.FIELD_DEFINITION])
@@ -282,4 +284,15 @@ internal class FunctionBuilderTest : TypeTestHelper() {
282284
val stringType = GraphQLTypeUtil.unwrapNonNull(result.type)
283285
assertEquals("String", stringType.name)
284286
}
287+
288+
@Test
289+
fun `Nested Self referencing object returns non null`() {
290+
val kInterfaceFunction = MyInterface::nestedReturnType
291+
val kInterfaceResult = builder.function(fn = kInterfaceFunction, parentName = "Query", target = null, abstract = false)
292+
val kImplFunction = MyImplementation::nestedReturnType
293+
val implResult = builder.function(fn = kImplFunction, parentName = "Query", target = null, abstract = false)
294+
295+
assertTrue(implResult.type is GraphQLNonNull)
296+
assertEquals(kInterfaceResult.type, implResult.type)
297+
}
285298
}

0 commit comments

Comments
 (0)