如何在 Java 中正确使用 Apollo Client 的 RxJava2 库进行同步调用?

How can I use the RxJava2 library for the Apollo Client in Java properly for synchronous calls?

RxJava2 is available for the Apollo GraphQL JVM。但我可以弄清楚如何正确使用它。我尝试同步使用它:

/**
 * This method is supposed to send an Apollo Call, map the data of the Responses into Optionals and 
 * give back the Optional<Data> object.
 * @param <T>: the build query
 * @param <V>: the expected data structure
 *
*/
    public static <T extends com.apollographql.apollo.api.Query> Optional<Data> execute(
            T operation) {
        ApolloClient client = new CommonClient().getClient();
        ApolloCall<Data> apolloCall = client.query(operation);
        return Rx2Apollo.from(apolloCall)
                .map(value -> Optional.of(value.data()))
                .onErrorReturn(o ->  {
                  logger.error(o.getMessage());
                  return Optional.empty();
                })
                .blockingFirst();
    }

但问题是我在 .map(value -> Optional.of(value.data())) 行收到错误。错误是:

java.util.Optional cannot be cast to com.example.graphql.client.KundeQuery$Data

所以,我做错了什么?或者至少有一种更简单的方法来同步处理 Apollo GraphQL JVM 客户端中的数据?

我已经知道了。传入的数据结构是 Response> 而不是 >。所以它无法读取它。解决方案可能是这样的:

public static <T extends com.apollographql.apollo.api.Query> Optional<Data> execute(T operation) {

    ApolloClient client = new CommonClient().getClient();
    ApolloCall<Optional<Data>> apolloCall = client.query(operation);

    return Rx2Apollo.from(apolloCall)
            .map(IncomingResponse::extracted)
            .onErrorReturn(o -> {
                logger.error(o.getMessage());
                return Optional.empty();
                })
            .blockingFirst();
}

private static Optional<Data> extracted(Response<Optional<Data>> value) {
    Optional<Data> result = value.data();
    return result; //Optional.of(localData);
}