我有一个 spring-boot graphQL SPQR 服务,公开了一个子图。 我们使用 apollo 服务器将所有子图联合成一个超级图。 为了使子图模式联合兼容,apollo 有一个名为 federation-jvm 的项目 他们的 SchemaTransformer 添加了常见的 Federation 类型定义(例如 _Any 标量、_Entity union、Federation 指令等)。
从他们的示例中,我注意到我必须将以下两个 bean 添加到 spring 应用程序中。
@Bean
public GraphQlSourceBuilderCustomizer customizer(FederationSchemaFactory factory) {
return builder -> builder.schemaFactory(factory::createGraphQLSchema);
}
@Bean
FederationSchemaFactory federationSchemaFactory() {
return new FederationSchemaFactory();
}
失败并显示错误“未配置 GraphQL 架构定义”,因为需要架构定义。
这是完整的堆栈跟踪。请给我一些指导,我如何集成他们的架构转换器。
org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名为“routerFunctionMapping”的bean时出错[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:创建名为“routerFunctionMapping”的bean时出错graphQlRouterFunction'在类路径资源[org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.class]中定义:通过方法'graphQlRouterFunction'参数0表达的依赖关系不满足:创建在类路径资源中定义的名称为'graphQlHttpHandler'的bean时出错[ org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.class]:通过方法“graphQlHttpHandler”参数表达的依赖关系不满足0:创建类路径资源中定义的名为“webGraphQlHandler”的bean时出错[org/springframework/boot/autoconfigure/ graphql/servlet/GraphQlWebMvcAutoConfiguration.class]:通过方法“webGraphQlHandler”参数表达的依赖关系不满足0:创建类路径资源[org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.class]中定义的名为“executionGraphQlService”的bean时出错:不满足通过方法“executionGraphQlService”参数0表示的依赖关系:创建在类路径资源[org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.class]中定义的名为“graphQlSource”的bean时出错:无法实例化[org.springframework.graphql.execution .GraphQlSource]:工厂方法“graphQlSource”抛出异常并显示消息:未配置 GraphQL 架构定义。
我发现我可以这样做来转换模式。但 gui 仍然显示没有添加的指令。但打印的模式确实显示了 federation_jvm 添加的 _service。这是否意味着此更改不会对 GUI 中公开的最终架构产生任何影响?
@Bean
public GraphQLSchema federatedSchema() {
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withBasePackages("com.madhi.graphql_demo2.service", "com.madhi.graphql_demo2")
.withOperationsFromSingletons(payrollService, employeeService)
.generate();
schema = Federation.transform(schema).build();
String printedSchema = new SchemaPrinter(
// Tweak the options accordingly
SchemaPrinter.Options.defaultOptions().
includeDirectives(true)
).print(schema);
System.out.println(printedSchema);
return schema;
}
终于成功了。更改后的架构会显示在 GUI 中。
@Bean
public ExecutableSchema graphQLExecutableSchema(GraphQLSchemaGenerator schemaGenerator) {
ExecutableSchema schema = schemaGenerator.generateExecutable();
GraphQLSchema fedSchema = Federation.transform(schema.getSchema()).build();
String printedSchema = new SchemaPrinter(
// Tweak the options accordingly
SchemaPrinter.Options.defaultOptions().
includeDirectives(true)
).print(fedSchema);
System.out.println(printedSchema);
return new ExecutableSchema(fedSchema, schema.getTypeRegistry(), schema.getBatchLoaders(), null);
}