我有一个 Java 21 Spring Boot 3.3.1 应用程序。这是一个 MVC 应用程序。应用程序发出几个不同的 GraphQL 请求,我目前的模板文件位于:
src
|- main
|- resources
|- graphql-documents
|- templates
| |- templateC.graphql
|- templateA.graphql
|- templateB.graphql
它可以从
graphql-documents/
读取(它是 HttpSyncGraphQlClient 的默认目录)。但是,它不会查看任何子目录。所以它只能得到templateA
和templateB
,不会得到templateC
。
我尝试创建自己的 DocumentSource 类来指向子目录,但它不起作用:
@Configuration
public class HttpConfig {
@Bean
public RestClient getRestClient() {
return RestClient.create();
}
@Bean
public HttpSyncGraphQlClient getGraphQlClient(RestClient restClient) {
return HttpSyncGraphQlClient.builder(restClient).documentSource(new GraphQlDocumentSource()).build();
}
private static class GraphQlDocumentSource implements DocumentSource {
private final ResourceDocumentSource delegate;
public GraphQlDocumentSource() {
this.delegate = new ResourceDocumentSource(
Collections.singletonList(new ClassPathResource("graphql-documents/**")),
ResourceDocumentSource.FILE_EXTENSIONS);
}
@Override
public @NonNull Mono<String> getDocument(@NonNull String name) {
return delegate.getDocument(name);
}
}
}
我尝试过
graphql-documents
(这只是默认组合)、graphql-documents/
、graphql-documents/**
、graphql-documents/**/*
的组合,但它们都不会在子目录中查找。
如何让它搜索
.graphql
目录下所有目录下的graphql-documents
文件?
我认为你可以通过以下方式实现这一目标:
@Configuration
public class HttpConfig {
@Bean
public RestClient getRestClient() {
return RestClient.create();
}
@Bean
public HttpSyncGraphQlClient getGraphQlClient(RestClient restClient) {
List<Resource> folders= List.of(new ClassPathResource("graphql-documents/"),
new ClassPathResource("graphql-documents/templates/"));
ResourceDocumentSource source = new ResourceDocumentSource(folders,
ResourceDocumentSource.FILE_EXTENSIONS);
return HttpSyncGraphQlClient.builder(restClient).documentSource(source).build();
}
}