Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rafalskalny opened this issue Sep 12, 2023 · 4 comments
Closed

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

rafalskalny opened this issue Sep 12, 2023 · 4 comments

Comments

@rafalskalny
Copy link

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: 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
Copy link
Collaborator

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
Copy link
Author

rafalskalny commented Sep 15, 2023

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
Copy link
Collaborator

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
Copy link
Collaborator

Correction available in 2.3 and 1.18.12 versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants