Federation

适用于 GraphQL 的 Spring 为 federation-jvm 库提供了一个小型集成层,该库又建立在 GraphQL Java 之上,并有助于为联邦图中的子图的 GraphQL Java 应用程序初始化 graphql.schema.GraphQLSchema。有关详细信息,请参见 Apollo FederationSubgraph spec

Spring for GraphQL provides a small integration layer for the federation-jvm library that in turn builds on GraphQL Java, and helps to initialize the graphql.schema.GraphQLSchema for a GraphQL Java application that is a sub-graph within a federated graph. For more details, see Apollo Federation and the Subgraph spec.

要使用这项支持,您可以在配置中声明一个 FederationSchemaFactory Bean,并将其插入 GraphQlSource.Builder。在 Spring Boot 应用程序中,您可以通过 GraphQlSourceBuilderCustomizer 按照如下方式进行操作:

To use the support you can declare a FederationSchemaFactory bean in your config, and plug it into GraphQlSource.Builder. In a Spring Boot application you can do this through a GraphQlSourceBuilderCustomizer as follows:

@Configuration
public class FederationConfig {

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

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

}

现在,您的子图模式可以扩展联合类型:

Now your sub-graph schema can extend federated types:

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

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

要帮助解决作为 _entities 查询一部分的联邦类型,您可以将 @EntityMapping 方法与 @SchemaMapping 方法并排用于子图应用程序拥有的数据。例如:

To assist with resolving federated types as part of an _entities query, you can use @EntityMapping methods side by side with @SchemaMapping methods for the data that the subgraph application owns. For example:

@Controller
private static class BookController {

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

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

}

@Argument 方法参数从实体的 “representation” 输入映射中解析。您还可以注入完整 Map<String, Object>。下面显示了所有受支持的参数:

The @Argument method parameters is resolved from the "representation"input map for the entity. You can also inject the full Map<String, Object>. The below shows all supported arguments:

Method Argument Description

@Argument

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

Map<String, Object>

The full "representation" input map for the entity.

@ContextValue

For access to an attribute from the main GraphQLContext in DataFetchingEnvironment.

@LocalContextValue

For access to an attribute from the local GraphQLContext in DataFetchingEnvironment.

GraphQLContext

For access to the context from the DataFetchingEnvironment.

java.security.Principal

Obtained from the Spring Security context, if available.

@AuthenticationPrincipal

For access to Authentication#getPrincipal() from the Spring Security context.

DataFetchingFieldSelectionSet

For access to the selection set for the query through the DataFetchingEnvironment.

Locale, Optional<Locale>

For access to the Locale from the DataFetchingEnvironment.

DataFetchingEnvironment

For direct access to the underlying DataFetchingEnvironment.

@EntityMapping 方法可以返回 MonoCompletableFutureCallable 或实际实体。

@EntityMapping methods can return Mono, CompletableFuture, Callable, or the actual entity.

你可以使用 @GraphQlExceptionHandler 方法来将 @EntityMapping 方法中的异常映射到 GraphQLError’s 。这些错误将包含在“_entities”查询的响应中。异常处理程序方法可以位于同一控制器中,或位于 `@ControllerAdvice 类中。

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.