Spring for GraphQL provides an integration for the federation-jvm library, which uses GraphQL Java to initialize the schema of a sub-graph within a graph of federated services. See Apollo Federation and the Subgraph specification for details.spring-doc.cn

Config

To enable the integration, declare a FederationSchemaFactory bean in your config, and plug it into GraphQlSource.Builder. For example, with Spring Boot:spring-doc.cn

@Configuration
public class FederationConfig {

	@Bean
	public GraphQlSourceBuilderCustomizer customizer(FederationSchemaFactory factory) {
		return builder -> builder.schemaFactory(factory::createGraphQLSchema);
	}

	@Bean
	public FederationSchemaFactory schemaFactory() {
		return new FederationSchemaFactory();
	}
}

Now the schema for the sub-graph service can extend federated types:spring-doc.cn

type Book @key(fields: "id") @extends {
    id: ID! @external
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}

@EntityMapping

An @EntityMapping method can load federated type instances in response to an _entities query from the federation gateway. For example:spring-doc.cn

For example:spring-doc.cn

@Controller
private static class BookController {

	@EntityMapping
	public Book book(@Argument int id) { (1)
		// ...
	}

	@SchemaMapping
	public Author author(Book book) { (2)
		// ...
	}

}
1 The @Argument method parameter is resolved from the "representation" input map for the entity. The full "representation" input Map can also be resolved. See Method Signature for supported method argument and return value types.
2 @SchemaMapping methods can be used for the rest of the graph.

An @EntityMapping method can batch load federated entities of a given type. To do that, declare the @Argument method parameter as a list, and return the corresponding entity instances as a list in the same order.spring-doc.cn

For example:spring-doc.cn

@Controller
private static class BookController {

	@EntityMapping
	public List<Book> book(@Argument List<Integer> idList) { (1)
		// ... return books in the same order
	}

	@BatchMapping
	public Map<Book, Author> author(List<Book> books) { (2)
		// ...
	}
}
1 The idList naming convention helps to de-pluralize the parameter name in order to look up the correct value in the "representation" input map. You can also set the argument name through the annotation.
2 @BatchMapping methods can be used for the rest of the graph.

Method Signature

Entity mapping methods support the following arguments:spring-doc.cn

Method Argument Description

@Argumentspring-doc.cn

For access to a named value from the "representation" input map, also converted to typed Object.spring-doc.cn

Map<String, Object>spring-doc.cn

The full "representation" input map for the entity.spring-doc.cn

List<Map<String, Object>>spring-doc.cn

The list of "representation" input maps when using a single controller method to load all entities of a given type.spring-doc.cn

@ContextValuespring-doc.cn

For access to an attribute from the main GraphQLContext in DataFetchingEnvironment.spring-doc.cn

@LocalContextValuespring-doc.cn

For access to an attribute from the local GraphQLContext in DataFetchingEnvironment.spring-doc.cn

GraphQLContextspring-doc.cn

For access to the context from the DataFetchingEnvironment.spring-doc.cn

java.security.Principalspring-doc.cn

Obtained from the Spring Security context, if available.spring-doc.cn

@AuthenticationPrincipalspring-doc.cn

For access to Authentication#getPrincipal() from the Spring Security context.spring-doc.cn

DataFetchingFieldSelectionSetspring-doc.cn

For access to the selection set for the query through the DataFetchingEnvironment.spring-doc.cn

Locale, Optional<Locale>spring-doc.cn

For access to the Locale from the DataFetchingEnvironment.spring-doc.cn

DataFetchingEnvironmentspring-doc.cn

For direct access to the underlying DataFetchingEnvironment.spring-doc.cn

@EntityMapping methods can return Mono, CompletableFuture, Callable, or the actual entity.spring-doc.cn

Exception Handling

You can use @GraphQlExceptionHandler methods to map exceptions from @EntityMapping methods to GraphQLError's. The errors will be included in the response of the "_entities" query. Exception handler methods can be in the same controller or in an @ControllerAdvice class.spring-doc.cn

1 The @Argument method parameter is resolved from the "representation" input map for the entity. The full "representation" input Map can also be resolved. See Method Signature for supported method argument and return value types.
2 @SchemaMapping methods can be used for the rest of the graph.
1 The idList naming convention helps to de-pluralize the parameter name in order to look up the correct value in the "representation" input map. You can also set the argument name through the annotation.
2 @BatchMapping methods can be used for the rest of the graph.
Method Argument Description

@Argumentspring-doc.cn

For access to a named value from the "representation" input map, also converted to typed Object.spring-doc.cn

Map<String, Object>spring-doc.cn

The full "representation" input map for the entity.spring-doc.cn

List<Map<String, Object>>spring-doc.cn

The list of "representation" input maps when using a single controller method to load all entities of a given type.spring-doc.cn

@ContextValuespring-doc.cn

For access to an attribute from the main GraphQLContext in DataFetchingEnvironment.spring-doc.cn

@LocalContextValuespring-doc.cn

For access to an attribute from the local GraphQLContext in DataFetchingEnvironment.spring-doc.cn

GraphQLContextspring-doc.cn

For access to the context from the DataFetchingEnvironment.spring-doc.cn

java.security.Principalspring-doc.cn

Obtained from the Spring Security context, if available.spring-doc.cn

@AuthenticationPrincipalspring-doc.cn

For access to Authentication#getPrincipal() from the Spring Security context.spring-doc.cn

DataFetchingFieldSelectionSetspring-doc.cn

For access to the selection set for the query through the DataFetchingEnvironment.spring-doc.cn

Locale, Optional<Locale>spring-doc.cn

For access to the Locale from the DataFetchingEnvironment.spring-doc.cn

DataFetchingEnvironmentspring-doc.cn

For direct access to the underlying DataFetchingEnvironment.spring-doc.cn