在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Distelli/graphql-apigen开源软件地址(OpenSource Url):https://github.com/Distelli/graphql-apigen开源编程语言(OpenSource Language):Java 97.3%开源软件介绍(OpenSource Introduction):graphql-apigenGenerate Java APIs with GraphQL Schemas in order to facilitate "schema first" development. Posts ExampleCreate a file to define your schema. In this example we are creating the type Author @java(package:"com.distelli.posts") {
id: Int! # the ! means that every author object _must_ have an id
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}
type Post @java(package:"com.distelli.posts") {
id: Int!
title: String
author: Author
votes: Int
}
# the schema allows the following query:
type QueryPosts @java(package:"com.distelli.posts") {
posts: [Post]
}
input InputPost @java(package:"com.distelli.posts") {
title: String
authorId: Int!
}
# this schema allows the following mutation:
type MutatePosts @java(package:"com.distelli.posts") {
createPost(post:InputPost): Post
upvotePost(
postId: Int!
): Post
} Notice that we annotate the types with a java package name. The above schema
will generate the following java interfaces in
The Each of these interfaces also have a default inner class named Any field that takes arguments will cause a Any field that does NOT take arguments will generate method names prefixed with "get". Finally, the above schema also generates a Guice module Putting this all together, we can implement the public class QueryPostsImpl implements QueryPosts {
private Map<Integer, Post> posts;
public QueryPostsImpl(Map<Integer, Post> posts) {
this.posts = posts;
}
@Override
public List<Post> getPosts() {
return new ArrayList<>(posts.values());
}
} ...and the public class MutatePostsImpl implements MutatePosts {
private AtomicInteger nextPostId = new AtomicInteger(1);
private Map<Integer, Post> posts;
public MutatePostsImpl(Map<Integer, Post> posts) {
this.posts = posts;
}
@Override
public Post createPost(MutatePosts.CreatePostArgs args) {
InputPost req = args.getPost();
Post.Builder postBuilder = new Post.Builder()
.withTitle(req.getTitle())
.withAuthor(new Author.Unresolved(req.getAuthorId()));
Post post;
synchronized ( posts ) {
Integer id = nextPostId.incrementAndGet();
post = postBuilder.withId(id).build();
posts.put(id, post);
}
return post;
}
@Override
public Post upvotePost(MutatePosts.UpvotePostArgs args) {
synchronized ( posts ) {
Post post = posts.get(args.getPostId());
if ( null == post ) {
throw new NoSuchEntityException("PostId="+args.getPostId());
}
Post upvoted = new Post.Builder(post)
.withVotes(post.getVotes()+1)
.build();
posts.put(args.getPostId(), upvoted);
return upvoted;
}
}
} ...and the public class AuthorResolver implements Author.Resolver {
private Map<Integer, Author> authors;
public AuthorResolver(Map<Integer, Author> authors) {
this.authors = authors;
}
@Override
public List<Author> resolve(List<Author> unresolvedList) {
List<Author> result = new ArrayList<>();
for ( Author unresolved : unresolvedList ) {
// In a real app we would check if it is instanceof Author.Unresolved
result.add(authors.get(unresolved.getId()));
}
return result;
}
} ...and the public class PostResolver implements Post.Resolver {
private Map<Integer, Post> posts;
public PostResolver(Map<Integer, Post> posts) {
this.posts = posts;
}
@Override
public List<Post> resolve(List<Post> unresolvedList) {
List<Post> result = new ArrayList<>();
for ( Post unresolved : unresolvedList ) {
if ( null == unresolved ) {
result.add(null);
} else {
result.add(posts.get(unresolved.getId()));
}
}
return result;
}
} ...and you can use Guice to wire it all together as such (see below on using this from Spring): public class MainModule implements AbstractModule {
@Override
protected void configure() {
// Create the "data" used by the implementations:
Map<Integer, Post> posts = new LinkedHashMap<>();
Map<Integer, Author> authors = new LinkedHashMap<>();
// Install the generated module:
install(new PostsModule());
// Declare our implementations:
bind(Author.Resolver.class)
.toInstance(new AuthorResolver(authors));
bind(Post.Resolver.class)
.toInstance(new PostResolver(posts));
bind(MutatePosts.class)
.toInstance(new MutatePostsImpl(posts));
bind(QueryPosts.class)
.toInstance(new QueryPostsImpl(posts));
}
} ...and to use it: public class GraphQLServlet extends HttpServlet {
private static ObjectMapper OM = new ObjectMapper();
private GraphQL graphQL;
@Inject
protected void GraphQLServlet(Map<String, GraphQLType> types) {
GraphQLSchema schema = GraphQLSchema.newSchema()
.query((GraphQLObjectType)types.get("QueryPosts"))
.mutation((GraphQLObjectType)types.get("MutatePosts"))
.build(new HashSet<>(types.values()));
graphQL = new GraphQL(schema, new BatchedExecutionStrategy());
}
protected void service(HttpServletRequest req, HttpServletResponse resp) {
ExecutionResult result = graphQL.execute(req.getParameter("query"));
OM.writeValue(resp.getOutputStream(), result);
}
} This example is also a unit test which can be found here Using Spring instead of GuiceIf you want to use Spring to wire the components together instead of Guice, you need to
instruct Spring to include the generated code in a package-scan. Spring will find the For example, if your code was generated into the package @ComponentScan("com.distelli.posts")
@Configuration
public class MyAppConfig {
...
} To generate a mapping similar to the guice code above, you can add this to your spring configuration: @Bean
public Map<String, GraphQLType> graphqlTypeMap(List<Provider<? extends GraphQLType>> typeList) {
return typeList.stream().map(Provider::get).collect(Collectors.toMap(GraphQLType::getName, Function.identity()));
} This will take any GraphQLTypes and generate a map of their string name to their implementation. Getting startedHow to use the latest release with MavenGenerate the code with the following maven: <project ...>
...
<properties>
<apigen.version>4.0.0</apigen.version>
</properties>
<build>
<plugins>
...
<plugin>
<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen</artifactId>
<version>${apigen.version}</version>
<configuration>
<!-- Optional. This is only needed when using Guice -->
<guiceModuleName>com.example.my.MyGuiceModule</guiceModuleName>
<!-- Optional. This is only needed if you omit the @java(package:"...")
annotations from your schema types. Using this feature
also means your GraphQL schema can NOT be depended upon
by GraphQL schemas defined in other maven projects. See:
https://github.com/Distelli/graphql-apigen/issues/5#issuecomment-275923555
-->
<defaultPackageName>com.example.my</defaultPackageName>
<!-- Optional. Location of your schema file(s). Default is ${project.basedir}/schema. The
expected extension is *.graphql. -->
<sourceDirectory>schema/folder</sourceDirectory>
<!-- Optional. Output folder for Java source. Default is ${project.basedir}/target/generated-sources/apigen.
-->
<outputDirectory>output/folder</outputDirectory>
</configuration>
<executions>
<execution>
<id>java-apigen</id>
<goals>
<goal>apigen</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
...
<!-- Required by the generated code -->
<dependency>
<groupId>com.distelli.graphql</groupId>
<artifactId>graphql-apigen-deps</artifactId>
<version>${apigen.version}</version>
</dependency>
<!-- Optional, dependencies if using Guice for Dependency Injection -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
</project> Be sure to replace the values above with the correct values (and remove unnecessary configuration properties if the defaults are satisfactory). Customizing the OutputYou can customize the generated Java source by copying the graphql-apigen.stg file to the base directory of your project and making any necessary changes. The plugin will automatically use it instead of the one distributed with the library. The template uses the StringTemplate template language. The model used for the template is defined in STModel.java. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论