Home

Awesome

Table of Content

<!--ts--> <!--te-->

GraphQL Java Generator

What is it?

The GraphQL Java Generator makes it easy to work in Java with graphQL in a schema first approach.

This project is a code generator, that allows to quickly develop GraphQL clients and GraphQL servers in java, based on a GraphQL schema.

That is: graphql-java-generator generates the boilerplate code, and lets you concentrate on what's specific to your use case. Then, the running code doesn't depend on any dependencies from graphql-java-generator. So you can get rid of graphql-java-generator at any time: just put the generated code in your SCM, and that's it.

Other points that are worth to point out:

The interesting part is that graphql-java-generator is an accelerator: you don't depend on any library from graphql-java-generator. So, it just helps you to build application based on graphql-java. At any time, you can take the generated code as a full sample for your graphql-java usage. You can then update the generated code, where it's not compliant for you. The other way is to use the personalization capability, and stay with the generated code as a basis.

The plugin goals/tasks

All maven goals and gradle tasks are described on this page

This plugin contains these goals (Maven) / tasks (Gradle):

Availibility: Maven and Gradle

The generator is currently available both as a Maven plugin and as a Gradle plugin:

Documentation

This README

This README gives you a quick overview of what this plugin offers

The full project documentation

The Full documentation is available here , for both the Gradle and the Maven plugin.

For the client mode, you'll find there a description on how to:

For the server mode, you'll find explanations on how to:

The tutorials

Two tutorials are available for the Maven plugin:

The same two tutorials exist for the Gradle plugin:

How to use it?

Full project documentation

You'll find below a quick presentation of the plugin.

For all the available information, please go to the project website

Plugin documentation

The plugin documentation, generated by maven, is available on this page

Samples

You'll find the following samples in the project. For all of these samples, there are two projects: the client and the server.

Note: The client projects for these samples contain integration tests. They are part of the global build. These integration tests check the graphql-maven-plugin behaviour for both the client and the server for these samples.

Client mode

When in client mode, you can query the server with just one line of code.

For instance :

String id = [an id];

Human human = queryType.human("{id name appearsIn homePlanet friends{name}}", id);

You can use input types:

HumanInput input = new HumanInput();
... [some initialization of input content]

Human human = mutationType.createHuman("{id name appearsIn friends {id name}}", input);

In this mode, the plugin generates:

You'll find more information on the client page.

Server mode

When in server mode, the plugin generates:

Once all this is generated, your only work is to implement the DataFetchersDelegate interfaces. They are the link between the GraphQL schema and your data storage. As such, they are specific to your use case. A DataFetchersDelegate implementation looks like this:

package com.graphql_java_generator.samples.forum.server.specific_code;

[imports]

@Component
public class DataFetchersDelegateTopicImpl implements DataFetchersDelegateTopic {

	@Resource
	MemberRepository memberRepository;
	@Resource
	PostRepository postRepository;
	@Resource
	TopicRepository topicRepository;

	@Resource
	GraphQLUtil graphQLUtil;

	@Override
	public CompletableFuture<Member> author(DataFetchingEnvironment dataFetchingEnvironment,
			DataLoader<UUID, Member> dataLoader, Topic source) {
		return dataLoader.load(source.getAuthorId());
	}

	@Override
	public List<Post> posts(DataFetchingEnvironment dataFetchingEnvironment, Topic source, String since) {
		if (since == null)
			return graphQLUtil.iterableToList(postRepository.findByTopicId(source.getId()));
		else
			return graphQLUtil.iterableToList(postRepository.findByTopicIdAndSince(source.getId(), since));
	}

	@Override
	public List<Topic> batchLoader(List<UUID> keys) {
		return topicRepository.findByIds(keys);
	}
}

You'll find all the info on the server page.

Custom code templates

Customizing the templates allows you to modify the generated code, in the way you want.

So, if for any reason you may need to customize the generated code, you can replace the default templates by your own, by using the plugin parameter templates: the templates plugin parameter is a map where the key is the ID of the template to customize and the value is a classpath entry to the resources containing the customized template.

Customize templates shall be provided in a dependency configured in the plugin.

Both client and server templates can be customized.

All the documentation, and the list of available templates is available in the Customizing code templates page.

Compatibility with GraphQL

This plugin respects quite all the GraphQL specification:

Change log

The Change Log is available here

Note for contributors

All the plugin logic is stored in the graphql-maven-plugin-project project.

The Maven plugin and the Gradle plugin are just wrapper for the plugin logic, available in the graphql-maven-plugin-logic module of the maven project.

If you want to compile the maven project, you'll have to add the lombok.jar file in your IDE. Please see the relevant section, in the Install menu of the https://projectlombok.org/ home page. This very nice tools generates all java boiler plate code, like setters, getters, constructors from fields...

If you use eclipse, please use the code formatter given with the project (file graphql-java-generator (eclipse code formatter).xml at the root of the project). This allows to have the sample code formatting: the code is then homogeneous, and the comparison between versions is simpler. To do this, go to the eclipse preferences, select Java/Code Style/Formatter, and import this file. Then, in the Java/Editor/Save Actions, check the "Perform the selected action on save", "Format source code", "Format all lines", "Organize imports" and "Additional actions" which its default content

License

graphql-java-generator is licensed under the MIT License. See LICENSE for details.