graphql-java-generator / graphql-gradle-plugin-project

graphql-gradle-plugin is a Gradle 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
54 stars 8 forks source link

Field graphQlClient in de.mine.generated.util.MutationsExecutor required a bean of type 'org.springframework.graphql.client.GraphQlClient' that could not be found. #22

Closed Erando89 closed 3 months ago

Erando89 commented 6 months ago

Hi Étienne, thank you for your hard work on this project. I followed your Client Gradle tutorial and ran in some problems which I'm not sure you're aware of.

I used your plugin successfully to generate a client of a GraphQL API (using Spring Boot 2.4.4 btw). I copied all generated files into my project and added one dependency:

implementation "com.graphql-java-generator:graphql-java-client-runtime:2.4"

Thus all files could get compiled. When starting the application though I get this exception and the app terminates:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field graphQlClient in de.mine.generated.util.MutationsExecutor required a bean of type 'org.springframework.graphql.client.GraphQlClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    - @org.springframework.beans.factory.annotation.Qualifier("httpGraphQlClient")

Action:

Consider defining a bean of type 'org.springframework.graphql.client.GraphQlClient' in your configuration.

Thank you for looking into this.

Best regards

etienne-sf commented 6 months ago

Hum,

If I understand well, you executed these steps:

Right ?

Then did you also copy the generated resources folder ? It contains an autoconfiguration spring files, that is mandatory to make this work.

Etienne

etienne-sf commented 6 months ago

Any update ?

thomson90 commented 3 months ago

Hi Etienne,

during a migration from Spring Boot 2 to 3, I am currently upgrading from version 1.18.9 to 2.4. I have meticulously followed your migration steps and reviewed all example configurations. Unfortunately, I am encountering a similar problem to Erando89. All files could be compiled and code generation works. However, when starting the application, I get this exception:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field springContextBean in de.myCompany.myProject.gitlab.spring_autoconfiguration.GraphQLPluginAutoConfiguration required a bean of type 'com.graphql_java_generator.client.SpringContextBean' that could not be found.

Action:
Consider defining a bean of type 'com.graphql_java_generator.client.SpringContextBean' in your configuration.

This is my build.gradle:

buildscript {
    ext {
        azureVersion = '2.2.5'
        springBootVersion = '3.2.1'
        springCoreVersion = '6.1.8'
        springSecurityVersion = '6.2.4'
        graphQLPluginVersion = '2.4'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-starter-graphql:${springBootVersion}"
    }
}

plugins {
    id "com.graphql-java-generator.graphql-gradle-plugin3" version "${graphQLPluginVersion}"
    id 'idea'
    id 'java'
    id 'org.springframework.boot' version "${springBootVersion}"
    id 'io.spring.dependency-management' version '1.1.5'
    id 'org.owasp.dependencycheck' version '7.4.4'
}

apply from: 'graphql.gradle' //applies generateClientCodeConf 

dependencies {
    implementation "org.springframework.boot:spring-boot:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-security:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-validation:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
   implementation "org.springframework.boot:spring-boot-starter-graphql:${springBootVersion}"

    implementation "com.graphql-java-generator:graphql-java-client-runtime:${graphQLPluginVersion}"

    testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
    testImplementation "org.springframework.security:spring-security-test:${springSecurityVersion}"
    testImplementation "org.junit.jupiter:junit-jupiter-params:5.10.2"
    testImplementation "org.mockito:mockito-junit-jupiter:4.11.0"
}

sourceSets.main.java.srcDirs += '/build/generated/sources/graphqlGradlePlugin'
sourceSets.main.resources.srcDirs += '/build/generated/resources/graphqlGradlePlugin'

The only thing i changed in the code is, to create a graphQlClient...

  @Bean
  public GraphQlClient graphQlClient(WebClient webClient) {
    return HttpGraphQlClient.builder(webClient)
            .build();
  }

Because for some reason, since the version change, the builder no longer only requires the GraphQlRequest.class but also a GraphQlClient to use withQueryResponseDef:

  private ObjectResponse getObjectResponse(String queryResponseDef) throws GraphQLRequestPreparationException {
    return new Builder(graphQlClient, GraphQLRequest.class).withQueryResponseDef(queryResponseDef).build();
  }

It would be great if you could check that out.

Best regards Thomas

etienne-sf commented 3 months ago

Hello,

Thanks to your detailed info, I'll try to repeat this issue.

Currently, I'm having a hard time just compiling the project. My PC seems to be quite reluctant about your gradle conf :(

etienne-sf commented 3 months ago

Yes it definitely doesn't want : I upgraded java to version 21, gradle to 8.8, and I'm back to this error : Could not resolve all artifacts for configuration ':detachedConfiguration1'. I'll keep on testing tomorrow

thomson90 commented 3 months ago

No hurry. If you have found a working combination of a gradle configuration, I would be very happy. It doesn't necessarily have to be the very latest Java, gradle or Spring version.

I am very glad that you want to solve the problem.

In case you need the graphql.gradle to run everything here it is:

