Skip to content

Allow updating custom additional types #593

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

Merged
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 @@ -82,5 +82,10 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
}
}

private fun generateAdditionalTypes(additionalTypes: Set<KType>): Set<GraphQLType> = additionalTypes.map { generateGraphQLType(this, it) }.toSet()
/**
* Generate the GraphQL type for all the `additionalTypes`. They are generated as non-inputs and not as IDs.
* If you need to provide more custom additional types that were not picked up from reflection of the schema objects,
* you can modify the set of `additionalTypes` before you call this method.
*/
protected open fun generateAdditionalTypes(additionalTypes: Set<KType>): Set<GraphQLType> = additionalTypes.map { generateGraphQLType(this, it) }.toSet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package com.expediagroup.graphql.generator

import com.expediagroup.graphql.SchemaGeneratorConfig
import com.expediagroup.graphql.extensions.deepName
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.createType
import kotlin.test.assertEquals

class SchemaGeneratorTest {
Expand All @@ -38,8 +41,22 @@ class SchemaGeneratorTest {
assertEquals(1, generator.additionalTypes.size)
}

@Test
fun generateAdditionalTypes() {
val config = SchemaGeneratorConfig(listOf("com.expediagroup.graphql.generator"))
val generator = CustomSchemaGenerator(config)
val types = setOf(SomeObjectWithAnnotaiton::class.createType())

val result = generator.generateCustomAdditionalTypes(types)

assertEquals(1, result.size)
assertEquals("SomeObjectWithAnnotaiton!", result.first().deepName)
}

class CustomSchemaGenerator(config: SchemaGeneratorConfig) : SchemaGenerator(config) {
internal fun addTypes(annotation: KClass<*>) = addAdditionalTypesWithAnnotation(annotation)

internal fun generateCustomAdditionalTypes(types: Set<KType>) = generateAdditionalTypes(types)
}

annotation class MyCustomAnnotation
Expand Down