in src/main/kotlin/com/netflix/dgs/plugin/hints/DgsInputArgumentValidationInspector.kt [42:99]
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return UastVisitorAdapter(object : AbstractUastNonRecursiveVisitor() {
override fun visitMethod(node: UMethod): Boolean {
if (InputArgumentUtils.hasDgsAnnotation(node) ) {
val dgsDataAnnotation = InputArgumentUtils.getDgsAnnotation(node)
val dgsService = dgsDataAnnotation.project.getService(DgsService::class.java)
val typeDefinitionRegistry = GraphQLSchemaProvider.getInstance(dgsDataAnnotation.project).getSchemaInfo(node.javaPsi).registry
val dgsDataFetcher = dgsService.dgsComponentIndex.dataFetchers.find { it.psiAnnotation.toUElement() == dgsDataAnnotation.toUElement() }
if (dgsDataFetcher?.schemaPsi != null) {
val isJavaFile = dgsDataFetcher.psiFile is PsiJavaFile
val arguments = (dgsDataFetcher.schemaPsi as? GraphQLFieldDefinitionImpl)?.argumentsDefinition?.inputValueDefinitionList
if (!arguments.isNullOrEmpty()) {
// validate each argument specified in the schema against the data fetcher's input arguments
node.uastParameters.stream().filter { it.hasAnnotation(InputArgumentUtils.DGS_INPUT_ARGUMENT_ANNOTATION) }.toList().forEach { inputArgument ->
val inputArgName = getInputArgumentName(inputArgument)
val schemaSearchResult = arguments.stream().filter { graphQLInputValueDefinition ->
inputArgName == (graphQLInputValueDefinition.nameIdentifier as GraphQLIdentifierImpl).name }.toList()
// if the list is empty, there is no corresponding input argument with the same name defined in the schema
if (schemaSearchResult.isEmpty()) {
var validArgumentNames = ""
// construct a list of valid argument names for the hint and fix
arguments.forEach {
validArgumentNames = validArgumentNames.plus((it.nameIdentifier as GraphQLIdentifierImpl).name).plus(", ")
}
validArgumentNames = validArgumentNames.removeSuffix(", ")
val message = MyBundle.getMessage(
"dgs.inspection.dgsinputargumentnamevalidation.hint",
inputArgName,
validArgumentNames
)
registerProblemWithArgumentName(holder, node, inputArgument, message)
} else {
// validate the type of the input argument
val graphQLInputValueDefinition = schemaSearchResult[0]
val fixedInputArgument = InputArgumentUtils.getHintForInputArgument(graphQLInputValueDefinition!!, typeDefinitionRegistry, isJavaFile)
// enable hinting only if the argument is not a custom scalar or if it does not have the expected type
if (!InputArgumentUtils.isCustomScalarType(graphQLInputValueDefinition.type!!, typeDefinitionRegistry)
&& !hasExpectedAnnotation(graphQLInputValueDefinition, inputArgument, isJavaFile)) {
val message = MyBundle.getMessage(
"dgs.inspection.dgsinputargumentvalidation.hint",
fixedInputArgument
)
registerProblemWithArgumentType(holder, node, inputArgument, message, fixedInputArgument)
}
}
}
}
}
}
return super.visitMethod(node)
}
}, false)
}