Skip to content

Commit

Permalink
Fix serialization of default values for input object enums fix #276
Browse files Browse the repository at this point in the history
  • Loading branch information
oliemansm committed Jun 18, 2019
1 parent cf387b0 commit 6b33b1d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 72 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/com/coxautodev/graphql/tools/SchemaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import graphql.schema.idl.DirectiveBehavior
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.ScalarInfo
import graphql.schema.idl.SchemaGeneratorHelper
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.reflect.KClass

Expand All @@ -57,6 +58,7 @@ import kotlin.reflect.KClass
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, private val options: SchemaParserOptions, private val runtimeWiring: RuntimeWiring) {

companion object {
val log = LoggerFactory.getLogger(SchemaClassScanner::class.java)!!
const val DEFAULT_DEPRECATION_MESSAGE = "No longer supported"

@JvmStatic
Expand Down Expand Up @@ -201,7 +203,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
.name(inputDefinition.name)
.definition(inputDefinition)
.description(if (inputDefinition.description != null) inputDefinition.description.content else getDocumentation(inputDefinition))
.defaultValue(inputDefinition.defaultValue)
.defaultValue(buildDefaultValue(inputDefinition.defaultValue))
.type(determineInputType(inputDefinition.type))
.withDirectives(*buildDirectives(inputDefinition.directives, setOf(), Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
builder.field(fieldBuilder.build())
Expand All @@ -226,6 +228,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
val enumName = enumDefinition.name
val enumValue = type.unwrap().enumConstants.find { (it as Enum<*>).name == enumName }
?: throw SchemaError("Expected value for name '$enumName' in enum '${type.unwrap().simpleName}' but found none!")

val enumValueDirectives = buildDirectives(enumDefinition.directives, setOf(), Introspection.DirectiveLocation.ENUM_VALUE)
getDeprecated(enumDefinition.directives).let {
val enumValueDefinition = GraphQLEnumValueDefinition.newEnumValueDefinition()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.coxautodev.graphql.tools

import graphql.GraphQL
import graphql.execution.AsyncExecutionStrategy
import graphql.schema.GraphQLSchema
import spock.lang.Specification

class EnumDefaultValueSpec extends Specification {

def "enumvalue is not passed down to graphql-java"() {
when:
GraphQLSchema schema = SchemaParser.newParser().schemaString('''\
type Query {
test(input: MySortSpecifier): SortBy
}
input MySortSpecifier {
sortBy: SortBy = createdOn
value: Int = 10
}
enum SortBy {
createdOn
updatedOn
}
''').resolvers(new GraphQLQueryResolver() {

SortBy test(MySortSpecifier input) {
return input.sortBy
}

})
.build()
.makeExecutableSchema()
GraphQL gql = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new AsyncExecutionStrategy())
.build()
def data = Utils.assertNoGraphQlErrors(gql, [input: [value: 1]]) {
'''
query test($input: MySortSpecifier) {
test(input: $input)
}
'''
}

then:
noExceptionThrown()
data.test == 'createdOn'
}

static class MySortSpecifier {
SortBy sortBy
int value
}

enum SortBy {
createdOn,
updatedOn
}

}
Loading

0 comments on commit 6b33b1d

Please sign in to comment.