RxJava2可用于 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 不能转换为 com.example.graphql.client.KundeQuery$Data
那么,我做错了什么?或者至少有一种更简单的方法来同步处理 Apollo GraphQL JVM 客户端中的数据?