GraphQL is a query language for the API, through which you can obtain data on demand. Spring recently released the Spring GraphQL project, which combines GraphQL with Spring. You can use GraphQL via Spring Boot

Introduction of depend on

	<dependencies>
		<dependency>
			<groupId>org.springframework.experimental</groupId>
			<artifactId>graphql-spring-boot-starter</artifactId>
			<version>1.0.0 - the SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
Copy the code

Write yML configuration files

server:
  port: 8888
spring:
  graphql:
    schema:
      printer:
        enabled: true
    locations: /graphql  Specifies the location of the schema file, which defaults to resources under graphQL folder
Copy the code

Write a schema

Schema: Defines data structures, types, and relationships. The types defined in schema correspond to entities in Java

Type: corresponds to an entity object in Java

Type Query: Defines the Query. The Query is performed according to the definition in Query. It does not need to be mapped to A Java object and must exist in the system

Create a schema.graphqls file in the resources/graphql directory and write the following:

# define type Query {greeting: Stringauthor(id: ID!) # Define an Author object that corresponds to a Java Author object with type {Author}id: ID!
    name: String!
    sex: SexEnum
    books: [Book]}bookName: String!
    publish: Boolean! SexEnum SexEnum {man, woman}Copy the code

Writing entity classes

@Data
public class Author {

	private Integer id;

	private String name;

	private SexEnum sex;

	private List<Book> books;
}


@Data
public class Book {

	private String bookName;

	private boolean publish;

	private float price;
}

public enum SexEnum {

    man, woman;
}
Copy the code

Write the query

@Component
public class CustomizeRuntimeWiring implements RuntimeWiringBuilderCustomizer {

    @Override
    public void customize(RuntimeWiring.Builder builder) {
        
        builder.type("Query", typeWiring -> {
            
            // Query greeting and return Hello
            typeWiring.dataFetcher("greeting", env -> "hello");

            // Query the author
            typeWiring.dataFetcher("author", env -> {

                Integer id = Integer.valueOf(env.getArgument("id"));

                Author author = new Author();
                author.setId(id);
                author.setName("Xiao Ming");
                author.setSex(SexEnum.man);

                Book book1 = new Book();
                book1.setBookName("Unbeatable Fist thirteen.");
                book1.setPublish(false);
                book1.setPrice(new SecureRandom().nextFloat());

                Book book2 = new Book();
                book2.setBookName("The Thirteen Swords of Dugu");
                book2.setPublish(true);
                book2.setPrice(new SecureRandom().nextFloat());

                author.setBooks(Arrays.asList(book1, book2));

                return author;
            });

            returntypeWiring; }); }}Copy the code

Start project query

Visit http://localhost:8888/graphiql to enter a query page after launch

The query

Query the greeting

Querying author Information

Search for authors and books