Skip to content

Allowing nested inputs defined in extensions to be parsed as well #480

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 2 commits into from
Feb 16, 2021
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
37 changes: 20 additions & 17 deletions src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,29 @@ internal class SchemaClassScanner(
}

is InputObjectTypeDefinition -> {
graphQLType.inputValueDefinitions.forEach { inputValueDefinition ->
val inputGraphQLType = inputValueDefinition.type.unwrap()
if (inputGraphQLType is TypeName && !ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(inputGraphQLType.name)) {
val inputValueJavaType = findInputValueType(inputValueDefinition.name, inputGraphQLType, javaType.unwrap())
if (inputValueJavaType != null) {
handleFoundType(typeClassMatcher.match(TypeClassMatcher.PotentialMatch.parameterType(
inputValueDefinition.type,
inputValueJavaType,
GenericType(javaType, options).relativeToType(inputValueJavaType),
InputObjectReference(inputValueDefinition)
)))
} else {
var mappingAdvice = "Try adding it manually to the dictionary"
if (javaType.unwrap().name.contains("Map")) {
mappingAdvice = " or add a class to represent your input type instead of a Map."
val inputObjectTypes = listOf(graphQLType) + inputExtensionDefinitions.filter { it.name == graphQLType.name }
inputObjectTypes
.flatMap { it.inputValueDefinitions }
.forEach { inputValueDefinition ->
val inputGraphQLType = inputValueDefinition.type.unwrap()
if (inputGraphQLType is TypeName && !ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(inputGraphQLType.name)) {
val inputValueJavaType = findInputValueType(inputValueDefinition.name, inputGraphQLType, javaType.unwrap())
if (inputValueJavaType != null) {
handleFoundType(typeClassMatcher.match(TypeClassMatcher.PotentialMatch.parameterType(
inputValueDefinition.type,
inputValueJavaType,
GenericType(javaType, options).relativeToType(inputValueJavaType),
InputObjectReference(inputValueDefinition)
)))
} else {
var mappingAdvice = "Try adding it manually to the dictionary"
if (javaType.unwrap().name.contains("Map")) {
mappingAdvice = " or add a class to represent your input type instead of a Map."
}
log.warn("Cannot find definition for field '${inputValueDefinition.name}: ${inputGraphQLType.name}' on input type '${graphQLType.name}' -> ${javaType.unwrap().name}. $mappingAdvice")
}
log.warn("Cannot find definition for field '${inputValueDefinition.name}: ${inputGraphQLType.name}' on input type '${graphQLType.name}' -> ${javaType.unwrap().name}. $mappingAdvice")
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,55 @@ class NestedInputTypesSpec extends Specification {
data.materials == []
}

def "nested input in extensions are parsed"() {
when:
GraphQLSchema schema = SchemaParser.newParser().schemaString('''\
type Query {
materials(filter: MaterialFilter): [Material!]!
}

input MaterialFilter {
title: String
}

extend input MaterialFilter {
requestFilter: RequestFilter
}

input RequestFilter {
and: [RequestFilter!]
or: [RequestFilter!]
discountTypeFilter: DiscountTypeFilter
}

input DiscountTypeFilter {
name: String
}

type Material {
id: ID!
}
''').resolvers(new QueryResolver())
.build()
.makeExecutableSchema()
GraphQL gql = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new AsyncExecutionStrategy())
.build()
def data = Utils.assertNoGraphQlErrors(gql, [filter: [title: "title", requestFilter: [discountTypeFilter: [name: "discount"]]]]) {
'''
query materials($filter: MaterialFilter!) {
materials(filter: $filter) {
id
}
}
'''
}

then:
noExceptionThrown()
data.materials == []
}

class QueryResolver implements GraphQLQueryResolver {
List<Material> materials(MaterialFilter filter) { Collections.emptyList() }
}
Expand Down