gpt4 book ai didi

java - 如何正确使用Java中Apollo客户端的RxJava2库进行同步调用?

转载 作者:行者123 更新时间:2023-12-01 16:39:42 25 4
gpt4 key购买 nike

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 cannot be cast tocom.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);
}

关于java - 如何正确使用Java中Apollo客户端的RxJava2库进行同步调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61885925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com