generateClientCodeConf {
    schemaFileFolder = 'src/main/resources/graphql/gitlab'

    packageName = 'de.myCompany.myProject.gitlab'

    copyRuntimeSources = false
    generateDeprecatedRequestResponse = false
    separateUtilityClasses = true

    customScalars =
            [
                    [
                            graphQLTypeName             : "AlertManagementHttpIntegrationID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "AwardableID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "BoardID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CiBuildID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CiJobArtifactID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CiPipelineID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CiPipelineScheduleID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CiRunnerID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ClustersAgentID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ClustersAgentTokenID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ClustersClusterID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ContainerRepositoryID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CustomEmojiID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CustomerRelationsContactID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CustomerRelationsOrganizationID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DependencyProxyManifestID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DesignManagementDesignAtVersionID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DesignManagementDesignID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DesignManagementVersionID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DiffNoteID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "DiscussionID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "EnvironmentID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "GitlabErrorTrackingDetailedErrorID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "GlobalID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "GroupID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "IncidentManagementTimelineEventID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "IntegrationsPrometheusID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "IssuableID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "IssueID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "JobID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "CommitStatusID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "LabelID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ListID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "MergeRequestID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "MetricsDashboardAnnotationID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "MilestoneID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "NoteID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "NoteableID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesConanFileMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesConanMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesDependencyID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesDependencyLinkID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesMavenMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesNugetDependencyLinkMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesNugetMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesPackageFileID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesPackageID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "PackagesPypiMetadatumID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ProjectID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ReleaseID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "ReleasesLinkID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "SnippetID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "TerraformStateID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "TimelogID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "TodoID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "TodoableID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "UploadID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "UserID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "UsersSavedReplyID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "IssueID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "WorkItemID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "WorkItemsTypeID",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],

                    [
                            graphQLTypeName             : "BigInt",
                            javaType                    : "java.math.BigInteger",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "Color",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "Date",
                            javaType                    : "java.util.Date",
                            graphQLScalarTypeStaticField: "com.graphql_java_generator.customscalars.GraphQLScalarTypeDate.Date"
                    ],
                    [
                            graphQLTypeName             : "Duration",
                            javaType                    : "java.lang.Float",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLFloat"
                    ],
                    [
                            graphQLTypeName             : "ISO8601Date",
                            javaType                    : "java.util.Date",
                            graphQLScalarTypeStaticField: "com.graphql_java_generator.customscalars.GraphQLScalarTypeDate.Date"
                    ],
                    [
                            graphQLTypeName             : "JSON",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "Time",
                            javaType                    : "java.util.Date",
                            graphQLScalarTypeStaticField: "com.graphql_java_generator.customscalars.GraphQLScalarTypeDateTime.DateTime"
                    ],
                    [
                            graphQLTypeName             : "UntrustedRegexp",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
                    [
                            graphQLTypeName             : "Upload",
                            javaType                    : "java.lang.String",
                            graphQLScalarTypeStaticField: "graphql.Scalars.GraphQLString"
                    ],
            ]
}
thomson90 commented 3 months ago

Hi Etienne, i have solved this Problem...

Field springContextBean in de.myCompany.myProject.gitlab.spring_autoconfiguration.GraphQLPluginAutoConfiguration required a bean of type 'com.graphql_java_generator.client.SpringContextBean' that could not be found.

Action: Consider defining a bean of type 'com.graphql_java_generator.client.SpringContextBean' in your configuration.

with the ComponentScan Annotation on of my config-classes: @ComponentScan(basePackages = { "com.graphql_java_generator.client" })

I think that the classes that were autogenerated could not call the SpringContextBean of the client library. Unfortunately, I have other problems with the migration. I will rebuild my project so that I can publish it on Gitlab and keep you in the loop.

etienne-sf commented 3 months ago

Hello,

I created a project from your ´build.gradle´ and ´graph.gradle´ files.

But I could not locate this code (that you provided):

´´´ java private ObjectResponse getObjectResponse(String queryResponseDef) throws GraphQLRequestPreparationException { return new Builder(graphQlClient, GraphQLRequest.class).withQueryResponseDef(queryResponseDef).build(); } ´´´

From where did you copy this code ?

Étienne

thomson90 commented 3 months ago

Sorry for the confusion. The code was written by myself in my project and uses your builder constructor. I thought the changes in your new Builder-constructor where responsible for the problem mentioned above i was able to fix the original problem by setting the correct annotation:

@SpringBootApplication(scanBasePackageClasses = { Application.class, GraphqlClientUtils.class, QueryExecutor.class })

I was unable to reproduce @Erando89's error. Maybe he forgot the correct Annotation too. From my point of view, this issue can be closed.

etienne-sf commented 3 months ago

Right, ok.

That explains why I didn't find this code.

BTW, I don't understand why you had to create a GraphQlClient yourself. The plugin generates a spring autoconfiguration file, that creates the needed GraphQlClient. So you should not have to create it yourself. Please note that:

I added a note in my todo list, to check that this is properly documented.

Etienne