背景
我的spring-boot-3应用程序需要从外部GraphQL API获取数据。在那里我可以/需要通过4个不同的查询获取数据。我想找到一些通用的GraphQL客户端,具有默认的GraphQL异常处理和默认的响应转换为对象。
提问
有没有简单的方法来调用GraphQL API?有没有依赖项或客户端?
我现在的实现
GraphQLQueryLoader.class
@Slf4j
@RequiredArgsConstructor
@Component
public class GraphQLQueryLoader {
private final ResourceLoader resourceLoader;
/**
* Query file should be in
* <code>/resources/graphql/{fileName}.graphql</code>
* */
public String loadQuery(final String queryName) {
try {
final var location = "classpath:graphql/" + queryName + ".graphql";
final var path = resourceLoader.getResource(location).getFile().getPath();
return Files.readString(Paths.get(path));
} catch (final Exception e) {
log.error(String.format("Could not load GraphQL query (%s).", queryName), e);
throw new GraphQLQueryException();
}
}
}
字符串
GraphQLClient.class
@Slf4j
@RequiredArgsConstructor
public class GraphQLClient {
private static final Duration TIMEOUT = Duration.ofSeconds(30);
private final WebClient webClient;
private final GraphQLQueryLoader graphQLQueryLoader;
public GraphQLResponse getByQueryName(final String query) {
final var query = graphQLQueryLoader.loadQuery(query);
return webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(query))
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, handleClientError())
.onStatus(HttpStatusCode::is5xxServerError, handleServerError())
.bodyToMono(GraphQLResponse.class)
.block(TIMEOUT);
}
private Function<ClientResponse, Mono<? extends Throwable>> handleClientError() {
return response -> response.bodyToMono(String.class)
.flatMap(body -> {
log.warn("Client error with body: {}", body);
return Mono.error(new HttpClientErrorException(response.statusCode(), "Client error"));
});
}
private Function<ClientResponse, Mono<? extends Throwable>> handleServerError() {
return response -> response.bodyToMono(String.class)
.flatMap(body -> {
log.warn("Server error with body: {}", body);
return Mono.error(new HttpServerErrorException(response.statusCode(), "Server error"));
});
}
}
型
GraphQLResponse.class
public record GraphQLResponse(Object data) {}
型
resources/graphql/my-query.graphql
query myQuery() {
myQuery(id: '123') {
myCollection {
items {
title
body
action
}
}
}
}
型
用法
final var response = client.getByQueryName("my-query");
型
1条答案
按热度按时间13z8s7eq1#
graphql-java-kickstart
,这个库提供了一种在Java中使用GraphQL的方便方法。您可以参考官方文档了解更多详细信息:graphql-java-kickstart。