graphql - 有没有办法使用 spring boot starter 应用程序 graphql-spring-boot-starter 公开 2 个 graphql 端点?

标签 graphql graphql-java

目前我们正在使用

    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>${graphql-spring-starter.version}</version>
    </dependency>

有了这个,我们将使用/graphql 端点公开我们的 graphql API。我想要多个这样的端点,/graphql1 和/graphql2,以便我可以根据端点定义不同的响应格式。最好的方法是什么?任何输入都受到高度赞赏。

最佳答案

归结起来就是创建一个 GraphQLHttpServlet并配置其上下文路径。在封面下,它使用自动配置GraphQLWebAutoConfiguration定义一个 GraphQLHttpServlet作为bean,并将上下文路径配置为/graphql .

这意味着您可以引用 GraphQLWebAutoConfiguration并创建另一个 GraphQLHttpServlet注册到其他上下文路径的实例。

重点是要注册一个Servlet在 spring boot 中,你可以简单地创建一个 ServletRegistrationBean包装 HttpServlet您要创建的内容。请参阅 docs更多细节。

一个简单的例子是:

@Bean
public ServletRegistrationBean<AbstractGraphQLHttpServlet> fooGraphQLServlet() {
    //Create and configure the GraphQL Schema.
    GraphQLSchema schema = xxxxxxx;

    GraphQLHttpServlet graphQLHttpServlet = GraphQLHttpServlet.with(schema);
    ServletRegistrationBean<AbstractGraphQLHttpServlet> registration = new ServletRegistrationBean<>(
                    graphQLHttpServlet, "/graphql2/*");

    registration.setName("Another GraphQL Endpoint");
    return registration;
} 

关于graphql - 有没有办法使用 spring boot starter 应用程序 graphql-spring-boot-starter 公开 2 个 graphql 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62202051/

相关文章:

reactjs - Gatsby (Gatsby)和斯特拉皮(Strapi)实现国际化的最佳途径

error-handling - 如何消耗 Vue Graphql 组件中的错误并让其他错误全局处理?

android - Apollo Android 中的多个模式

graphql - 我是否应该在 graphql 模式文件中将每个对象写为 'input' 和 'type' 两次

javascript - 获取内容时 graphql 查询中出现未知类型错误?

node.js - 订阅 GraphQL Server 时出错

arangodb - 将 GraphiQL 与 Foxx 结合使用

java - Graphql 在 Postman 中使用变量作为文本发送 POST

java - 如何通过Graphql schema生成java实体

java - GraphQL 是否支持 websockets?