我试图用Netflix编写一个客户端。在开发人员文档中,给出了以下示例代码,用于使用使用DGS编码元生成的类生成查询。
GraphQLQueryRequest graphQLQueryRequest =
new GraphQLQueryRequest(
new TicksGraphQLQuery.Builder()
.first(first)
.after(after)
.build(),
new TicksConnectionProjectionRoot()
.edges()
.node()
.date()
.route()
.name()
.votes()
.starRating()
.parent()
.grade());在这里,所有要查询的字段都是在代码本身中使用TicksConnectionProjectionRoot给出的。
但是,如果我想查询具有许多嵌套类型的15-20个字段,那么这将是令人厌烦的。是否有任何方法来生成graphql请求或像这样的投影,使用包含.graphql查询和所需字段的文件。
发布于 2022-07-26 22:04:07
如果这是您的junit测试,那么我建议直接使用DgsQueryExecutor。
ExecutionResult result = dgsQueryExecutor.execute(getQuery("/graphql/request/query1.txt"));
public static String getQuery(String path) throws IOException {
InputStream stream = MyClass.class.getResourceAsStream(path)
return IOUtils.toString(stream, StandardCharsets.UTF_8);
}query.txt包含客户端通过http请求发送的实际图形查询字符串。
DgsQueryExecutor有多种方法来满足大多数测试需求。
如果通过http进行调用,则可以使用MonoGraphQLClient并直接传递查询字符串。
WebClient webClient = WebClient.create(url);
WebClientGraphQLClient client = MonoGraphQLClient.createWithWebClient(webClient);
//The GraphQLResponse contains data and errors.
Mono<GraphQLResponse> graphQLResponseMono = graphQLClient.reactiveExecuteQuery(query);query是字符串,您可以从资源中读取它。有关客户端的更多信息可以在https://netflix.github.io/dgs/advanced/java-client/和https://fullstackcode.dev/2022/02/10/java-graphql-client-using-netflix-dgs-framework/上找到。
https://stackoverflow.com/questions/71383303
复制相似问题