graphql-java / graphql-java-extended-validation

Validation library for graphql-java input
MIT License
128 stars 34 forks source link

Exceptions not working as expected with Directive Wiring #79

Open jdsalasbarrantes opened 2 years ago

jdsalasbarrantes commented 2 years ago

Describe the bug

I'm trying to connect kotlin, spring for graphql, and the validation feature, but when I add the directive wiring, the exceptions thrown in the service layer are not being displayed correctly.

Config:

@Configuration
class GraphQLConfig {

    @Bean
    fun runtimeWiringConfigurer(): RuntimeWiringConfigurer {
        val validationRules = ValidationRules.newValidationRules()
            .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
            .build()
        val schemaWiring = ValidationSchemaWiring(validationRules)

        return RuntimeWiringConfigurer { builder: RuntimeWiring.Builder ->
            builder.directiveWiring(schemaWiring).build()
        }
    }
}

Controller:

   @MutationMapping
    fun createCourse(
        @Arguments createCourseDto: CreateCourseDto
    ): CourseDto {
        throw RuntimeException("exception from controller")
    }

Graphql:

directive @NotBlank(message : String! = "graphql.validation.NotBlank.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

 createCourse(
        name: String! @NotBlank
    ): Course

type Course {
    id: ID
    name: String
}

When I call this endpoint, the result is an empty object, in the background, an exception is being thrown (check the controller snippet above)

{
  "data": {
    "createCourse": {
      "id": null,
      "name": null
    }
  }
}

But If I remove the directive wiring config:

//@Configuration
//class GraphQLConfig {
//
//    @Bean
//    fun runtimeWiringConfigurer(): RuntimeWiringConfigurer {
//        val validationRules = ValidationRules.newValidationRules()
//            .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
//            .build()
//        val schemaWiring = ValidationSchemaWiring(validationRules)
//
//        return RuntimeWiringConfigurer { builder: RuntimeWiring.Builder ->
//            builder.directiveWiring(schemaWiring).build()
//        }
//    }
//}

I do get the exception as usual:

{
  "errors": [
    {
      "message": "INTERNAL_ERROR for a9883b45-f615-258a-4f7a-bda5b432d485",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createCourse"
      ],
      "extensions": {
        "classification": "INTERNAL_ERROR"
      }
    }
  ],
  "data": {
    "createCourse": null
  }
}

Is it something wrong with the code, or could this be a bug?

JBO24 commented 2 years ago

We are running into the same issue. Do you have any solution for this?

jdsalasbarrantes commented 2 years ago

Hi @JBO24, I ended up implementing my own version of the ValidationSchemaWiring, using only the onArgument function. Another alternative I consider was using the spring validator