[英]Generating projection using graphql file for DGS client
我正在尝试使用 Netflix DGS 编写一个 java-graphql 客户端。 在开发人员文档中,给出了以下示例代码,用于使用 DGS codegen 生成的类生成查询。
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 请求或使用 a.graphql 文件包含所需的grapqhl查询和字段的投影。
如果是您的 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
是字符串,您可以从资源中读取它。 More about client can be found at https://netflix.github.io/dgs/advanced/java-client/ and https://fullstackcode.dev/2022/02/10/java-graphql-client-using-netflix- dgs-框架/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.