Search code examples
javagraphql-java

GraphQL client server connection


I have a sample spring-boot app to run graphQL server, having graphiql-java as client and my pom has following dependencies :

<dependencies>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-spring-boot-autoconfigure</artifactId>
            <version>5.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java-servlet -->
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-servlet</artifactId>
            <version>6.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>4.3.0</version>
        </dependency>
    </dependencies>

When i try to connect from client i get the following error :

enter image description here

Hunting resolution kindly suggest.

I have a couple of more questions :

  1. Should i be using SimpleGraphQLHttpServlet to route the request to the endpoint ?
  2. I am using apollo-client on React UI so is it mandated to use apollo-server or spring-boot would work ? Any specific customisation on spring-boot side?
  3. If i have multiple resolvers in a spring-boot app then how can i resolve a request to specific resolver ?

Solution

    1. Should i be using SimpleGraphQLHttpServlet to route the request to the endpoint ?

    There is only one GraphQL endpoint , so there is no need to route the request. What you need to make the server listening the GraphQL request at an URL , usually /graphQL. This is equivalent to implement a Servlet listening to this URL in standard Java way. SimpleGraphQLHttpServlet is one of this Servlet implementation from graphql-java-servlet if you do not want to implement it by yourself. The alternatively is graphql-java-spring if you are using Spring.

    1. I am using apollo-client on React UI so is it mandated to use apollo-server or spring-boot would work ? Any specific customisation on spring-boot side?

    Assuming you are only using GraphQL features that are compliance with the GraphQL specification , apollo-client should work with any GraphQL severs. So it does not matter the server is implemented using apollo-server , graphql-java or whatever languages.

    1. If i have multiple resolvers in a spring-boot app then how can i resolve a request to specific resolver ?

    Every field in every GraphQL type should have a resolver configured for them to resolve its value. Every GraphQL query will start resolving from a field of root query/mutation/subscription type.In graphql-java , you can configure a field of a type using which resolver by configure TypeRuntimeWiring or WiringFactory (see this for more detail).