Skip to content

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

Closed
@rafalskalny

Description

@rafalskalny

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions