Skip to content

Commit 6444816

Browse files
authored
Allow updating custom additional types (ExpediaGroup#593)
* Allow updating custom additional types As dicussed in other PRs ExpediaGroup#587 and ExpediaGroup#585 we can allow developers to have more customization of the schema by allowing them to add additional types of their own without having to add it to the schema directly. What they do with this feature will be up to them, but from a libray perspective it is not bad feature to support. * Make function open
1 parent 8393731 commit 6444816

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
8282
}
8383
}
8484

85-
private fun generateAdditionalTypes(additionalTypes: Set<KType>): Set<GraphQLType> = additionalTypes.map { generateGraphQLType(this, it) }.toSet()
85+
/**
86+
* Generate the GraphQL type for all the `additionalTypes`. They are generated as non-inputs and not as IDs.
87+
* If you need to provide more custom additional types that were not picked up from reflection of the schema objects,
88+
* you can modify the set of `additionalTypes` before you call this method.
89+
*/
90+
protected open fun generateAdditionalTypes(additionalTypes: Set<KType>): Set<GraphQLType> = additionalTypes.map { generateGraphQLType(this, it) }.toSet()
8691
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package com.expediagroup.graphql.generator
1818

1919
import com.expediagroup.graphql.SchemaGeneratorConfig
20+
import com.expediagroup.graphql.extensions.deepName
2021
import org.junit.jupiter.api.Test
2122
import kotlin.reflect.KClass
23+
import kotlin.reflect.KType
24+
import kotlin.reflect.full.createType
2225
import kotlin.test.assertEquals
2326

2427
class SchemaGeneratorTest {
@@ -38,8 +41,22 @@ class SchemaGeneratorTest {
3841
assertEquals(1, generator.additionalTypes.size)
3942
}
4043

44+
@Test
45+
fun generateAdditionalTypes() {
46+
val config = SchemaGeneratorConfig(listOf("com.expediagroup.graphql.generator"))
47+
val generator = CustomSchemaGenerator(config)
48+
val types = setOf(SomeObjectWithAnnotaiton::class.createType())
49+
50+
val result = generator.generateCustomAdditionalTypes(types)
51+
52+
assertEquals(1, result.size)
53+
assertEquals("SomeObjectWithAnnotaiton!", result.first().deepName)
54+
}
55+
4156
class CustomSchemaGenerator(config: SchemaGeneratorConfig) : SchemaGenerator(config) {
4257
internal fun addTypes(annotation: KClass<*>) = addAdditionalTypesWithAnnotation(annotation)
58+
59+
internal fun generateCustomAdditionalTypes(types: Set<KType>) = generateAdditionalTypes(types)
4360
}
4461

4562
annotation class MyCustomAnnotation

0 commit comments

Comments
 (0)