Skip to content

Support for prefixes and suffixes on generated java classes #155

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

Merged
merged 30 commits into from
Nov 4, 2022

Conversation

agesenm-ELS
Copy link
Contributor

To support using graphql-maven-plugin instead of another code generator in an existing code base, the code generator is extended to support adding custom prefixes and suffixes on the generated java classes.

The need for this change was identified when trying to replace an old code generator in a very large code base.
The old generator is using a naming scheme with a suffix for input objects and types, where for example the type Droid would be named DroidGraphQLType (similarly for interfaces and enums). The code base have thousands of usages of these classes, making it risky to migrate all existing code, especially because there are existing classes (domain objects, and enums) used in the data fetchers, with the same class name as those that would be generated without a suffix.

The plugin is extended the following configuration options:

	/**
	 *  An optional prefix for the generated java classes for GraphQL types.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.typePrefix", defaultValue = "")
	public String typePrefix;

	/**
	 *  An optional suffix for the generated java classes for GraphQL types.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.typeSuffix", defaultValue = "")
	public String typeSuffix;

	/**
	 *  An optional prefix for the generated java classes for GraphQL unions.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.unionPrefix", defaultValue = "")
	public String unionPrefix;

	/**
	 *  An optional suffix for the generated java classes for GraphQL unions.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.unionSuffix", defaultValue = "")
	public String unionSuffix;

	/**
	 *  An optional prefix for the generated java classes for GraphQL enums.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.enumPrefix", defaultValue = "")
	public String enumPrefix;

	/**
	 *  An optional suffix for the generated java classes for GraphQL enums.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.enumSuffix", defaultValue = "")
	public String enumSuffix;

	/**
	 *  An optional prefix for the generated java classes for GraphQL interfaces.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.interfacePrefix", defaultValue = "")
	public String interfacePrefix;

	/**
	 *  An optional suffix for the generated java classes for GraphQL interfaces.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.interfaceSuffix", defaultValue = "")
	public String interfaceSuffix;

	/**
	 *  An optional prefix for the generated java classes for GraphQL input objects.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.inputPrefix", defaultValue = "")
	public String inputPrefix;

	/**
	 *  An optional suffix for the generated java classes for GraphQL input objects.
	 */
	@Parameter(property = "com.graphql_java_generator.mavenplugin.javaClassSuffix", defaultValue = "")
	public String inputSuffix;

Tests have been added to verify the configuration and code generation. A fix has been made for GraphqlUtilsTest which failed on master.

@agesenm-ELS agesenm-ELS mentioned this pull request Oct 13, 2022
It worked without prefixes/suffies because javaName and name were the same.
@agesenm-ELS
Copy link
Contributor Author

I added another commit addressing an issue in server_GraphQLWiring.vm where the javaName was used instead of name.
@etienne-sf can you point out the best location for a test of this?

@agesenm-ELS agesenm-ELS marked this pull request as draft October 14, 2022 12:57
@agesenm-ELS
Copy link
Contributor Author

I am changing the status to draft until testing of the wiring is complete.

Any review feedback would be much appreciated.

@etienne-sf
Copy link
Collaborator

Hello,

Thanks for the PR.
I was reluctant to integrate the idea of prefix and suffix in the first plugin, because I thought it would be complex. But your PR has quite a low impact on the code: that's nice!

My global comment is that is is well integrated.
And you added unit tests to check it.

FYI, the whole project didn't build on my computer, with failure during the IT tests (the sample projects). But I guess it's due to my crazy antivirus (I use my pro PC), as I checked your branch out in another folder to avoid name collision: the IT tests seem Ok when started from eclipse.

There is a point with the Gradle plugin:

  • If you can also have a PR for that, it would be nice. But the Gradle code is more complex. I can handle it, if you don't want to.
  • To be sure that Gradle plugin uses the same default, please use constants when defining the default value for the plugin (in the com.graphql_java_generator.mavenplugin.AbstractCommonMojo class), like with the other maven parameters.

