Skip to content

Commit e052d89

Browse files
committed
Add warning about using a Map as input object instead of specific class
1 parent b457c12 commit e052d89

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/SchemaClassScanner.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
8787
do {
8888
do {
8989
// Require all implementors of discovered interfaces to be discovered or provided.
90-
handleInterfaceOrUnionSubTypes(getAllObjectTypesImplementingDiscoveredInterfaces(), { "Object type '${it.name}' implements a known interface, but no class could be found for that type name. Please pass a class for type '${it.name}' in the parser's dictionary." })
90+
handleInterfaceOrUnionSubTypes(getAllObjectTypesImplementingDiscoveredInterfaces()) { "Object type '${it.name}' implements a known interface, but no class could be found for that type name. Please pass a class for type '${it.name}' in the parser's dictionary." }
9191
} while (scanQueue())
9292

9393
// Require all members of discovered unions to be discovered.
94-
handleInterfaceOrUnionSubTypes(getAllObjectTypeMembersOfDiscoveredUnions(), { "Object type '${it.name}' is a member of a known union, but no class could be found for that type name. Please pass a class for type '${it.name}' in the parser's dictionary." })
94+
handleInterfaceOrUnionSubTypes(getAllObjectTypeMembersOfDiscoveredUnions()) { "Object type '${it.name}' is a member of a known union, but no class could be found for that type name. Please pass a class for type '${it.name}' in the parser's dictionary." }
9595
} while (scanQueue())
9696

9797
return validateAndCreateResult(rootTypeHolder)
@@ -253,7 +253,7 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
253253
* Enter a found type into the dictionary if it doesn't exist yet, add a reference pointing back to where it was discovered.
254254
*/
255255
private fun handleFoundType(type: TypeDefinition<*>, clazz: Class<*>?, reference: Reference) {
256-
val realEntry = dictionary.getOrPut(type, { DictionaryEntry() })
256+
val realEntry = dictionary.getOrPut(type) { DictionaryEntry() }
257257
var typeWasSet = false
258258

259259
if (clazz != null) {
@@ -288,16 +288,23 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
288288

289289
is InputObjectTypeDefinition -> {
290290
graphQLType.inputValueDefinitions.forEach { inputValueDefinition ->
291-
findInputValueType(inputValueDefinition.name, javaType)?.let { inputValueJavaType ->
292-
val inputGraphQLType = inputValueDefinition.type.unwrap()
293-
if(inputGraphQLType is TypeName && !ScalarInfo.STANDARD_SCALAR_DEFINITIONS.containsKey(inputGraphQLType.name)) {
291+
val inputGraphQLType = inputValueDefinition.type.unwrap()
292+
if (inputGraphQLType is TypeName && !ScalarInfo.STANDARD_SCALAR_DEFINITIONS.containsKey(inputGraphQLType.name)) {
293+
val inputValueJavaType = findInputValueType(inputValueDefinition.name, javaType)
294+
if (inputValueJavaType != null) {
294295
handleFoundType(typeClassMatcher.match(TypeClassMatcher.PotentialMatch.parameterType(
295-
inputValueDefinition.type,
296-
inputValueJavaType,
297-
GenericType(javaType, options).relativeToType(inputValueJavaType),
298-
InputObjectReference(inputValueDefinition),
299-
false
296+
inputValueDefinition.type,
297+
inputValueJavaType,
298+
GenericType(javaType, options).relativeToType(inputValueJavaType),
299+
InputObjectReference(inputValueDefinition),
300+
false
300301
)))
302+
} else {
303+
var mappingAdvice = ""
304+
if (javaType.name.contains("Map")) {
305+
mappingAdvice = ". Try using a class to represent your input type instead of a Map."
306+
}
307+
log.warn("Cannot find definition for field '${inputValueDefinition.name}: ${inputGraphQLType.name}' on input type '${graphQLType.name}' -> ${javaType.name}$mappingAdvice")
301308
}
302309
}
303310
}

src/test/groovy/com/coxautodev/graphql/tools/SchemaParserSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class SchemaParserSpec extends Specification {
344344
}
345345
346346
input SaveInput {
347+
age: Int
347348
type: EnumType!
348349
}
349350

0 commit comments

Comments
 (0)