SpringBoot和graphql实践

摘要 

属于 :API 标签: 发布于:2022-07-12 20:38:27

首先创建一个springboot工程,

springboot2.7已经支持graphql

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
    </parent>
	<groupId>com.example</groupId>
	<artifactId>springbootgraphqldemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springbootgraphqldemo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-graphql</artifactId>
		</dependency> 
		
		 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope> 
		</dependency>
		 
		<dependency>
			<groupId>org.springframework.graphql</groupId>
			<artifactId>spring-graphql-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		 <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-devtools</artifactId>
	        <optional>true</optional>
	    </dependency> 
        
       </dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>


graphql配置

src/main/resources/graphql/schema.graphqls

type Query {
    greeting(name: String! = "Spring"): String
    hello: String
    bookById(id: String): Book
    project(slug: ID!): Project
}

type Book {
    id: String
    name: String
    pageCount: Int
    author: Author
}

type Author {
	id: String
	name: String
}

""" A Project in the Spring portfolio """
type Project {
    """ Unique string id used in URLs """
    slug: ID!
    """ Project name """
    name: String!
    """ URL of the git repository """
    repositoryUrl: String!
    """ Current support status """
    status: ProjectStatus!
}

enum ProjectStatus {
    """ Actively supported by the Spring team """
    ACTIVE
    """ Supported by the community """
    COMMUNITY
    """ Prototype, not officially supported yet  """
    INCUBATING
    """ Project being retired, in maintenance mode """
    ATTIC
    """ End-Of-Lifed """
    EOL
}

springboot启动类

package com.example.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication { 
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

Hello

import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

@Controller
public class HelloController {
	 
	@QueryMapping
	public String hello() {
		System.out.println("hello print=====>");
		return "Hello, This is Graphql!";
	}
 
}

Greeting

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;

@Controller
public class GreetingController {
	@QueryMapping
	public String greeting(@Argument String name) {
		System.out.println("greeting print=====>"");
		return "Hello, " + name + "!";
	}

}

BookController

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;

@Controller
public class BookController {
	@QueryMapping
	public Book bookById(@Argument String id) {
		System.out.println("bookById---");
		Book book = new Book();
		book.setId(id);
		book.setName("Harry "+id);
		book.setPageCount(200);
//		Author author = new Author();
//		author.setId("999");
//		author.setName("Harry Potter");
//		book.setAuthor(author);
		return book;
	}
	
	@SchemaMapping
    public Author author(Book book) {
		Author author = new Author();
		author.setId("999");
		author.setName("Harry Potter");
		return author;
    }
}

Book实体类

import lombok.Data;

@Data
public class Book {
	private String id;
	private String name;
	private Integer pageCount;
	private Author author;
}

Author实体类

import lombok.Data;

@Data
public class Author {
	private String id;
	private String name;
}

访问

http://localhost:8080/graphiql?query=query%257B%250A%2520%2520hello%250A%257D&path=/graphql


http://localhost:8080/graphiql?query=query%257B%250A%2520%2520hello%250A%257D&path=/graphql


http://localhost:8080/graphiql?query=query%257B%250A%2520%2520hello%250A%257D&path=/graphql



postman: http://localhost:8080/graphql 

post

JSON

{"query":"query{\n  hello\n}","operationName":null}