【问题标题】:Is there a way to expose 2 graphql endpoints using spring boot starter app graphql-spring-boot-starter?有没有办法使用spring boot starter app graphql-spring-boot-starter公开2个graphql端点?
【发布时间】:2020-09-23 21:01:08
【问题描述】:

目前我们正在使用

    <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,这样我就可以根据端点定义不同的响应格式。最好的方法是什么?任何意见都非常感谢。

【问题讨论】:

    标签: graphql graphql-java


    【解决方案1】:

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

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

    主要的一点是,要在 spring boot 中注册一个 Servlet,您可以简单地创建一个 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;
    } 
    

    【讨论】:

    • cors 设置怎么样?
    猜你喜欢
    • 2021-12-13
    • 2021-06-01
    • 2020-09-16
    • 2014-04-07
    • 2019-06-12
    • 2016-01-29
    • 1970-01-01
    • 2021-09-10
    • 2015-07-07
    相关资源
    最近更新 更多