Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Manually delegate to dictionary.
  • Loading branch information
apottere committed May 30, 2017
commit c8f651839d6dfff5b165e95dbfd10b261c48c26b
74 changes: 48 additions & 26 deletions src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import graphql.schema.GraphQLScalarType
/**
* @author Andrew Potter
*/
class SchemaParserBuilder constructor(private val dictionary: SchemaParserDictionary = SchemaParserDictionary()): SchemaParserDictionaryMethods by dictionary {
class SchemaParserBuilder constructor(private val dictionary: SchemaParserDictionary = SchemaParserDictionary()) {

private val schemaString = StringBuilder()
private val resolvers = mutableListOf<GraphQLResolver<*>>()
Expand Down Expand Up @@ -60,70 +60,92 @@ class SchemaParserBuilder constructor(private val dictionary: SchemaParserDictio
}

/**
* Build the parser with the supplied schema and dictionary.
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
*/
fun build(): SchemaParser {
val document = Parser().parseDocument(this.schemaString.toString())
val definitions = document.definitions

val resolvers = resolvers.map { Resolver(it) }
val customScalars = scalars.associateBy { it.name }

return SchemaClassScanner(dictionary.getDictionary(), definitions, resolvers, customScalars).scanForClasses()
fun dictionary(name: String, clazz: Class<*>) = this.apply {
this.dictionary.add(name, clazz)
}
}

interface SchemaParserDictionaryMethods {

/**
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
*/
fun add(name: String, clazz: Class<*>): SchemaParserDictionary
fun dictionary(dictionary: Map<String, Class<*>>) = this.apply {
this.dictionary.add(dictionary)
}

/**
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
* Add arbitrary classes to the parser's dictionary.
*/
fun add(dictionary: Map<String, Class<*>>): SchemaParserDictionary
fun dictionary(clazz: Class<*>) = this.apply {
this.dictionary.add(clazz)
}

/**
* Add arbitrary classes to the parser's dictionary.
*/
fun add(clazz: Class<*>): SchemaParserDictionary
fun dictionary(vararg dictionary: Class<*>) = this.apply {
this.dictionary.add(*dictionary)
}

/**
* Add arbitrary classes to the parser's dictionary.
*/
fun add(vararg dictionary: Class<*>): SchemaParserDictionary
fun dictionary(dictionary: List<Class<*>>) = this.apply {
this.dictionary.add(dictionary)
}

/**
* Add arbitrary classes to the parser's dictionary.
* Build the parser with the supplied schema and dictionary.
*/
fun add(dictionary: List<Class<*>>): SchemaParserDictionary
fun build(): SchemaParser {
val document = Parser().parseDocument(this.schemaString.toString())
val definitions = document.definitions

val resolvers = resolvers.map { Resolver(it) }
val customScalars = scalars.associateBy { it.name }

return SchemaClassScanner(dictionary.getDictionary(), definitions, resolvers, customScalars).scanForClasses()
}
}

class SchemaParserDictionary: SchemaParserDictionaryMethods {
class SchemaParserDictionary {

private val dictionary: BiMap<String, Class<*>> = HashBiMap.create()

fun getDictionary(): BiMap<String, Class<*>> = Maps.unmodifiableBiMap(dictionary)

override fun add(name: String, clazz: Class<*>) = this.apply {
/**
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
*/
fun add(name: String, clazz: Class<*>) = this.apply {
this.dictionary.put(name, clazz)
}

override fun add(dictionary: Map<String, Class<*>>) = this.apply {
/**
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
*/
fun add(dictionary: Map<String, Class<*>>) = this.apply {
this.dictionary.putAll(dictionary)
}

override fun add(clazz: Class<*>) = this.apply {
/**
* Add arbitrary classes to the parser's dictionary.
*/
fun add(clazz: Class<*>) = this.apply {
this.add(clazz.simpleName, clazz)
}

override fun add(vararg dictionary: Class<*>) = this.apply {
/**
* Add arbitrary classes to the parser's dictionary.
*/
fun add(vararg dictionary: Class<*>) = this.apply {
dictionary.forEach { this.add(it) }
}

override fun add(dictionary: List<Class<*>>) = this.apply {
/**
* Add arbitrary classes to the parser's dictionary.
*/
fun add(dictionary: List<Class<*>>) = this.apply {
dictionary.forEach { this.add(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ResolverDataFetcherSpec extends Specification {
}

private static ResolverDataFetcher createResolver(String methodName, List<InputValueDefinition> arguments = [], GraphQLResolver<?> resolver) {
ResolverDataFetcher.create(new Resolver(resolver).getMethod(new FieldDefinition(methodName).with { getInputValueDefinitions().addAll(arguments); it }))
ResolverDataFetcher.create(new Resolver(resolver).getMethod(new FieldDefinition(methodName, new TypeName('Boolean')).with { getInputValueDefinitions().addAll(arguments); it }))
}

private static DataFetchingEnvironment createEnvironment(Map<String, Object> arguments = [:]) {
Expand Down