Skip to content
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

Add support for unused types for all dictionary types #737

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@ internal class SchemaClassScanner(
do {
val unusedDefinitions = (definitionsByName.values - (dictionary.keys.toSet() + unvalidatedTypes))
.filter { definition -> definition.name != "PageInfo" }
.filterIsInstance<ObjectTypeDefinition>().distinct()
.filter { isCompositeOrEnumType(it) }
.distinct()

if (unusedDefinitions.isEmpty()) {
break
}

val unusedDefinition = unusedDefinitions.first()

handleDictionaryTypes(listOf(unusedDefinition)) { "Object type '${it.name}' is unused and includeUnusedTypes is true. Please pass a class for type '${it.name}' in the parser's dictionary." }
handleDictionaryTypes(unusedDefinitions) { "Type '${it.name}' is unused and includeUnusedTypes is true. Please pass a class for type '${it.name}' in the parser's dictionary." }
} while (scanQueue())
}

return validateAndCreateResult(rootTypeHolder)
}

private fun isCompositeOrEnumType(definition: TypeDefinition<*>): Boolean {
return definition is ObjectTypeDefinition || definition is InterfaceTypeDefinition || definition is UnionTypeDefinition || definition is EnumTypeDefinition
}

private fun scanQueue(): Boolean {
if (queue.isEmpty()) {
return false
Expand Down Expand Up @@ -229,7 +232,7 @@ internal class SchemaClassScanner(
}.flatten().distinct()
}

private fun handleDictionaryTypes(types: List<ObjectTypeDefinition>, failureMessage: (ObjectTypeDefinition) -> String) {
private fun handleDictionaryTypes(types: List<TypeDefinition<*>>, failureMessage: (TypeDefinition<*>) -> String) {
types.forEach { type ->
val dictionaryContainsType = dictionary.filter { it.key.name == type.name }.isNotEmpty()
if (!unvalidatedTypes.contains(type) && !dictionaryContainsType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,19 @@ class SchemaClassScannerTest {
type Implementation implements SomeInterface {
value: String
}

union SomeUnion = Unused | Implementation

enum SomeEnum {
A
B
}
""")
.resolvers(object : GraphQLQueryResolver {
fun whatever(): Whatever? = null
})
.options(SchemaParserOptions.newOptions().includeUnusedTypes(true).build())
.dictionary(Unused::class, Implementation::class)
.dictionary(Unused::class, Implementation::class, SomeInterface::class, SomeUnion::class, SomeEnum::class)
.build()
.makeExecutableSchema()

Expand Down
7 changes: 7 additions & 0 deletions src/test/kotlin/graphql/kickstart/tools/TestInterfaces.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ interface SomeInterface {
fun getValue(): String?
}

interface SomeUnion

enum class SomeEnum {
A,
B
}