Skip to content

drop support for java versions before 17 #25

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -11,8 +11,6 @@ jobs:
strategy:
matrix:
version:
- 8
- 11
- 17
- 21
distribution:
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencies>
@@ -54,7 +54,7 @@
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>20.4</version>
<version>21.3</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
@@ -92,7 +92,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
2 changes: 1 addition & 1 deletion src/integration-test/simple/pom.xml
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.alme.graphql.generator;

import static java.lang.String.format;

import java.io.File;
import java.util.Map;
import java.util.Set;
@@ -192,14 +190,14 @@ public void execute() throws MojoExecutionException {
.importPackages(importPackages)
.build();

getLog().info(format("Current configuration: %s.", configuration));
getLog().info("Current configuration: %s.".formatted(configuration));

GqlContext context = new GqlContext(getLog(), configuration.getScalars(), configuration.getAliases());
ReaderFactory readerFactory = new ReaderFactory(configuration.getSourceFiles(), getLog());
WriterFactory writerFactory = new WriterFactory(configuration.getOutputRoot());

new GqlReader(readerFactory).read(context, configuration);
getLog().debug(format("Current context: %s.", context));
getLog().debug("Current context: %s.".formatted(context));

new GqlWriter(writerFactory).write(context, configuration);
getLog().info("Generation is done.");
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.alme.graphql.generator.dto;

import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toSet;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
@@ -29,27 +27,27 @@ public class GqlSelection {
@Setter private String targetTypeName;

public String getKey() {
return String.format("%s:%s:%s:%s", alias, field.getName(), field.getType(), fragmentTypeName);
return "%s:%s:%s:%s".formatted(alias, field.getName(), field.getType(), fragmentTypeName);
}

public static GqlSelection of(GqlField field, String alias, String fragmentTypeName) {
return new GqlSelection(field, alias, fragmentTypeName, emptySet());
return new GqlSelection(field, alias, fragmentTypeName, Set.of());
}

public static GqlSelection of(GqlField field, String alias, SelectionSet subset) {
return new GqlSelection(field, alias, "", getSubsets(subset));
}

public static GqlSelection of(GqlField field, String alias) {
return new GqlSelection(field, alias, "", emptySet());
return new GqlSelection(field, alias, "", Set.of());
}

public static GqlSelection of(String typeName, SelectionSet subset) {
return new GqlSelection(GqlField.of(null, GqlType.named(typeName)), null, "", getSubsets(subset));
}

private static Set<SelectionSet> getSubsets(SelectionSet subset) {
return Optional.ofNullable(subset).map(Collections::singleton).orElseGet(Collections::emptySet);
return Optional.ofNullable(subset).map(Set::of).orElseGet(Set::of);
}

public GqlSelection merge(GqlSelection that) {
51 changes: 21 additions & 30 deletions src/main/java/com/github/alme/graphql/generator/dto/GqlType.java
Original file line number Diff line number Diff line change
@@ -22,14 +22,14 @@ public enum Flag {
GqlType nested;

public static GqlType of(Type<?> type, UnaryOperator<String> naming) {
if (type instanceof NonNullType) {
return mandatory(of(((NonNullType) type).getType(), naming));
if (type instanceof NonNullType nonnull) {
return mandatory(of(nonnull.getType(), naming));
}
else if (type instanceof ListType) {
return list(of(((ListType) type).getType(), naming));
else if (type instanceof ListType list) {
return list(of(list.getType(), naming));
}
else if (type instanceof TypeName) {
return named(naming.apply(((TypeName) type).getName()));
else if (type instanceof TypeName named) {
return named(naming.apply(named.getName()));
}
return null;
}
@@ -58,39 +58,30 @@ public String getName() {
* Used in templates
*/
public String getFull() {
switch (flag) {
case MANDATORY:
return nested.getFull();
case LIST:
return String.format("java.util.List<%s>", nested.getFull());
default:
return name;
}
return switch (flag) {
case MANDATORY -> nested.getFull();
case LIST -> "java.util.List<%s>".formatted(nested.getFull());
case NAMED -> name;
};
}

/**
* Used in templates
*/
public String getCustom(String customType) {
switch (flag) {
case MANDATORY:
return nested.getCustom(customType);
case LIST:
return String.format("java.util.List<%s>", nested.getCustom(customType));
default:
return customType;
}
return switch (flag) {
case MANDATORY -> nested.getCustom(customType);
case LIST -> "java.util.List<%s>".formatted(nested.getCustom(customType));
case NAMED -> customType;
};
}

@Override
public String toString() {
switch (flag) {
case MANDATORY:
return String.format("%s!", nested);
case LIST:
return String.format("[%s]", nested);
default:
return name;
}
return switch (flag) {
case MANDATORY -> "%s!".formatted(nested);
case LIST -> "[%s]".formatted(nested);
case NAMED -> name;
};
}
}
18 changes: 8 additions & 10 deletions src/main/java/com/github/alme/graphql/generator/io/GqlReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.alme.graphql.generator.io;

import static java.lang.String.format;

import java.io.IOException;
import java.io.Reader;

@@ -41,36 +39,36 @@ public void read(GqlContext context, GqlConfiguration configuration) {
.parserOptions(configuration.getParserOptions())
.build();
Document doc = Parser.parse(environment);
log.info(format(LOG_PARSER, doc.getDefinitions().size()));
log.info(LOG_PARSER.formatted(doc.getDefinitions().size()));

new InputObjectTypeTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getInputObjectTypes().size(), "Input Object type"));
log.info(LOG_TRANSLATOR.formatted(context.getInputObjectTypes().size(), "Input Object type"));

new EnumTypeTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getEnumTypes().size(), "Enum type"));
log.info(LOG_TRANSLATOR.formatted(context.getEnumTypes().size(), "Enum type"));

new InterfaceTypeTranslator().translate(doc, context);
new UnionTypeTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getInterfaceTypes().size(), "Interface and union type"));
log.info(LOG_TRANSLATOR.formatted(context.getInterfaceTypes().size(), "Interface and union type"));

new ObjectTypeTranslator().translate(doc, context);
new RelayConnectionTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getObjectTypes().size(), "Object type"));
log.info(LOG_TRANSLATOR.formatted(context.getObjectTypes().size(), "Object type"));

