简体   繁体   English

Graphql 联邦在扩展关系上返回 null

[英]Graphql federation returning null on extended relation

I'm trying to setup a prototype for using graphql across multiple java microservices, which requires me to join multiple graphql schema's into one.我正在尝试设置一个原型,以便在多个 java 微服务中使用 graphql,这需要我将多个 graphql 模式合并为一个。

I'm using 2 java-services and the ApolloServer with ApolloGateway;我正在使用 2 个 java 服务和带有 ApolloGateway 的 ApolloServer; which shows the following schema in the playground:它在操场上显示了以下架构:

type Client {
  id: ID!
  name: String
  linkeduser: User
}

type Query {
  user(id: ID!): User
  users: [User]
  client(id: ID!): Client
  clients: [Client]
}

type User {
  id: ID!
  name: String
}

When running the simple query:运行简单查询时:

query client {
  client(id: 1) {
    id
    name
    linkeduser {
      id
      name
    }
  }
}

What I expect this to return is a client with a linkeduser;我希望这会返回一个带有链接用户的客户端; When debugging the client service gets queried, the user service gets queried, yet the response is:调试时查询客户端服务,查询用户服务,但响应为:

{
  "data": {
    "client": {
      "id": "1",
      "name": "Bob",
      "linkeduser": null
    }
  }
}

How do I get a linked user response in my client?如何在我的客户端中获得链接的用户响应?

I've tried returning lists of users, a new client object with a list of linkedusers, a single user.我试过返回用户列表,一个新客户端 object 和一个链接用户列表,一个用户。 The example of https://github.com/apollographql/federation-jvm is the base of this code, though I've yet to see this working. https://github.com/apollographql/federation-jvm的示例是此代码的基础,尽管我还没有看到它有效。

Code:代码:

Service 1: Client服务一:客户端


@WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {
    @Override
    protected GraphQLConfiguration getConfiguration() {
        return GraphQLConfiguration.with(getGraphQLSchema()).build();
    }

    private static GraphQLSchema getGraphQLSchema() {
        InputStream inputStream = client.GraphQLService.class
            .getClassLoader().getResourceAsStream("schema.graphqls");
        TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
        RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getClient))
            .build();
        return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
            .fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
                .stream()
                .map(values -> {
                    if ("Client".equals(values.get("__typename"))) {
                        final Object id = values.get("id");
                        if (id instanceof String) {
                            return getSingleClient((String) id);
                        }
                    }
                    return null;
                })
                .collect(Collectors.toList()))
            .resolveEntityType(env -> {
                final Object src = env.getObject();
                if (src instanceof Client) {
                    return env.getSchema().getObjectType("Client");
                }
                return null;
            }).build();
    }

    private static Object getClient(DataFetchingEnvironment environment) {
        switch (environment.getFieldDefinition().getName()) {
            case "client":
                return getSingleClient(environment.getArgument("id"));
            case "clients":
                return getAllClients();
            default:
                return null;
        }
    }

    //... extra code with simple getters
}

With this schema:使用此架构:

extend type Query {
    client(id: ID!): Client
    clients: [Client]
}

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

Service 2: User服务 2:用户


@WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {

    @Override
    protected GraphQLConfiguration getConfiguration() {
        return GraphQLConfiguration.with(getGraphQLSchema()).build();
    }

    private static GraphQLSchema getGraphQLSchema() {
        InputStream inputStream = user.GraphQLService.class
            .getClassLoader().getResourceAsStream("schema.graphqls");
        TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
        RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getUser))
            .build();
        return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
            .fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
                .stream()
                .map(values -> {
                    if ("Client".equals(values.get("__typename"))) {
                        final Object id = values.get("id");
                        if (id instanceof String) {
                            return getSingleUser((String) id);
                        }
                    }
                    return null;
                })
                .collect(Collectors.toList()))
            .resolveEntityType(env -> {
                final Object src = env.getObject();
                if (src instanceof User) {
                    return env.getSchema().getObjectType("User");
                }
                return null;
            })
            .build();
    }

    private static Object getUser(DataFetchingEnvironment environment) {
        switch (environment.getFieldDefinition().getName()) {
            case "user":
                return getSingleUser(environment.getArgument("id"));
            case "users":
                return getAllUsers();
            default:
                return null;
        }
    }
    //... extra code with simple getters
}

With this schema:使用此架构:

type Query @extends{
    user (id: ID!): User
    users: [User]
}

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

type Client @key(fields: "id") @extends{
    id: ID! @external
    linkeduser : User
}

Version in POM.xml POM.xml 中的版本

<graphql.version>14.0</graphql.version>
<graphql-tools.version>5.2.4</graphql-tools.version>
<graphql-servlet.version>9.0.1</graphql-servlet.version>
<graphql-federation-support.version>0.4.0</graphql-federation-support.version>

In user service, you need to return a pojo of the type client, with a getter for a linkeduser (only the extends fields need to be present) :在用户服务中,您需要返回一个客户端类型的 pojo,以及一个用于链接用户的 getter (只需要存在扩展字段)

if ("Client".equals(values.get("__typename"))) {
    final Object id = values.get("id");
    if (id instanceof String) {
        return new Client((String) id, getSingleUser((String) id));
    }
}

Also the resolveTypeEntity needs to resolve to said client resolveTypeEntity 也需要解析到所述客户端

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

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