有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试为跨多个java微服务使用graphql建立一个原型,这需要我将多个graphql模式连接到一个

我正在使用2个java服务和ApolloServer以及ApolloGateway;其中显示了操场中的以下模式:

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

我希望它能回报一个拥有linkeduser的客户;当调试客户端服务时,用户服务被查询,但响应是:

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

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

我试着返回用户列表,一个新的客户端对象和一个linkedusers列表,一个用户。 {a1}的例子是这段代码的基础,尽管我还没有看到它的工作原理

代码:

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

共 (1) 个答案

  1. # 1 楼答案

    在用户服务中,需要返回client类型的pojo,并为linkeduser返回一个getter(只需要存在extends字段)

    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还需要解析到所述客户端