boolean generateDefinedOperations = configuration.isGenerateDefinedOperations();
boolean generateDynamicOperations = configuration.isGenerateDynamicOperations();

if (generateDefinedOperations || generateDynamicOperations) {
new SchemaTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getOperations().size(), "Schema"));
log.info(LOG_TRANSLATOR.formatted(context.getOperations().size(), "Schema"));
}
if (generateDefinedOperations) {
new OperationTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getDefinedOperations().size(), "Defined operation"));
log.info(LOG_TRANSLATOR.formatted(context.getDefinedOperations().size(), "Defined operation"));
}
if (generateDynamicOperations) {
new DynamicOperationTranslator().translate(doc, context);
log.info(format(LOG_TRANSLATOR, context.getDynamicOperations().size(), "Dynamic operation"));
log.info(LOG_TRANSLATOR.formatted(context.getDynamicOperations().size(), "Dynamic operation"));
}
}
catch (IOException e) {
30 changes: 14 additions & 16 deletions src/main/java/com/github/alme/graphql/generator/io/GqlWriter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.alme.graphql.generator.io;

import static java.lang.String.format;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.groupingBy;

@@ -25,7 +24,6 @@
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModelException;
import lombok.val;

public class GqlWriter {

@@ -106,41 +104,41 @@ private void createSchemaTypes(GqlContext context, GqlConfiguration configuratio
String packageName = configuration.getSchemaTypesPackageName();
int count = 0;
if (configuration.isGenerateSchemaInputTypes()) {
val inputObjectFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.INPUT_OBJECT);
var inputObjectFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.INPUT_OBJECT);
context.getInputObjectTypes()
.forEach((className, gqlStructure) -> inputObjectFileCreator
.createFile(packageName, className, gqlStructure));
count += context.getInputObjectTypes().size();
}
if (configuration.isGenerateSchemaInputTypes() || configuration.isGenerateSchemaOtherTypes()) {
val enumFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.ENUM);
var enumFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.ENUM);
context.getEnumTypes()
.forEach((className, gqlStructure) -> enumFileCreator
.createFile(packageName, className, gqlStructure));
count += context.getEnumTypes().size();
}
if (configuration.isGenerateSchemaOtherTypes()) {
val interfaceFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.INTERFACE);
var interfaceFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.INTERFACE);
context.getInterfaceTypes()
.forEach((className, gqlStructure) -> interfaceFileCreator
.createFile(packageName, className, gqlStructure));
count += context.getInterfaceTypes().size();
val objectFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.OBJECT);
var objectFileCreator = new StructureFileCreator(writerFactory, freemarker, Structure.OBJECT);
context.getObjectTypes()
.forEach((className, gqlStructure) -> objectFileCreator
.createFile(packageName, className, gqlStructure));
count += context.getObjectTypes().size();
}
if (count > 0) {
context.getLog().info(format("Finished creating %d schema type class(es).", count));
context.getLog().info("Finished creating %d schema type class(es).".formatted(count));
}
}

