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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
spring support for graphQl schema generator
fix 2
- masked injector code from posts project
  • Loading branch information
PeretzNadav committed Jan 28, 2019
commit a4bffdcf0669581f29627672ea362e1df0dacff6
28 changes: 21 additions & 7 deletions apigen/src/main/java/com/distelli/graphql/apigen/ApiGen.java
Original file line number Diff line number Diff line change
@@ -213,12 +213,7 @@ public void generate() throws IOException {
moduleBuilder.append(stGroup.getInstanceOf(generatorName+"GuiceModule")
.add("model", model)
.render());
} else if ( springModuleName != null && stGroup.isDefined(generatorName + "SpringModule") ) {
moduleBuilder.append(stGroup.getInstanceOf(generatorName+"SpringModule")
.add("model", model)
.render());
}

writeFile(Paths.get(directory.toString(), fileName),
content);
}
@@ -237,12 +232,31 @@ public void generate() throws IOException {
writeFile(Paths.get(getDirectory(packageClassName.packageName).toString(),
packageClassName.className+".java"),
content);
} else if ( moduleBuilder.length() > 0 && springModuleName != null && stGroup.isDefined("springModule") ) {
} 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", moduleBuilder.toString())
.add("body",body)
.render();
writeFile(Paths.get(getDirectory(packageClassName.packageName).toString(),
packageClassName.className+".java"),
16 changes: 15 additions & 1 deletion apigen/src/main/resources/graphql-apigen.stg
Original file line number Diff line number Diff line change
@@ -352,12 +352,26 @@ public class <className> extends AbstractModule {
springModule(packageName, className, body) ::= <<
package <packageName>;

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import graphql.schema.GraphQLType;

import javax.inject.Provider;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Configuration
public class <className> {
<body>
<body>
@Lazy
@Bean
public Map\<String, GraphQLType> graphqlTypeMap(List\<Provider\<? extends GraphQLType>\> typeList) {
return typeList.stream().map(Provider::get).collect(Collectors.toMap(GraphQLType::getName, Function.identity()));
}
}
>>