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

标签 java graphql apollo-federation apollo-gateway

我对 Apollo Federation 和 Gateway 很陌生,并尝试使用 apollo federation-jvm一个演示项目。我使用联合 jvm 创建了两个联合服务。这两个服务都通过 Apollo Gateway 相互连接。使用this来自其官方 github 页面的节点网关示例。

以下是这些服务的架构和解析器:

联合服务1:

视频.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:

名称.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),没有任何问题。

但是,当我通过将以下内容添加到 video.graphql 架构中来扩展名称类型中的视频类型架构时

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

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

我尝试将方法 getVideoByNameId(String nameId) 添加到 VideoQuery.java(从 getVideoByNameId 方法返回硬编码视频对象),但图表始终返回 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 .

关于java - GraphQL Apollo Federation-JVM 中 @extends 类型的解析器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926668/

相关文章:

java - Graphql 联合在扩展关系上返回 null

graphql - Apollo 联合网关背后的 Hasura GraphQL 端点

java - 我需要创建一个 10x10 乘法表,但我不断收到错误

java - 从对象中的对象调用对象中的方法

reactjs - 如何在电容器应用程序处于后台时继续运行 apollo 订阅?

node.js - 为什么子字段在不同界面时会发生冲突?

java - 扫描具有未指定文件名的非 txt 文件

java - 最大连续子数组和的线性时间算法

javascript - 在 setVariables 之后 React 继电器挂起的变量总是为 null

docker - apollo网关(federation)无法连接kubernetes环境中的服务