我的应用程序构建了REST API,我们计划使用GraphQL。我想知道是否有任何文档或在线参考简要介绍了Spring与GraphQL在服务器端的集成。有什么需要帮忙的吗?
发布于 2017-10-03 22:54:24
你的问题太宽泛了,无法回答。任何GraphQL客户端都可以与任何GraphQL服务器一起工作,并且服务器可以使用任何框架堆栈来实现,因为GraphQL只是API层。
有关使用graphql-spqr的graphql-java的最小(但相当完整) Spring Boot示例,请参见https://github.com/leangen/graphql-spqr-samples
简而言之,您将创建一个普通控制器,在其中创建GraphQL模式并初始化运行时,并公开一个端点以接收查询。
@RestController
public class GraphQLSampleController {
private final GraphQL graphQL;
@Autowired
public GraphQlSampleController(/*Inject the services needed*/) {
GraphQLSchema schema = ...; //create the schema
graphQL = GraphQL.newGraphQL(schemaFromAnnotated).build();
}
//Expose an endpoint for queries
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Object endpoint(@RequestBody Map<String, Object> request) {
ExecutionResult executionResult = graphQL.execute((String) request.get("query"));
return executionResult;
}
}这是最低要求。有关使用graphql-java-tools但不使用Spring的完整教程,请查看the Java track on HowToGraphQL。
https://stackoverflow.com/questions/46534987
复制相似问题