Skip to content

feat: new hook for modifying object types #232

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 1 commit into from
Jun 13, 2019
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
17 changes: 11 additions & 6 deletions src/main/kotlin/com/expedia/graphql/generator/TypeBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ internal open class TypeBuilder constructor(protected val generator: SchemaGener
val graphQLType = hookGraphQLType
?: generator.scalarType(type, annotatedAsID)
?: objectFromReflection(type, inputType)
val typeWithNullityTakenIntoAccount = graphQLType.wrapInNonNull(type)
config.hooks.didGenerateGraphQLType(type, typeWithNullityTakenIntoAccount)
return typeWithNullityTakenIntoAccount

val typeWithNullability = graphQLType.wrapInNonNull(type)

config.hooks.didGenerateGraphQLType(type, typeWithNullability)

return typeWithNullability
}

internal fun objectFromReflection(type: KType, inputType: Boolean): GraphQLType {
Expand All @@ -39,12 +42,14 @@ internal open class TypeBuilder constructor(protected val generator: SchemaGener
val kClass = type.getKClass()
val graphQLType = getGraphQLType(kClass, inputType, type)

if (graphQLType !is GraphQLTypeReference) {
val kGraphQLType = KGraphQLType(kClass, graphQLType)
val modifiedGraphQLType = config.hooks.willAddGraphQLTypeToSchema(type, graphQLType)

if (modifiedGraphQLType !is GraphQLTypeReference) {
val kGraphQLType = KGraphQLType(kClass, modifiedGraphQLType)
state.cache.put(cacheKey, kGraphQLType)
}

return graphQLType
return modifiedGraphQLType
}

private fun getGraphQLType(kClass: KClass<*>, inputType: Boolean, type: KType): GraphQLType = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ interface SchemaGeneratorHooks {
@Suppress("Detekt.FunctionOnlyReturningConstant")
fun willGenerateGraphQLType(type: KType): GraphQLType? = null

/**
* Called after using reflection to generate the graphql object type but before returning it to the schema builder.
* This allows for modifying the type info, like description or directives
*/
fun willAddGraphQLTypeToSchema(type: KType, generatedType: GraphQLType): GraphQLType = generatedType

/**
* Called before resolving a KType to the GraphQL type.
* This allows for a custom resolver on how to extract wrapped values, like in a CompletableFuture.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ class SchemaGeneratorHooksTest {
assertEquals("SomeData!", hooks.lastSeenGeneratedType?.deepName)
}

@Test
fun `calls hook before adding type to schema`() {
class MockSchemaGeneratorHooks : SchemaGeneratorHooks {
var hookCalled = false

override fun willAddGraphQLTypeToSchema(type: KType, generatedType: GraphQLType): GraphQLType {
hookCalled = true
return when {
generatedType is GraphQLObjectType && generatedType.name == "SomeData" -> GraphQLObjectType.Builder(generatedType).description("My custom description").build()
else -> generatedType
}
}
}

val hooks = MockSchemaGeneratorHooks()
val schema = toSchema(
queries = listOf(TopLevelObject(TestQuery())),
config = getTestSchemaConfigWithHooks(hooks)
)
assertTrue(hooks.hookCalled)

val type = schema.getObjectType("SomeData")
assertNotNull(type)
assertEquals(expected = "My custom description", actual = type.description)
}

@Test
fun `calls hook before adding query to schema`() {
class MockSchemaGeneratorHooks : SchemaGeneratorHooks {
Expand Down