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

graphql-maven-plugin unable to create scalar for custom class #198

Closed rafalskalny closed 9 months ago

rafalskalny commented 10 months ago

Hey,

we are using graphql-maven-plugin 1.x to generate java model classes from graphqls files.

We have custom scalars defined with our custom classes, e.g.:

scalar CustomerId

type Query {
    getCustomer(id: CustomerId!): Customer!
}

and the java class which backs the scalar is:

package com.mycompany.myproject.somepackage;

public record CustomerId(String id) { }

Unfortunately the generation of the java classes stopped working for us when version 1.18.10 was released. The reason for this is this commit: https://github.com/graphql-java-generator/graphql-maven-plugin-project/commit/cb0ad28b#diff-ddaf51417f6600ca7ae82284e8c0b25bded77c66552242f6083382fad4325ee2 and the change to the com.graphql_java_generator.util.GraphqlUtils#getPackageName method. The reason the generation does not work any more is because the Class<?> cls = Class.forName(classFullName); code from com.graphql_java_generator.util.GraphqlUtils#getPackageName is not able to load CustomerId class from the project (the CustomerId class is not a JDK class).

If the current code:

public String getPackageName(String classFullNameParam) {
    String classFullName = (classFullNameParam.endsWith("[]"))
            ? classFullNameParam.substring(0, classFullNameParam.length() - 2)
            : classFullNameParam;

    if (isPrimitiveType(classFullName)) {
        return null; // No package for primitive types
    }

    try {
        Class<?> cls = Class.forName(classFullName);
        return cls.getPackage().getName();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(
                "Could not find the package for the class '" + classFullNameParam + "', due to: " + e.getMessage(),
                e);
    }
}

gets replaced with:

public String getPackageName(String classFullNameParam) {
    String classFullName = (classFullNameParam.endsWith("[]"))
            ? classFullNameParam.substring(0, classFullNameParam.length() - 2)
            : classFullNameParam;

    if (isPrimitiveType(classFullName)) {
        return null; // No package for primitive types
    }

    int lstPointPosition = classFullName.lastIndexOf('.');
    return classFullName.substring(0, lstPointPosition);
}

then the class generation with my custom scalars gets fixed.

Could you please apply this change to both 1.x and 2.x versions of the plugin?

Thanks

etienne-sf commented 9 months ago

Hello,

Thank you for the detailed analysis.

I'm still surprised that I didn't encounter this in all the existing integration tests.

Of course, I'll delivered that.

In the meanwhile, can you check the workaround that I suggested in the #184 ?

Etienne

rafalskalny commented 9 months ago

Hi,

Thank you for your response.

Unfortunately, the workaround you mentioned does not work for me. The scalar classes (e.g. record CustomerId) that are referenced in graphql schema files (e.g. scalar CustomerId) are part of the same maven project where the graphql-maven-plugin is executed to generate model classes. Since both are in the same Maven project, the artifact containing the scalar classes obviously hasn't been built yet when the plugin generates the model classes - it is impossible to reference an artifact that hasn't been built yet.

For it to work in my case, the plugin would have to add all classes from the `src/main/java' directory to the classpath. This is not a perfect approach, however, as it gets very complicated when it comes to implementing support for multi-module Maven projects.

I have to wait for the fix to be released.

Cheers Rafal

etienne-sf commented 9 months ago

Hello,

The correction for this issue has been released the 2.3 version. I'll see to add it into the 1.x version

etienne-sf commented 9 months ago

Correction available in 2.3 and 1.18.12 versions.