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

dil*_*280 5 graphql graphql-java

目前我们正在使用

    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>${graphql-spring-starter.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

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

Ken*_*han 6

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

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

要点是要Servlet在 spring boot 中注册 a ,您可以简单地创建一个ServletRegistrationBean包装HttpServlet您要创建的。有关更多详细信息,请参阅文档

一个简单的例子是:

@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;
} 
Run Code Online (Sandbox Code Playgroud)