Skip to content

Commit d45d31b

Browse files
committed
Update docs for schema interface mappings
Closes gh-871
1 parent 0b491b9 commit d45d31b

File tree

1 file changed

+164
-1
lines changed

1 file changed

+164
-1
lines changed

spring-graphql-docs/modules/ROOT/pages/controllers.adoc

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,88 @@ Schema mapping handler methods can return:
217217
For this to work, `AnnotatedControllerConfigurer` must be configured with an `Executor`.
218218

219219

220+
221+
[[controllers.schema-mapping.interfaces]]
222+
=== Interface Schema Mappings
223+
224+
When a controller method is mapped to a schema interface field, by default the mapping is
225+
replaced with multiple mappings, one for each schema object type that implements the interface.
226+
This allows use of one controller method for all subtypes.
227+
228+
For example, given:
229+
230+
[source,graphql,indent=0,subs="verbatim,quotes"]
231+
----
232+
type Query {
233+
activities: [Activity!]!
234+
}
235+
236+
interface Activity {
237+
id: ID!
238+
coordinator: User!
239+
}
240+
241+
type FooActivity implements Activity {
242+
id: ID!
243+
coordinator: User!
244+
}
245+
246+
type BarActivity implements Activity {
247+
id: ID!
248+
coordinator: User!
249+
}
250+
251+
type User {
252+
name: String!
253+
}
254+
----
255+
256+
You can write a controller like this:
257+
258+
[source,java,indent=0,subs="verbatim,quotes"]
259+
----
260+
@Controller
261+
public class BookController {
262+
263+
@QueryMapping
264+
public List<Activity> activities() {
265+
// ...
266+
}
267+
268+
@SchemaMapping
269+
public User coordinator(Activity activity) {
270+
// Called for any Activity subtype
271+
}
272+
273+
}
274+
----
275+
276+
If necessary, you can take over the mapping for individual subtypes:
277+
278+
[source,java,indent=0,subs="verbatim,quotes"]
279+
----
280+
@Controller
281+
public class BookController {
282+
283+
@QueryMapping
284+
public List<Activity> activities() {
285+
// ...
286+
}
287+
288+
@SchemaMapping
289+
public User coordinator(Activity activity) {
290+
// Called for any Activity subtype except FooActivity
291+
}
292+
293+
@SchemaMapping
294+
public User coordinator(FooActivity activity) {
295+
// ...
296+
}
297+
298+
}
299+
----
300+
301+
220302
[[controllers.schema-mapping.argument]]
221303
=== `@Argument`
222304

@@ -675,6 +757,88 @@ Batch mapping methods can return:
675757

676758

677759

760+
[[controllers.batch-mapping.interfaces]]
761+
=== Interface Batch Mappings
762+
763+
As is the case with xref:controllers.adoc#controllers.schema-mapping.interfaces[Interface Schema Mappings],
764+
when a batch mapping method is mapped to a schema interface field, the mapping is replaced with
765+
multiple mappings, one for each schema object type that implements the interface.
766+
767+
That means, given the following:
768+
769+
[source,graphql,indent=0,subs="verbatim,quotes"]
770+
----
771+
type Query {
772+
activities: [Activity!]!
773+
}
774+
775+
interface Activity {
776+
id: ID!
777+
coordinator: User!
778+
}
779+
780+
type FooActivity implements Activity {
781+
id: ID!
782+
coordinator: User!
783+
}
784+
785+
type BarActivity implements Activity {
786+
id: ID!
787+
coordinator: User!
788+
}
789+
790+
type User {
791+
name: String!
792+
}
793+
----
794+
795+
You can write a controller like this:
796+
797+
[source,java,indent=0,subs="verbatim,quotes"]
798+
----
799+
@Controller
800+
public class BookController {
801+
802+
@QueryMapping
803+
public List<Activity> activities() {
804+
// ...
805+
}
806+
807+
@BatchMapping
808+
Map<Activity, User> coordinator(List<Activity> activities) {
809+
// Called for all Activity subtypes
810+
}
811+
}
812+
----
813+
814+
If necessary, you can take over the mapping for individual subtypes:
815+
816+
[source,java,indent=0,subs="verbatim,quotes"]
817+
----
818+
@Controller
819+
public class BookController {
820+
821+
@QueryMapping
822+
public List<Activity> activities() {
823+
// ...
824+
}
825+
826+
@BatchMapping
827+
Map<Activity, User> coordinator(List<Activity> activities) {
828+
// Called for all Activity subtypes
829+
}
830+
831+
@BatchMapping(field = "coordinator")
832+
Map<Activity, User> fooCoordinator(List<FooActivity> activities) {
833+
// ...
834+
}
835+
}
836+
----
837+
838+
839+
840+
841+
678842
[[controllers.exception-handler]]
679843
== `@GraphQlExceptionHandler`
680844

@@ -696,7 +860,6 @@ controller, exception handler methods apply to exceptions from the same controll
696860
public GraphQLError handle(BindException ex) {
697861
return GraphQLError.newError().errorType(ErrorType.BAD_REQUEST).message("...").build();
698862
}
699-
700863
}
701864
----
702865

0 commit comments

Comments
 (0)