3
3
4
4
Spring for GraphQL provides an integration for the
5
5
https://github.com/apollographql/federation-jvm[federation-jvm] library, which uses
6
- GraphQL Java to initialize the schema of a sub-graph within a federated graph .
6
+ GraphQL Java to initialize the schema of a sub-graph within a graph of federated services .
7
7
See https://www.apollographql.com/docs/federation/[Apollo Federation] and the
8
- https://www.apollographql.com/docs/federation/subgraph-spec[Subgraph spec ] for further details.
8
+ https://www.apollographql.com/docs/federation/subgraph-spec[Subgraph specification ] for details.
9
9
10
10
11
11
12
12
[[federation.config]]
13
13
== Config
14
14
15
- To use the integration, declare a `FederationSchemaFactory` bean in your config, and plug
16
- it into `GraphQlSource.Builder`. For example, in a Spring Boot application :
15
+ To enable the integration, declare a `FederationSchemaFactory` bean in your config, and plug
16
+ it into `GraphQlSource.Builder`. For example, with Spring Boot:
17
17
18
18
[source,java,indent=0,subs="verbatim,quotes"]
19
19
----
20
20
@Configuration
21
21
public class FederationConfig {
22
22
23
- @Bean
24
- public FederationSchemaFactory schemaFactory() {
25
- return new FederationSchemaFactory();
26
- }
27
-
28
23
@Bean
29
24
public GraphQlSourceBuilderCustomizer customizer(FederationSchemaFactory factory) {
30
25
return builder -> builder.schemaFactory(factory::createGraphQLSchema);
31
26
}
32
27
28
+ @Bean
29
+ public FederationSchemaFactory schemaFactory() {
30
+ return new FederationSchemaFactory();
31
+ }
33
32
}
34
33
----
35
34
@@ -53,9 +52,9 @@ type Author {
53
52
[[federation.entity-mapping]]
54
53
== `@EntityMapping`
55
54
56
- To resolve federated types in response to the
57
- https://www.apollographql.com/docs/federation/subgraph-spec/#understanding-query_entities[_entities query],
58
- you can use `@EntityMapping` methods along with `@SchemaMapping` methods for types under the entity.
55
+ An `@EntityMapping` method can load federated type instances in response to an
56
+ https://www.apollographql.com/docs/federation/subgraph-spec/#understanding-query_entities[_entities query]
57
+ from the federation gateway. For example:
59
58
60
59
For example:
61
60
@@ -65,26 +64,27 @@ For example:
65
64
private static class BookController {
66
65
67
66
@EntityMapping
68
- public Book book(@Argument int id) {
67
+ public Book book(@Argument int id) { // <1>
69
68
// ...
70
69
}
71
70
72
71
@SchemaMapping
73
- public Author author(Book book) {
72
+ public Author author(Book book) { // <2>
74
73
// ...
75
74
}
76
75
77
76
}
78
77
----
79
78
80
- The `@Argument` method parameters is resolved from the "representation" input map for
81
- the entity. You can also inject the full "representation" input `Map`. See
82
- xref:federation.adoc#federation.entity-mapping.signature[Method Signature] for all
83
- supported method argument and return value types.
79
+ <1> The `@Argument` method parameter is resolved from the "representation" input map for
80
+ the entity. The full "representation" input `Map` can also be resolved. See
81
+ xref:federation.adoc#federation.entity-mapping.signature[Method Signature] for supported
82
+ method argument and return value types.
83
+ <2> `@SchemaMapping` methods can be used for the rest of the graph.
84
84
85
- You can batch load federated entities by returning a `List` of instances from the controller
86
- method and accepting a `List` of argument values. In addition, you can use `@BatchMapping`
87
- methods for subfields .
85
+ An `@EntityMapping` method can batch load federated entities of a given type. To do that,
86
+ declare the `@Argument` method parameter as a list, and return the corresponding entity
87
+ instances as a list in the same order .
88
88
89
89
For example:
90
90
@@ -94,20 +94,21 @@ For example:
94
94
private static class BookController {
95
95
96
96
@EntityMapping
97
- public List<Book> book(@Argument List<Integer> idList) {
98
- // ...
97
+ public List<Book> book(@Argument List<Integer> idList) { // <1>
98
+ // ... return books in the same order
99
99
}
100
100
101
101
@BatchMapping
102
- public Map<Book, Author> author(List<Book> books) {
102
+ public Map<Book, Author> author(List<Book> books) { // <2>
103
103
// ...
104
104
}
105
105
}
106
106
----
107
107
108
- Note `idList` naming convention for the argument, which helps Spring for GraphQL to
109
- de-pluralize the method parameter name and derive the correct argument name to use.
110
- Alternatively, set the argument name through the annotation.
108
+ <1> The `idList` naming convention helps to de-pluralize the parameter name in order to
109
+ look up the correct value in the "representation" input map. You can also set the
110
+ argument name through the annotation.
111
+ <2> `@BatchMapping` methods can be used for the rest of the graph.
111
112
112
113
113
114
0 commit comments