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

How to exclude DataFetchers/Controllers in 2.x #190

Closed stahloss closed 1 year ago

stahloss commented 1 year ago

Given a GraphQL non-scalar schema type "ABC", with version 1.x it was possible to not have separate data loaders for types by overriding addWiringABC methods, like so:

@Primary
@Component
class CustomGraphQLWiring : GraphQLWiring() {

    override fun addWiringABC(
        typeWiring: TypeRuntimeWiring.Builder
    ): TypeRuntimeWiring.Builder {
        return typeWiring
    }
}

Now non-scalar ABC type would not be data loaded.

After migrating to version 2.x and with it to spring-grapql, @Controller classes are generated with @SchemaMapping(typeName = "ABC"). There is still a GraphQLWiring class that can be extended, but it seems impossible to exclude the data loader. I've tried as follows, but the Controller and DataFetcher methods are still called.

@Primary
@Component
class CustomGraphQLWiring : GraphQLWiring() {

    override fun configure(builder: RuntimeWiring.Builder) {
        builder.type("ABC") {
            it.dataFetchers(emptyMap())
        }
}

Is there any way to achieve the same behaviour as was possible in 1.x? If not, it would be great if it could be added.

etienne-sf commented 1 year ago

I like very much this question : it's not possible yet.

The initial idea I had on this subject, was to either generate or not generate the controllers. But then, to override one controller, you would have to create (and maintain) all the controllers: not easy, especially for bug GraphQL schemas.

I guess your need is to replace on of the controllers for a specific need. Or do you need to replace all of them ?

Etienne

stahloss commented 1 year ago

Yes it is inconvenient to have to create and maintain all of the controllers if you have a special case of one of them. This is the route I've taken now by only using the generatePojo goal of the plugin and implementing the controllers myself

My need is to be able to exclude some GraphQL types from generating to a Controller with SchemaMapping, because these objects will be fetched in the controller of the parent type.

Maybe this could be achieved through plugin configuration or schema personalization?

etienne-sf commented 1 year ago

I'll generate a spring autoconfiguration file, that will declare the controllers. This authorizes the use of the @Primary spring annotation, to replace such a declared bean. It will be in the next release.

stahloss commented 1 year ago

That will also do :)

Thanks for the quick resolution and I'll be looking forward to the new release.

etienne-sf commented 1 year ago

This is solved in the 2.1 release

etienne-sf commented 1 year ago

Please take a look at the for more information on this page: https://github.com/graphql-java-generator/graphql-maven-plugin-project/wiki/server_migrate_1-x_to_2-x