GraphQL Apollo Federation-JVM 中@extends 类型的解析器问题

Resolver Issue for @extends Type in GraphQL Apollo Federation-JVM

我对 Apollo Federation 和 Gateway 还很陌生,正在尝试使用他们官方 github 页面中的 apollo federation-jvm for a demo project. I have created two federated services using federation jvm. Both these services are connected to each other via Apollo Gateway. Using this 节点网关示例。

以下是这些服务的模式和解析器:

联合服务 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;
    }

}

我可以通过网关 在两种服务的类型查询中调用所有 API(topVideo、allVideos、getByNameId、getAllNames),没有任何问题.

然而,当我扩展 Name Type in Video Type schema 通过将以下内容添加到 video.graphql schema

type Name @key(fields: "nameId") @extends {
    nameId: String! @external
    getVideoByNameId: [Video]
}

我不确定如何为 getVideoByNameId 字段编写解析器。

我已尝试将方法 getVideoByNameId(String nameId) 添加到 VideoQuery.java(从 getVideoByNameId 方法返回硬编码视频对象)但图表总是 returns null .

我还尝试使用以下代码创建 RuntimeWiring,然后在创建 GraphQLSchema 对象时传递它,如他们 github page

上的示例所示
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();
    }

似乎没有任何效果。非常感谢任何帮助。

您需要为您的 Name 类型实施 fetchEntitiesresolveEntityType,其实施方式与 here.

的实施方式类似