Skip to content

Commit

Permalink
Issue-129 Read typeMapping from properties file in classpath (#130)
Browse files Browse the repository at this point in the history
* Issue-129 Read GraphQL type to Java/Kotlin class mapping from typeMapping properties file in compile-classpath

* Issue-129
Update version as per review comment
Update ReadMe

* Issue-129
Update version as per review comment
* Remove CustomUrlClassLoader.java and read it through jarFile

* Issue-129
Remove debug logs
  • Loading branch information
ramapalani authored Feb 10, 2024
1 parent ea5a5a8 commit 206b703
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ Example
</typeMapping>
```

## typeMappingPropertiesFiles
Provide typeMapping as properties file(s) that is accessible as a compile-time-classpath resource
Key-Value pairs in the properties file will be added to `typeMapping` Map when it is not already present in it

When a same GraphQL type is present in both `typeMapping` and also in `typeMappingPropertiesFiles`, value in `typeMapping` will be used (and the value from `typeMappingPropertiesFiles` *will* be ignored)

- Type: Array
- Required: false

Example

```xml
<typeMappingPropertiesFiles>
<typeMappingPropertiesFile>commontypes-typeMapping.properties</typeMappingPropertiesFile>
<typeMappingPropertiesFile>someother-commontypes-typeMapping.properties</typeMappingPropertiesFile>
</typeMappingPropertiesFiles>
```

## subPackageNameClient

- Type: string
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>io.github.deweyjose</groupId>
<artifactId>graphqlcodegen-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.51.3</version>
<version>1.60.0</version>

<name>GraphQL Code Generator</name>
<description>Maven port of the Netflix DGS GraphQL Codegen gradle build plugin</description>
Expand Down
63 changes: 62 additions & 1 deletion src/main/java/io/github/deweyjose/graphqlcodegen/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
import com.netflix.graphql.dgs.codegen.CodeGen;
import com.netflix.graphql.dgs.codegen.CodeGenConfig;
import com.netflix.graphql.dgs.codegen.Language;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.*;

@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.COMPILE)
public class Codegen extends AbstractMojo {

@Parameter(defaultValue = "${project}")
Expand Down Expand Up @@ -55,6 +63,15 @@ public class Codegen extends AbstractMojo {
@Parameter(property = "typeMapping")
private Map<String, String> typeMapping;

/**
* Provide the typeMapping as properties file(s)
* that is accessible as a compile-time-classpath resource
* Values in the properties file will be added to `typeMapping` Map
* when it is not already present
*/
@Parameter(property = "typeMappingPropertiesFiles")
private String [] typeMappingPropertiesFiles;

@Parameter(property = "generateBoxedTypes", defaultValue = "false")
private boolean generateBoxedTypes;

Expand Down Expand Up @@ -209,6 +226,25 @@ public void execute() {
return;
}

if (typeMappingPropertiesFiles!=null && typeMappingPropertiesFiles.length > 0) {
Set<Artifact> dependencies = project.getArtifacts();
java.util.Properties typeMappingProperties = new java.util.Properties();
for (Artifact dependency : dependencies) {
File artifactFile = dependency.getFile();
if (artifactFile != null && artifactFile.isFile()) {
loadPropertiesFile(typeMappingProperties, artifactFile, typeMappingPropertiesFiles);
}
}
//Set key-value from this properties object to typeMapping Map
//only when it is not already present in the Map
if (typeMapping == null) {
typeMapping = new HashMap<>();
}
typeMappingProperties.forEach((k, v) -> {
typeMapping.putIfAbsent(String.valueOf(k), String.valueOf(v));
});
}

final CodeGenConfig config = new CodeGenConfig(
emptySet(),
schemaPaths,
Expand Down Expand Up @@ -268,4 +304,29 @@ public void execute() {
}
}
}

/**
*
* @param typeMappingProperties: Java Properties where typeMapping will be loaded into
* @param artifactFile: Artifact file
* @param typeMappingPropertiesFiles: Input: Classpath location of typeMapping properties file
* @return
*/
private void loadPropertiesFile(java.util.Properties typeMappingProperties,
File artifactFile, String [] typeMappingPropertiesFiles) {
try (JarFile jarFile = new JarFile(artifactFile)) {
for (String file : typeMappingPropertiesFiles) {
ZipEntry entry = jarFile.getEntry(file);
if (entry!=null) {
try (InputStream inputStream = jarFile.getInputStream(entry)) {
// load the data into the typeMappingProperties
typeMappingProperties.load(inputStream);
}
}
}
} catch (IOException e) {
getLog().error(e);
}
}

}

0 comments on commit 206b703

Please sign in to comment.