Skip to content

Commit b5a6790

Browse files
committed
Polishing Validation section in reference
See gh-344
1 parent 304fb45 commit b5a6790

File tree

1 file changed

+52
-47
lines changed

1 file changed

+52
-47
lines changed

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@ You can use `@Argument` on a `Map<String, Object>` argument, to obtain all argum
12591259
values. The name attribute on `@Argument` must not be set.
12601260

12611261

1262+
12621263
[[controllers-schema-mapping-arguments]]
12631264
==== `@Arguments`
12641265

@@ -1271,53 +1272,6 @@ case, top-level arguments are bound to `BookInput` properties.
12711272

12721273

12731274

1274-
[[controllers-schema-mapping-validation]]
1275-
==== `@Argument(s)` Validation
1276-
1277-
If a {spring-framework-ref-docs}/core.html#validation-beanvalidation-overview[Bean Validation]
1278-
`Validator` (or typically, a `LocalValidatorFactoryBean`) bean is present in the application context,
1279-
the `AnnotatedControllerConfigurer` will auto-detect it and configure support for validation.
1280-
Controller arguments annotated with `@Valid` and `@Validated` are then validated before method invocation.
1281-
1282-
Bean Validation lets you declare constraints on types, as the following example shows:
1283-
1284-
[source,java,indent=0,subs="verbatim,quotes"]
1285-
----
1286-
public class BookInput {
1287-
1288-
@NotNull
1289-
private String title;
1290-
1291-
@NotNull
1292-
@Size(max=13)
1293-
private String isbn;
1294-
}
1295-
----
1296-
1297-
We can then mark our argument for validation with `@Valid`:
1298-
1299-
[source,java,indent=0,subs="verbatim,quotes"]
1300-
----
1301-
@Controller
1302-
public class BookController {
1303-
1304-
@MutationMapping
1305-
public Book addBook(@Argument @Valid BookInput bookInput) {
1306-
// ...
1307-
}
1308-
}
1309-
----
1310-
1311-
If an error occurs during validation, a `ConstraintViolationException` is thrown and can be
1312-
later <<execution-exceptions,resolved with a custom `DataFetcherExceptionResolver`>>.
1313-
1314-
[TIP]
1315-
====
1316-
Unlike Spring MVC, handler method signatures do not support the injection of `BindingResult`
1317-
for reacting to validation errors: those are globally dealt with as exceptions.
1318-
====
1319-
1320-
13211275
[[controllers-schema-mapping-projectedpayload-argument]]
13221276
==== `@ProjectedPayload` Interface
13231277

@@ -1368,6 +1322,7 @@ For example:
13681322
----
13691323

13701324

1325+
13711326
[[controllers-schema-mapping-source]]
13721327
==== Source
13731328

@@ -1436,6 +1391,56 @@ delegates to a `DataLoader`, you can reduce boilerplate by using a
14361391
<<controllers-batch-mapping,@BatchMapping>> method as described in the next section.
14371392

14381393

1394+
[[controllers-schema-mapping-validation]]
1395+
==== Validation
1396+
1397+
When a `javax.validation.Validator` bean is found, `AnnotatedControllerConfigurer` enables support for
1398+
{spring-framework-ref-docs}/core.html#validation-beanvalidation-overview[Bean Validation]
1399+
on annotated controller methods. Typically, the bean is of type `LocalValidatorFactoryBean`.
1400+
1401+
Bean validation lets you declare constraints on types:
1402+
1403+
[source,java,indent=0,subs="verbatim,quotes"]
1404+
----
1405+
public class BookInput {
1406+
1407+
@NotNull
1408+
private String title;
1409+
1410+
@NotNull
1411+
@Size(max=13)
1412+
private String isbn;
1413+
}
1414+
----
1415+
1416+
You can then annotate a controller method parameter with `@Valid` to validate it before
1417+
method invocation:
1418+
1419+
[source,java,indent=0,subs="verbatim,quotes"]
1420+
----
1421+
@Controller
1422+
public class BookController {
1423+
1424+
@MutationMapping
1425+
public Book addBook(@Argument @Valid BookInput bookInput) {
1426+
// ...
1427+
}
1428+
}
1429+
----
1430+
1431+
If an error occurs during validation, a `ConstraintViolationException` is raised.
1432+
You can use the <<execution-exceptions>> chain to decide how to present that to clients
1433+
by turning it into an error to include in the GraphQL response.
1434+
1435+
TIP: In addition to `@Valid`, you can also use Spring's `@Validated` that allows
1436+
specifying validation groups.
1437+
1438+
Bean validation is useful for <<controllers-schema-mapping-argument>>,
1439+
<<controllers-schema-mapping-arguments>>, and
1440+
<<controllers-schema-mapping-projectedpayload-argument,@ProjectedPayload>>
1441+
method parameters, but applies more generally to any method parameter.
1442+
1443+
14391444

14401445
[[controllers-batch-mapping]]
14411446
=== `@BatchMapping`

0 commit comments

Comments
 (0)