2

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

我正在使用 2 个 java 服务和带有 ApolloGateway 的 ApolloServer;它在操场上显示了以下架构:

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

我希望这会返回一个带有链接用户的客户端;调试时查询客户端服务,查询用户服务,但响应为:

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

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

我尝试返回用户列表、带有链接用户列表的新客户端对象、单个用户。https://github.com/apollographql/federation-jvm的示例是此代码的基础,尽管我还没有看到它有效。

代码:

服务一:客户端


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

1 回答 1

2

在用户服务中,您需要返回一个客户端类型的 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 也需要解析到所述客户端

于 2020-05-23T11:45:19.677 回答