Use Graphql Java Annotations framework to realize the data interaction between the Java library and the front end
Title: Use GraphQL Java Annotations framework to realize the data interaction between the Java class library and the front end
Introduction:
Graphql is a specification for query language and runtime actuators for API.It allows front -end applications to define its data requirements and reduce the number of network requests.Graphql Java Annitations is an annotation framework for defining Graphql mode and query in the Java class.This article will introduce how to use Graphql Java Annotations framework to achieve data interaction between Java libraries and front -end libraries.
1. Introduce dependencies
First of all, you need to introduce Graphql Java Annotations framework in your Java project.You can add the following dependencies in the configuration file of your construction tool (such as Maven or Gradle):
Maven:
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.11.0</version>
</dependency>
Gradle:
groovy
implementation 'com.graphql-java-kickstart:graphql-java-tools:5.11.0'
2. Create Graphql SCHEMA
Before starting, you need to define Graphql Schema.You can implement this by using the annotations provided by the GraphQL Java Annotation Step framework.The following is a basic example:
@GraphQLSchema
public class MyGraphQLSchema {
@GraphQLQuery
public List<Book> getBooks() {
// Returns the list of BOOK objects obtained from the database or other data source
}
@GraphQLMutation
public Book addBook(String title, String author) {
// Create a new book object and return
}
}
In this example, we define a Graphql Schema and use the `@Graphqlquery` and@Graphqlmutation` to define the operation of the book that can be queried and adding a new book.
3. Create Graphql data loader
Graphql data loader is used to load data from the data source.You can implement a customized data loader class, and then use the Graphql Java Annotations framework `@Graphqldataloader` to quote it.The following is an example:
@GraphQLComponent
public class BookDataLoader implements BatchDataLoader<Integer, Book> {
@Override
public CompletionStage<List<Book>> load(List<Integer> keys) {
// Load the BOOK object list according to KEYS from the database or other data source
}
}
In this example, we define a data loader that loads Book objects from the database.
4. Start GraphQL server
Finally, you need to start a GraphQL server to provide API.You can use the `graphql.servlet.SimpleGraphqlServlet` class provided by the GraphQL Java Annotations framework to achieve this.The following is an example:
@WebServlet(urlPatterns = "/graphql")
public class MyGraphQLServlet extends graphql.servlet.SimpleGraphQLServlet {
public MyGraphQLServlet() {
super(buildSchema());
}
private static GraphQL buildSchema() {
return GraphQL.newGraphQL(MyGraphQLSchema.class)
.build();
}
}
In this example, we created a Servlet class and covered the constructor of the `SIMPLEGRAPHQLSERVLET` to load our previously defined Graphql Schema.
5. Configure the service container
Finally, you need to deploy your Graphql Servlet into a Servlet container.You can use the Java Web server (such as Apache Tomcat) or Spring Boot to complete this step.
For the Java Web server, you can pack your Java project into a war file according to the server's documentation and deploy the War file to the server.
For Spring Boot, you can create a Spring Boot application and use the `@Servletcomponentscan` Note on the` Application` class to scan your service class.Then, run your Spring Boot application.
Summarize:
By using the GraphQL Java Annotations framework, you can easily define the GraphQL mode and query in the Java class library, and realize the data interaction between the Java library and the front end.By learning this article, you should be able to start using Graphql Java Annotations framework and build a Java application with strong data query capabilities.