本文介绍了有没有办法使用Spring Boot Starter应用程序graphql-spring-boot-starter公开2个graphql端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我们正在使用

 < 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 ./p>

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

要点是要在Spring Boot中注册 Servlet ,您只需创建一个 ServletRegistrationBean 即可,它包装了所需的 HttpServlet 创建.请参见文档以获取更多详细信息.

一个简单的例子是:

  @Bean公共ServletRegistrationBean< AbstractGraphQLHttpServlet>fooGraphQLServlet(){//创建并配置GraphQL模式.GraphQLSchema模式= xxxxxxx;GraphQLHttpServlet graphQLHttpServlet = GraphQLHttpServlet.with(schema);ServletRegistrationBean< AbstractGraphQLHttpServlet>注册=新的ServletRegistrationBean<>(graphQLHttpServlet,"/graphql2/*");registration.setName(另一个GraphQL端点");返还注册;} 

Currently we are using

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

With this, we are exposing our graphql API using /graphql endpoint. I want to have multiple endpoints like this, /graphql1 and /graphql2 so that I can define different response formats based on the endpoints. what is the best way to do it? Any inputs is highly appreciated.

解决方案

It just boils down to create a GraphQLHttpServlet and configure its context path. Under the cover , it uses auto-configuration GraphQLWebAutoConfiguration to define a GraphQLHttpServlet as a bean, and configure the context path to be /graphql.

That means you can reference how GraphQLWebAutoConfiguration does and create another GraphQLHttpServlet instance that registered to other context path.

The main point is that to register a Servlet in spring boot , you can simply create a ServletRegistrationBean that wraps the HttpServlet which you want to create .See docs for more details.

A simple example is :

@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;
}

这篇关于有没有办法使用Spring Boot Starter应用程序graphql-spring-boot-starter公开2个graphql端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:37