Then a detail about the parameter doc:

  • This doc is displayed in the plugin page (https://graphql-maven-plugin-project.graphql-java-generator.com/graphql-maven-plugin/plugin-info.html). So it should be easy as possible to understand. In the com.graphql_java_generator.plugin.conf.CommonConfiguration interface and com.graphql_java_generator.mavenplugin.AbstractCommonMojo class, my opinion is that the doc should be a little more precise: "An optional suffix for the generated java classes for GraphQL input objects." to "An optional suffix to add to the classnames of the java classes generated for GraphQL input objects. The suffix is added at the beginning of the java classname, and must be compatible with java naming rules (no space, dot, comma...)" But I'm perhaps cutting hairs in four parts... (French expression to say it's too much) :) So do as you want

And, to finish, one word about the integration tests:

The IT should test various combination of prefix and suffix. My proposal here is:

  • Do the most complex stuff in the allGraphQlCases projects (both client and server), with different values for each suffix and prefix
  • Do the simplest thing in the forum projects (both client and server), with no suffix and no prefix.
  • If you think another test is useful, you can do it in the Star Wars project.

Notes :

  • By default, there is no output on the console, to avoid anoyance when buiding the project. You can add this to the JVM parameters: -Dlogging.config=classpath:logback-local.xml (like described here https://github.com/graphql-java-generator/graphql-maven-plugin-project/wiki/internal_howto_build#view-server-output). This will output the debug information on the console.
  • When building with eclipse, I often have a 'file not found' exception, when runnning the allGraphQlCases server. This is because this sample generate the schema, then uses it runtime. And when eclipse cleans the output, the plugin is not re-executed, and the file is missing. I should improve that (the plugin should detect the missing file, and regenerate it). My current workaround is to execute 'mvn clean install -pl :graphql-maven-plugin-samples-allGraphQLCases-server' to regenerate properly the missing file.

@etienne-sf
Copy link
Collaborator

I added another commit addressing an issue in server_GraphQLWiring.vm where the javaName was used instead of name. @etienne-sf can you point out the best location for a test of this?

About this specific question : the answer is in my previous mail. It's an integration test, and it will be tested in the allGraphQLCases server sample.

@agesenm-ELS
Copy link
Contributor Author

Hi @etienne-sf

Thanks for the detailed feedback.

agesenm-ELS and others added 5 commits October 24, 2022 09:31
It worked without prefixes/suffies because javaName and name were the same.
…lasses

This prevents the type prefix to be included in the names af data fetchers, executors, responses, and batch loaders.
@agesenm-ELS agesenm-ELS marked this pull request as ready for review October 25, 2022 12:46
@agesenm-ELS
Copy link
Contributor Author

agesenm-ELS commented Oct 25, 2022

Hi @etienne-sf

I clarified the parameter documentation as suggested, and created a module similar to the pojo samples, verifying the configured prefixes and suffixes used in client and server code generation. I chose not to add complexity to the existing samples by parameterising them with prefixes and suffixes.

The test revealed quite a few places in the templates, where name should be replaced with javaName to include the prefixes and suffixes. Similarly for data fetchers, executors, data loaders, where a type name is combined with another prefix or suffix (such as DataFetchersDelegate or Delegate), name should be used instead of javaName to avoid having two sets of prefixes and suffixes.

I would appreciate it, if you could deal with the gradle plugin.

FYI: The IT tests in the samples fails on my machine too, but they also do so on the master branch, so that is unrelated to this PR.

@etienne-sf
Copy link
Collaborator

Hello,

I had updates on the antivirus configuration of my computer.
The IT tests are ok on my computer for both the master branch and your PR.

I didn't find tests where a client with prefix and suffix executes queries against a server with (different) prefix and suffix).

The aim of the allGraphQLCases sample is to test complex stuff (whereas the Forum and StarWars samples are more a demo). So the allGraphQLCases sample should be updated so that both the client and the server have suffix and prefix. And these prefixes and suffixes should be different.
This will probably perhaps break some JUnit tests. If you succeed int making them work, it's nice. Otherwise I can manage their correction..

Etienne

@agesenm-ELS
Copy link
Contributor Author

So you are OK with that the existing allGraphqlCases client/server modules are changed to use prefix/suffix for the generated classes?

@etienne-sf
Copy link
Collaborator

Yes, please : it's exactly its aim.

BTW, I should also merge the template sample into it.
But it's another story
:)

Etienne

@agesenm-ELS
Copy link
Contributor Author

Hi Etienne

I added prefixes to allGraphQLCases client and server, which resulted in a lot of changes in the tests.

This revealed a few issues in the client, which lacked information on how to map a graphql type to the corresponding java class in the presence of prefixes/suffixes. So I added a new generated file for the client, GraphQLTypeMapping, and updated the client runtime to use.

Since allGraphQLCases client/server now uses prefixes/suffixes, there is no longer a need for the prefix-suffix module, that I had added, so I removed that again.

Mikkel

@etienne-sf etienne-sf merged commit bf613e3 into graphql-java-generator:master Nov 4, 2022
@etienne-sf
Copy link
Collaborator

Everything's fine.

Great !

Thank you for this job
:)

(There are issues with recent version of the JDK: I'll try to correct that before I release it. The next release should be this week end, still)

@agesenm-ELS
Copy link
Contributor Author

Thank you for the merge :-)

@etienne-sf
Copy link
Collaborator

Released in the 1.18.8 version

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

Successfully merging this pull request may close these issues.

2 participants