dotansimha / dotansimha/graphql-code-generator-community

Add Jakarta Validation Support to Generated GraphQL Input Classes

Open
#1,315 0 comments 0 reactions 1 assignee Claimed by @eddeee888 View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

**Is your feature request related to a problem? Please describe.**

On the Kotlin JVM platform, without the `@Valid` annotation, we can only perform validation manually.

```kotlin
@Controller
class ClientAddressMutationResolver() {

@MutationMapping
fun createAddress(
@Argument input: AddressInput,
): Address {
if (input.detailAddress.length < 5 || input.detailAddress.length > 200) {
throw Exception("...")
}
}
}
```

`AddressInput` is a generated class, such as:

```kt
data class AddressInput(
val detailAddress: kotlin.String,
)
```
**Describe the solution you'd like**
If `AddressInput` could be generated with the `jakarta.validation.constraints.Size` annotation, validation could be automated.

```diff
@MutationMapping
fun createAddress(
- @Argument input: AddressInput,
+ @Argument @Valid input: AddressInput,
): Address {
- if (input.detailAddress.length < 5 || input.detailAddress.length > 200) {
- throw Exception("...")
- }
}
```

Generate AddressInputas shown below:

```diff
data class AddressInput(
+ @field:Size(min = 5, max = 200, message = "Detailed address length must be 5-200 characters")
val detailAddress: kotlin.String,
}
```

And include a custom directive in the GraphQL schema definition:

```graphgl
directive @size(min: Int, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

input AddressInput {
detailAddress: String!
@size(min: 5, max: 200, message: "Detailed address length must be 5-200 characters")
}
```

**Describe alternatives you've considered**

**Additional context**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.