Is there a way to expose 2 graphql endpoints using spring boot starter app graphql-spring-boot-starter?

↘锁芯ラ 提交于 2020-06-17 22:54:37

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/62202051/is-there-a-way-to-expose-2-graphql-endpoints-using-spring-boot-starter-app-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!