在 Spring Boot 应用程序中,我们已经拥有一个功能齐全的 GraphQL 端点,.graphqls
通过 GraphQL Java 工具(我们包含graphql-spring-boot-starter
依赖项)提供文件,并通过我们的基Query
类实现GraphQLQueryResolver
和后续GraphQLResolver
的处理数据解析。
对于业务需求,我们必须重新创建标准 REST API 端点,所以我想知道为什么不直接调用 GraphQL(而不是再次“手动”重新实现数据解析)?由于它在同一个后端应用程序中,无需进行 HTTP 或 servlet (ForwardRequest) 调用,只需调用 Java 中的一些 API。问题是我不知道如何进行。
我读了这个例子,但它是基本的 GraphQL Java(不是工具): https ://www.graphql-java.com/documentation/v9/execution/
我知道这应该是可能的,因为我们可以在测试中这样做: https ://github.com/graphql-java-kickstart/graphql-spring-boot/blob/master/example-graphql-tools/src/test/java /com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java
但是如何在常规代码中做到这一点?没有GraphQLTemplate
.
我还尝试在以下位置搜索示例:
- https://github.com/graphql-java-kickstart/graphql-java-tools/tree/master/example
- https://github.com/graphql-java-kickstart/graphql-spring-boot
但没有发现任何与我们的需要相关的东西。
在文档中找不到更多内容:
我错过了什么?理想情况下,我希望注入一些GraphQLSomething
这样的:
@RestController
@RequestMapping(path = "api")
public class CompanyController {
@Autowired
private GraphQLSomething graphQLSomething;
@GetMapping("company/{id}")
public ResponseEntity<?> societe(@PathVariable @NotNull Integer id) {
GraphQLSomethingResult result = GraphQLSomething.query("company(id: $id) { id name andsoone }", "{ \"id\": 123456 }").execute(); // not really sure of the GraphQL syntax here, but it'll need some tests...
return result.getDataOrElse();
}
}