Skip to content
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

spring support for graphQl schema generator #33

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions apigen-deps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
<maven>3.0</maven>
</prerequisites>

<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen-deps</artifactId>
<version>3.0.1-SNAPSHOT</version>
<name>Dependencies for generated code</name>
<packaging>jar</packaging>

<version>${apigen.version}</version>

<properties>
<apigen.version>3.0.2</apigen.version>
</properties>

<parent>
<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen-pom</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<url>https://github.com/distelli/graphql-apigen</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public Object get(DataFetchingEnvironment env) {

public Object replaceResolved(Object result, Iterator<Object> resolved, int depth) {
if ( depth <= 0 ) {
return resolved.next();
if(resolved.hasNext())
return resolved.next();
return result;
}
List<Object> resolvedResults = new ArrayList<>();
if ( null == result ) return null;
Expand Down
11 changes: 7 additions & 4 deletions apigen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
<maven>3.0</maven>
</prerequisites>

<groupId>com.distelli.graphql</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the groupId get inherited from the parent?

<artifactId>graphql-apigen</artifactId>
<version>3.0.1-SNAPSHOT</version>
<name>Generate Java interfaces given a GraphQL Schema</name>
<packaging>maven-plugin</packaging>
<version>${apigen.version}</version>

<properties>
<apigen.version>3.0.2</apigen.version>
</properties>

<parent>
<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen-pom</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<url>https://github.com/distelli/graphql-apigen</url>
Expand Down Expand Up @@ -129,7 +132,7 @@
<dependency>
<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen-deps</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>${apigen.version}</version>
</dependency>
</dependencies>
</project>
52 changes: 45 additions & 7 deletions apigen/src/main/java/com/distelli/graphql/apigen/ApiGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ApiGen {
private Path outputDirectory;
private STGroup stGroup;
private String guiceModuleName;
private String springModuleName;
private String defaultPackageName;
private Map<String, TypeEntry> generatedTypes = new LinkedHashMap<>();
private Map<String, TypeEntry> referenceTypes = new HashMap<>();
Expand All @@ -36,6 +37,7 @@ public static class Builder {
private Path outputDirectory;
private STGroup stGroup;
private String guiceModuleName;
private String springModuleName;
private String defaultPackageName;

/**
Expand Down Expand Up @@ -67,6 +69,11 @@ public Builder withGuiceModuleName(String guiceModuleName) {
return this;
}

public Builder withSpringModuleName(String springModuleName) {
this.springModuleName = springModuleName;
return this;
}

public Builder withDefaultPackageName(String defaultPackageName) {
this.defaultPackageName = defaultPackageName;
return this;
Expand All @@ -89,6 +96,8 @@ private ApiGen(Builder builder) throws IOException {
throw new NullPointerException("The ApiGen outputDirectory must be specified");
}
guiceModuleName = builder.guiceModuleName;
springModuleName = builder.springModuleName;

defaultPackageName = builder.defaultPackageName;
outputDirectory = builder.outputDirectory;
stGroup = ( null == builder.stGroup )
Expand Down Expand Up @@ -200,7 +209,7 @@ public void generate() throws IOException {
String content = stGroup.getInstanceOf(generatorName+"Generator")
.add("model", model)
.render();
if ( stGroup.isDefined(generatorName + "GuiceModule") ) {
if ( guiceModuleName != null && stGroup.isDefined(generatorName + "GuiceModule") ) {
moduleBuilder.append(stGroup.getInstanceOf(generatorName+"GuiceModule")
.add("model", model)
.render());
Expand All @@ -216,13 +225,42 @@ public void generate() throws IOException {
if ( moduleBuilder.length() > 0 && guiceModuleName != null && stGroup.isDefined("guiceModule") ) {
PackageClassName packageClassName = getPackageClassName(guiceModuleName);
String content = stGroup.getInstanceOf("guiceModule")
.add("packageName", packageClassName.packageName)
.add("className", packageClassName.className)
.add("configure", moduleBuilder.toString())
.render();
.add("packageName", packageClassName.packageName)
.add("className", packageClassName.className)
.add("configure", moduleBuilder.toString())
.render();
writeFile(Paths.get(getDirectory(packageClassName.packageName).toString(),
packageClassName.className+".java"),
content);
} else if (springModuleName != null && stGroup.isDefined("springModule") ) {
PackageClassName packageClassName = getPackageClassName(springModuleName);
final String body = " /*\n" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply put this in the string template file?

" Please insert to your graphQL server class:\n" +
"\n" +
" @Autowired\n" +
" List<Provider<? extends GraphQLType>> graphQLTypes;\n" +
"\n" +
" @Autowired\n" +
" BeanFactory beanFactory;\n" +
" Map<String, GraphQLType> graphqlTypeMap;\n" +
"\n" +
" @PostConstruct\n" +
" public void initGraphQLServer(){\n" +
" graphqlTypeMap = (Map<String, GraphQLType>)beanFactory.getBean(\"graphqlTypeMap\", graphQLTypes);\n" +
" }\n" +
"\n" +
" Note: GraphQLServer class is not been generated because there is no one implementation for that class,\n" +
" the comments above are required in-order to generate schema files\n" +
" */";

String content = stGroup.getInstanceOf("springModule")
.add("packageName", packageClassName.packageName)
.add("className", packageClassName.className)
.add("body",body)
.render();
writeFile(Paths.get(getDirectory(packageClassName.packageName).toString(),
packageClassName.className+".java"),
content);
packageClassName.className+".java"),
content);
}
}

