首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Spring进行聚合的Graphql

使用Spring进行聚合的Graphql
EN

Stack Overflow用户
提问于 2017-10-03 07:13:37
回答 1查看 1.2K关注 0票数 0

我的应用程序构建了REST API,我们计划使用GraphQL。我想知道是否有任何文档或在线参考简要介绍了Spring与GraphQL在服务器端的集成。有什么需要帮忙的吗?

EN

回答 1

Stack Overflow用户

发布于 2017-10-03 22:54:24

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

有关使用graphql-spqr的graphql-java的最小(但相当完整) Spring Boot示例,请参见https://github.com/leangen/graphql-spqr-samples

简而言之,您将创建一个普通控制器,在其中创建GraphQL模式并初始化运行时,并公开一个端点以接收查询。

代码语言:javascript
复制
@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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46534987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档