Java GraphQL Spring Boot Apollo联邦问题与引用解析器



我正在尝试使用Java/Spring Boot和GraphQL -kickstart让Apollo federation与两个GraphQL服务一起工作。考虑以下模式:

人服务:

type Person @key(fields: "id") {
id: String!
name: String!
}

书服务:

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

我正在尝试运行如下查询:

query {
book(bookId:"1") {
author {
name
}
}
}

当我对Apollo网关运行查询时,它对Person服务运行的查询是:

query ($representations:[_Any!]!) {
_entities(representations:$representations) {
... on Person {
name
}
}
}
与变量:

{
"representations": [
{
"__typename": "Person",
"id": "2"
}
]
}

返回:

{
"data": {
"_entities": [null]
}
}

GraphQL配置(带有@Configuration注释的类)具有以下内容(取自https://github.com/setchy/graphql-java-kickstart-federation-example/blob/master/shows/src/main/java/com/example/demo/federation/FederatedSchema.java):

的示例)
@Bean
public GraphQLSchema customSchema(SchemaParser schemaParser) {
GraphQLSchema federatedSchema = Federation.transform(schemaParser.makeExecutableSchema())
.fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(reference -> {
return null;
})
.collect(Collectors.toList()))
.resolveEntityType(env -> {
return null;
})
.build();
return federatedSchema;
}

根据https://www.apollographql.com/docs/federation/entities/#2-define-a-reference-resolver上的阿波罗文档,. fetchentities方法应该添加一个解析类型的引用解析器。然而,我不确定如何在静态上下文中做到这一点,并且在上面链接的示例中没有这样做(尽管由于单独的问题,我无法使示例工作)。

任何有助于指出错误的想法都是值得感谢的。

由于您没有向Person提供任何字段,因此我认为您需要像这样定义它

书服务:

type Book @key(fields: "id") {
id: String!
author: Person!
}
type Person @extends @key(fields: "id", resolvable: false) {
id: String!
}

最新更新