Expand Down
24 changes: 14 additions & 10 deletions apigen/src/main/java/com/distelli/graphql/apigen/ApiGenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ApiGenMojo extends AbstractMojo {
private MavenProject project;

@Parameter(name="sourceDirectory",
defaultValue="schema")
defaultValue="schema")
private File sourceDirectory;

@Parameter(name="outputDirectory",
Expand All @@ -45,6 +45,9 @@ public class ApiGenMojo extends AbstractMojo {
@Parameter(name="guiceModuleName")
private String guiceModuleName;

@Parameter(name="springModuleName")
private String springModuleName;

@Parameter(name="defaultPackageName", defaultValue = "com.graphql.generated")
private String defaultPackageName;

Expand Down Expand Up @@ -84,6 +87,7 @@ public void execute() {
ApiGen apiGen = new ApiGen.Builder()
.withOutputDirectory(outputDirectory.toPath())
.withGuiceModuleName(guiceModuleName)
.withSpringModuleName(springModuleName)
.withDefaultPackageName(defaultPackageName)
.build();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cp);
Expand All @@ -109,20 +113,20 @@ public void execute() {
}

private interface VisitPath {
public void visit(Path path) throws IOException;
void visit(Path path) throws IOException;
}

private void findGraphql(File rootDir, VisitPath visitPath) throws IOException {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.graphql{,s}");
Files.walkFileTree(rootDir.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if ( matcher.matches(file) ) {
getLog().debug("Processing "+file);
visitPath.visit(file);
}
return FileVisitResult.CONTINUE;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if ( matcher.matches(file) ) {
getLog().debug("Processing "+file);
visitPath.visit(file);
}
});
return FileVisitResult.CONTINUE;
}
});
}
}
36 changes: 16 additions & 20 deletions apigen/src/main/java/com/distelli/graphql/apigen/STModel.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
package com.distelli.graphql.apigen;

import graphql.language.Definition;
import graphql.language.EnumTypeDefinition;
import graphql.language.EnumValueDefinition;
import graphql.language.FieldDefinition;
import graphql.language.InputObjectTypeDefinition;
import graphql.language.InputValueDefinition;
import graphql.language.InterfaceTypeDefinition;
import graphql.language.ListType;
import graphql.language.NonNullType;
import graphql.language.ObjectTypeDefinition;
import graphql.language.OperationTypeDefinition;
import graphql.language.ScalarTypeDefinition;
import graphql.language.SchemaDefinition;
import graphql.language.Type;
import graphql.language.TypeName;
import graphql.language.UnionTypeDefinition;
import graphql.language.Value;
import graphql.language.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps your editor did this, but I'd prefer to avoid star imports :).


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -76,7 +61,7 @@ public static class Arg {
public String name;
public String type;
public String graphQLType;
public String defaultValue;
public Object defaultValue;
public Arg(String name, String type) {
this.name = name;
this.type = type;
Expand All @@ -90,9 +75,10 @@ public static class Field {
public String name;
public String type;
public DataResolver dataResolver;
public boolean isStringValue;
public String graphQLType;
public List<Arg> args;
public String defaultValue;
public Object defaultValue;
public Field(String name, String type) {
this.name = name;
this.type = type;
Expand Down Expand Up @@ -289,6 +275,11 @@ private List<Field> getFields(InputObjectTypeDefinition def) {
Field field = new Field(fieldDef.getName(), toJavaTypeName(fieldDef.getType()));
field.graphQLType = toGraphQLType(fieldDef.getType());
field.defaultValue = toJavaValue(fieldDef.getDefaultValue());
if(field.defaultValue != null && field.defaultValue instanceof String ) {
field.isStringValue = true;
}else {
field.isStringValue = false;
}
fields.add(field);
}
return fields;
Expand Down Expand Up @@ -329,7 +320,12 @@ private List<Arg> toArgs(List<InputValueDefinition> defs) {
return result;
}

private String toJavaValue(Value value) {
private Object toJavaValue(Value value) {
if(value instanceof StringValue){
return ((StringValue) value).getValue();
}else if(value instanceof IntValue){
return ((IntValue) value).getValue();
}
// TODO: Implement me!
return null;
}
Expand Down
Loading