graphql-java-generator / graphql-maven-plugin-project

graphql-maven-plugin is a Maven Plugin for GraphQL, based on graphql-java. It accelerates the development for both the client and the server, by generating the Java code. It allows a quicker development when in contract-first approach, by avoiding to code the boilerplate code.
https://graphql-maven-plugin-project.graphql-java-generator.com
MIT License
115 stars 47 forks source link

Client application fails with springContectBean instantiation error. #206

Closed amar-shinde closed 6 months ago

amar-shinde commented 6 months ago

Hello,

I'm using graphql-maven-plugin for client code generation, i.e. only the domain Objects those are used in queries & response from a GraphQL server. I've saved the queries and graphql schema locally which I'm reading to generate the client code.

Following is mostly what my pom.xml looks like. Mentioned only necessary part here for this issue.

` <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.testproduct</groupId>
<artifactId>etest-product-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>etest-product-api</name>
<description>APIs to fetch Product Information</description>

<properties>
    <java.version>17</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    <org.mapstruct.version>1.5.1.Final</org.mapstruct.version>
    <org.projectlombok.version>1.18.24</org.projectlombok.version>
    <org.lombok-mapstruct-binding.version>0.2.0</org.lombok-mapstruct-binding.version>
    <springdoc.version>2.0.0</springdoc.version>
        <graphql.version>7.0.1</graphql.version>
        <graphql-java-tools.version>6.2.0</graphql-java-tools.version>
        <graphql-maven-plugin.version>2.3.2</graphql-maven-plugin.version>
        <skipTests>true</skipTests> 
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.7.0</version>
    </dependency>
    <!-- graphql -->  
    <dependency>
        <groupId>com.graphql-java-generator</groupId>
        <artifactId>graphql-java-client-runtime</artifactId>
        <version>${graphql-maven-plugin.version}</version>          
    </dependency>       
    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-java-servlet</artifactId>
        <version>15.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-spring-boot-autoconfigure</artifactId>
        <version>15.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>         
    </dependency>
</dependencies>

<build>     
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <skipTests>${skipTests}</skipTests>
            </configuration>
        </plugin>           
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>           
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${org.projectlombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <!-- GraphQL Maven plugin -->             
        <plugin>
            <groupId>com.graphql-java-generator</groupId>
            <artifactId>graphql-maven-plugin</artifactId>
            <version>${graphql-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generateClientCode</goal>
                    </goals>
                    <configuration>
                        <packageName>com.testproduct.graphql.client</packageName>
                        <schemaFileFolder>${basedir}/src/main/resources/graphql</schemaFileFolder>
                        <customScalars>
                            <customScalar>
                                <graphQLTypeName>Date</graphQLTypeName>
                                <javaType>java.util.Date</javaType>
                                <graphQLScalarTypeStaticField>com.graphql_java_generator.customscalars.GraphQLScalarTypeDate.Date</graphQLScalarTypeStaticField>
                            </customScalar>
                            <customScalar>
                                <graphQLTypeName>DateTime</graphQLTypeName>
                                <javaType>java.util.Date</javaType>
                                <graphQLScalarTypeStaticField>com.graphql_java_generator.customscalars.GraphQLScalarTypeDateTime.DateTime</graphQLScalarTypeStaticField>
                            </customScalar>
                            <customScalar>
                                <graphQLTypeName>Upload</graphQLTypeName>
                                <javaType>javax.servlet.http.Part</javaType>
                                <graphQLScalarTypeStaticField>graphql.kickstart.servlet.apollo.ApolloScalars.Upload</graphQLScalarTypeStaticField>
                            </customScalar>
                        </customScalars>
                        <!-- The parameters below change the 1.x default behavior to respect the future 2.x behavior -->
                        <copyRuntimeSources>false</copyRuntimeSources>
                        <generateDeprecatedRequestResponse>false</generateDeprecatedRequestResponse>
                        <separateUtilityClasses>true</separateUtilityClasses>
                        <skipGenerationIfSchemaHasNotChanged>true</skipGenerationIfSchemaHasNotChanged>
                    </configuration>
                </execution>
            </executions>               
        </plugin>
    </plugins>
</build>

`

The code compiles well, but the Springboot Application fails to start with following error:

Field springContextBean in com.testproduct.graphql.client.spring_autoconfiguration.GraphQLPluginAutoConfiguration required a bean of type 'com.graphql_java_generator.client.SpringContextBean' that could not be found. The injection point has the following annotations:

Is it something that I'm missing in my project dependencies? Or does it need to be addressed in some way?

Your help is much appreciated.

Thanks & Warm Regards, Amar

etienne-sf commented 6 months ago

Hello,

The issue is probably due to the spring context's configuration.

My opinion is that the GraphqlClientUtils.class is missing in the packages list that is scanned by Spring.

If not, can you show the way your Spring context is configured ?

Etienne