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

Graphql federation returning null on extended relation

我正在尝试设置一个跨多个 java 微服务使用 graphql 的原型,这需要我将多个 graphql 模式合并为一个。

我正在使用 2 个 java 服务和带有 ApolloGateway 的 ApolloServer;在 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
}

当运行简单查询时:

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

我希望这个 return 是一个有 linkeduser 的客户端;调试时查询客户端服务,查询用户服务,但响应为:

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

如何在我的客户端中获得链接的用户响应?

我试过 returning 用户列表,一个带有链接用户列表的新客户端对象,一个用户。 https://github.com/apollographql/federation-jvm 的示例是此代码的基础,但我还没有看到它起作用。

代码:

服务 1:客户


@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
}

使用此架构:

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

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

服务 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
}

使用此架构:

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
}

版本在 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>

在用户服务中,您需要 return 客户端类型的 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));
    }
}

还需要将 resolveTypeEntity 解析为所述客户端