0

我的应用程序构建了 REST API,我们计划使用 GraphQL。想知道是否有任何文档或任何在线参考资料简要介绍了 GraphQL Apollo 与 Spring 在服务器端的集成。请提供任何帮助。

4

1 回答 1

1

你的问题太广泛了,无法回答。任何 GraphQL 客户端都可以与任何 GraphQL 服务器一起使用,并且服务器可以使用任何框架堆栈来实现,因为 GraphQL 只是 API 层。

有关使用 graphql-java 的最小(但非常完整)Spring Boot 示例,使用graphql-spqr,请参阅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 的完整教程,请查看HowToGraphQL 上的 Java 跟踪

于 2017-10-03T14:54:23.617 回答