private void createOperationInterfaces(GqlContext context, GqlConfiguration configuration) {
if ((configuration.isGenerateDefinedOperations() && !context.getDefinedOperations().isEmpty()) ||
(configuration.isGenerateDynamicOperations() && !context.getDynamicOperations().isEmpty())
) {
val operationInterfaceFileCreator = new OperationInterfaceFileCreator(writerFactory, freemarker);
var operationInterfaceFileCreator = new OperationInterfaceFileCreator(writerFactory, freemarker);
context.getOperations().keySet().stream()
.map(Util::firstUpper)
.forEach(interfaceName -> operationInterfaceFileCreator
@@ -150,23 +148,23 @@ private void createOperationInterfaces(GqlContext context, GqlConfiguration conf

private void createDefinedOperations(GqlContext context, GqlConfiguration configuration) {
if (configuration.isGenerateDefinedOperations() && !context.getDefinedOperations().isEmpty()) {
val definedOperationFileCreator = new DefinedOperationFileCreator(writerFactory, freemarker);
val definedOperationVariablesFileCreator = new DefinedOperationVariablesFileCreator(writerFactory, freemarker);
var definedOperationFileCreator = new DefinedOperationFileCreator(writerFactory, freemarker);
var definedOperationVariablesFileCreator = new DefinedOperationVariablesFileCreator(writerFactory, freemarker);
context.getDefinedOperations().forEach((operationName, operation) -> {
String packageName = configuration.getDefinedOperationsPackageName() + "." + Util.firstLower(operationName);
definedOperationFileCreator.createFile(packageName, operationName, operation);
if (!operation.getVariables().isEmpty()) {
definedOperationVariablesFileCreator.createFile(packageName, operationName + "Variables", operation);
}
});
val definedOperationResultFileCreator = new DefinedOperationResultFileCreator(writerFactory, freemarker);
var definedOperationResultFileCreator = new DefinedOperationResultFileCreator(writerFactory, freemarker);
context.getDefinedSelections().forEach((operationName, typeMap) -> {
String packageName = configuration.getDefinedOperationsPackageName() + "." + Util.firstLower(operationName) + ".results";
typeMap.forEach((typeName, selections) -> definedOperationResultFileCreator
.createFile(packageName, typeName, singletonMap("selections", selections)));
});
context.getDefinedOperations().forEach((operationName, operation) ->
context.getLog().info(format("Finished creating %d class(es) for %s operation.",
context.getLog().info("Finished creating %d class(es) for %s operation.".formatted(
(operation.getVariables().isEmpty() ? 1 : 2) + context.getDefinedSelections().get(operationName).size(),
operationName)));
}
@@ -175,18 +173,18 @@ private void createDefinedOperations(GqlContext context, GqlConfiguration config
private void createDynamicOperations(GqlContext context, GqlConfiguration configuration) {
if (configuration.isGenerateDynamicOperations() && !context.getDynamicOperations().isEmpty()) {
String packageName = configuration.getDynamicOperationsPackageName();
val dynamicOperationFileCreator = new DynamicOperationFileCreator(writerFactory, freemarker);
var dynamicOperationFileCreator = new DynamicOperationFileCreator(writerFactory, freemarker);
context.getDynamicOperations()
.forEach((className, operation) -> dynamicOperationFileCreator.createFile(packageName, className, operation));
val dynamicOperationResultFileCreator = new DynamicOperationResultFileCreator(writerFactory, freemarker);
val dynamicOperationSelectorFileCreator = new DynamicOperationSelectorFileCreator(writerFactory, freemarker);
var dynamicOperationResultFileCreator = new DynamicOperationResultFileCreator(writerFactory, freemarker);
var dynamicOperationSelectorFileCreator = new DynamicOperationSelectorFileCreator(writerFactory, freemarker);
context.getDynamicSelections()
.forEach((className, selections) -> {
dynamicOperationResultFileCreator.createFile(packageName + ".results", className, singletonMap("selections", selections));
dynamicOperationSelectorFileCreator.createFile(packageName + ".selectors", className + "Selector",
singletonMap("selections", selections.stream().collect(groupingBy(GqlSelection::getFragmentTypeName))));
});
context.getLog().info(format("Finished creating %d dynamic operation(s) with %d common class(es).",
context.getLog().info("Finished creating %d dynamic operation(s) with %d common class(es).".formatted(
context.getDynamicOperations().size(),
context.getDynamicSelections().size() * 2));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.alme.graphql.generator.io;

import static java.lang.String.format;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
@@ -13,7 +11,6 @@

import graphql.parser.MultiSourceReader;
import lombok.RequiredArgsConstructor;
import lombok.Value;

@RequiredArgsConstructor
public class ReaderFactory {
@@ -27,7 +24,7 @@ public Reader getReader() {
try {
return new FileInfo(Files.newBufferedReader(path), path.toString());
} catch (IOException e) {
log.error(format("Skipping [%s].", path), e);
log.error("Skipping [%s].".formatted(path), e);
return null;
}
})
@@ -39,10 +36,6 @@ public Reader getReader() {
.build();
}

@Value
private static class FileInfo {
Reader reader;
String path;
}
private record FileInfo(Reader reader, String path) { }

}
Loading