简体   繁体   English

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

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

I am quite new to Apollo Federation and Gateway and trying to use apollo federation-jvm for a demo project.我对 Apollo Federation 和 Gateway 还是很陌生,并尝试将apollo federation-jvm用于演示项目。 I have created two federated services using federation jvm.我使用联合 jvm 创建了两个联合服务。 Both these services are connected to each other via Apollo Gateway.这两个服务都通过 Apollo 网关相互连接。 Using this node gateway example from their official github page.使用来自他们官方 github 页面的这个节点网关示例。

Below is the schema and resolvers for these services:以下是这些服务的架构和解析器:

Federated Service 1:联合服务 1:

video.graphql视频.graphql

type Video @key(fields: "videoId") {
    videoId: String!
    description: String
    relatedNameId: String
}

type Query {
    topVideo: Video
    allVideos: [Video]
}

Resolvers for video.graphql 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;
    }
}

Federated service 2:联合服务2:

name.graphql名称.graphql

type Name @key(fields: "nameId") {
    nameId: String!
    displayName: String
}

type Query {
    getByNameId(nameId: String): Name
    getAllNames: [Name]
}

Resolvers for name.graphql 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;
    }

}

I am able to call all the API's (topVideo, allVideos, getByNameId, getAllNames) in Type Query of both the services via gateway without any issues.我能够通过网关在两个服务的类型查询中调用所有 API(topVideo、allVideos、getByNameId、getAllNames),没有任何问题。

However when I extend the Name Type in Video Type schema by adding the following to the video.graphql schema但是,当我通过将以下内容添加到 video.graphql 架构来扩展Video Type 架构中的Name Type 时

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

I am not sure how to write the resolver for getVideoByNameId field.我不确定如何为getVideoByNameId字段编写解析器。

I have tried adding method getVideoByNameId(String nameId) to VideoQuery.java (returning hardcoded video object from getVideoByNameId method) but the graph always returns null.我曾尝试将方法getVideoByNameId(String nameId) 添加到 VideoQuery.java(从 getVideoByNameId 方法返回硬编码的视频对象),但该图始终返回 null。

I have also tried to create RuntimeWiring using below code and then passing that when creating GraphQLSchema object as shown in an example on their github page我还尝试使用以下代码创建 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();
    }

Nothing seems to work.似乎没有任何效果。 Any help is highly appreciated.任何帮助都受到高度赞赏。

您需要为您的Name类型实现fetchEntitiesresolveEntityType ,类似于它们在此处的实现方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM