Skip to content

Commit

Permalink
Clean up augmented schema to contain only types used in the resulting…
Browse files Browse the repository at this point in the history
… schema (#234)

resolves #233
  • Loading branch information
Andy2003 authored Jun 11, 2021
1 parent fc8f780 commit 5ddd00e
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 1,266 deletions.
25 changes: 23 additions & 2 deletions core/src/main/kotlin/org/neo4j/graphql/SchemaBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,29 @@ class SchemaBuilder(
.filter { it.name == queryTypeName || it.name == mutationTypeName || it.name == subscriptionTypeName }
.forEach { type -> handler.forEach { h -> h.augmentType(type) } }

// TODO copy over only the types used in the source schema
typeDefinitionRegistry.merge(neo4jTypeDefinitionRegistry)
val types = mutableListOf<Type<*>>()
neo4jTypeDefinitionRegistry.directiveDefinitions.values
.filterNot { typeDefinitionRegistry.getDirectiveDefinition(it.name).isPresent }
.forEach { directiveDefinition ->
typeDefinitionRegistry.add(directiveDefinition)
directiveDefinition.inputValueDefinitions.forEach { types.add(it.type) }
}
typeDefinitionRegistry.types()
.values
.flatMap { typeDefinition ->
when (typeDefinition) {
is ImplementingTypeDefinition -> typeDefinition.fieldDefinitions
.flatMap { fieldDefinition -> fieldDefinition.inputValueDefinitions.map { it.type } + fieldDefinition.type }
is InputObjectTypeDefinition -> typeDefinition.inputValueDefinitions.map { it.type }
else -> emptyList()
}
}
.forEach { types.add(it) }
types
.map { TypeName(it.name()) }
.filterNot { typeDefinitionRegistry.hasType(it) }
.mapNotNull { neo4jTypeDefinitionRegistry.getType(it).unwrap() }
.forEach { typeDefinitionRegistry.add(it) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class GraphQLSchemaTestSuite(fileName: String) : AsciiDocTestSuite(

diff(expectedSchema, augmentedSchema)
diff(augmentedSchema, expectedSchema)
targetSchemaBlock.adjustedCode = SCHEMA_PRINTER.print(augmentedSchema)
} catch (e: Throwable) {
if (ignore) {
Assumptions.assumeFalse(true, e.message)
Expand All @@ -65,9 +66,7 @@ class GraphQLSchemaTestSuite(fileName: String) : AsciiDocTestSuite(
Assertions.fail<Throwable>(e)
}
val actualSchema = SCHEMA_PRINTER.print(augmentedSchema)
targetSchemaBlock.adjustedCode = actualSchema + "\n" +
// this is added since the SCHEMA_PRINTER is not able to print global directives
javaClass.getResource("/lib_directives.graphql").readText()
targetSchemaBlock.adjustedCode = actualSchema
throw AssertionFailedError("augmented schema differs for '$title'",
expectedSchema?.let { SCHEMA_PRINTER.print(it) } ?: targetSchema,
actualSchema,
Expand All @@ -84,7 +83,7 @@ class GraphQLSchemaTestSuite(fileName: String) : AsciiDocTestSuite(
private val METHOD_PATTERN = Pattern.compile("(add|delete|update|merge|create)(.*)")

private val SCHEMA_PRINTER = SchemaPrinter(SchemaPrinter.Options.defaultOptions()
.includeDirectives(true)
.includeDirectives(false)
.includeScalarTypes(true)
.includeSchemaDefinition(true)
.includeIntrospectionTypes(false)
Expand Down
Loading

0 comments on commit 5ddd00e

Please sign in to comment.