我是非常新的阿波罗联邦和网关,并试图使用阿波罗联邦-jvm的演示项目。我使用联合jvm创建了两个联邦服务。这两项服务通过阿波罗网关相互连接。使用官方github页面中的这节点网关示例。
下面是这些服务的架构和解析器:
联邦服务1:
video.graphql
type Video @key(fields: "videoId") {
videoId: String!
description: String
relatedNameId: String
}
type Query {
topVideo: Video
allVideos: [Video]
}用于video.graphql的解析器
@Service
public class VideoQuery implements GraphQLQueryResolver {
private static final List<Video> videos = Lists.newArrayList(
Video.builder().videoId("vi00000001").description("Video 1").relatedNameId("nm000001").build(),
Video.builder().videoId("vi00000002").description("Video 2").relatedNameId("nm000001").build()
);
public Video topVideo() {
return videos.get(0);
}
public List<Video> allVideos() {
return videos;
}
}联邦服务2:
name.graphql
type Name @key(fields: "nameId") {
nameId: String!
displayName: String
}
type Query {
getByNameId(nameId: String): Name
getAllNames: [Name]
}用于name.graphql的解析器
@Service
public class NameQuery implements GraphQLQueryResolver {
private static final List<Name> names = Lists.newArrayList(
Name.builder().nameId("nm0000001").displayName("Pam Beesley").build(),
Name.builder().nameId("nm0000002").displayName("Dwight Schrute").build(),
Name.builder().nameId("nm0000003").displayName("Michael Scott").build()
);
public Name getByNameId(final String nameId) {
final Optional<Name> oName = names.stream().filter(name -> name.getNameId().equalsIgnoreCase(nameId))
.findFirst();
return oName.orElse(null);
}
public List<Name> getAllNames(final DataFetchingEnvironment dataFetchingEnvironment) {
return names;
}
}我能够通过网关topVideo调用两个服务类型查询中的所有API( allVideos、getByNameId、getAllNames),而不存在任何问题。
但是,当我在视频类型模式中扩展名称类型时,通过向video.graphql模式添加以下内容
type Name @key(fields: "nameId") @extends {
nameId: String! @external
getVideoByNameId: [Video]
}我不知道如何为getVideoByNameId字段编写解析器。
我尝试过将方法 getVideoByNameId (String nameId)添加到VideoQuery.java (从getVideoByNameId方法返回硬编码视频对象),但图形总是返回null。
我还尝试使用下面的代码创建RuntimeWiring,然后在创建GraphQLSchema对象时传递它,如其github页面上的一个示例所示
private static RuntimeWiring getRuntimeWiring(){
return RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Name")
.dataFetcher("getVideoByNameId", new StaticDataFetcher(someVideo)))
.type(newTypeWiring("Query")
.dataFetcher("topVideo", new StaticDataFetcher(someVideo)))
.type(newTypeWiring("Query")
.dataFetcher("allVideos", new StaticDataFetcher(someVideo)))
.build();
}似乎什么都起不到作用。任何帮助都是非常感谢的。
发布于 2020-01-27 08:41:18
您需要为您的fetchEntities类型实现Name和resolveEntityType,类似于它们实现这里的方式。
https://stackoverflow.com/questions/59926668
复制相似问题