4. GraphQL Server实现——基于SpringBoot+GraphQL
在实现一个GraphQL Server之前,建议大家先去看看一个具体的GraphQL查询语句是如何执行的,由于理论的内容太多,请大家具体参阅:
https://graphql.org/learn/execution/
本章节项目案例及部分代码参考自:
https://www.graphql-java.com/documentation/v14/
4.1 SpringBoot简介
SpringBoot应该是目前基于Java语言的最重要的一个框架了,也是Java开源框架中的一个具有划时代意义的产品。结合Java面向对象的语言特性和Spring的AOP软件工程思想,应该说SpringBoot是开发大型、复杂的前后端分离的软件的不二选择。特别是由Java衍生出的Scala结合Spark、Hadoop等大数据平台,为打造能力更强的“大后端”提供了可能性。
下面我们就结合一个具体的例子,来看看如何使用SpringBoot来开发一个我们自己的GraphQL Server。
4.2 开发平台和开发工具
Java开发工具:IntellJ IDEA,一个能够让我这个用了十多年Eclipse的转投IntellJ自然尤其自身的优势。
数据库:MySQL
数据库管理工具:MySQL Workbench
4.3 具体开发步骤
4.3.1 MySQL数据库创建
CREATE DATABASE /*!32312 IF NOT EXISTS*/`graphql` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `graphql`;
/*Table structure for table `author` */
DROP TABLE IF EXISTS `author`;
CREATE TABLE `author` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Key word',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created time',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Updated time',
`first_name` varchar(50) DEFAULT NULL COMMENT 'firstName',
`last_name` varchar(50) DEFAULT NULL COMMENT 'lastName',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Table structure for table `book` */
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Key word',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created time',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Updated time',
`title` varchar(50) DEFAULT NULL COMMENT 'Title',
`author_id` bigint(20) NOT NULL,
`isbn` varchar(255) DEFAULT NULL,
`page_count` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
执行完上述代码之后,在MySQL中能创建一个名为graphql的database。它包含两个表格“author”和“book”。
创建完成之后,schema的结构如下:
4.3.2 在IntellJ中创建SpringBoot项目
在IntellJ中,选择File=>new=>project,在new project对话框中,选择“Spring Initia”,在“Project SDK”中建议选择“1.8”,因为本人在实际开发过程中SDK 11曾经出现过很多兼容性问题。
单击“Next”
在这一步就按默认的选项,Java version建议选“8”,单击“Next”
在这一步我们需要选择项目当中需要用的的dependecies:
在本项目中,需要用到以下dependencies:
-
Developer Tools
-
Web
-
Template Engine
-
SQL.
在这里我们要同时添加 Spring Data JPA和MySQL Driver,分别用于创建数据中间层和MySQL的驱动
-
最后为项目命名并选择存储位置
4.3.3 在IntellJ中添加GraphQL plugin
File=>setting,在setting对话框中选择plugins,确认添加了JS GraphQL plugin
4.3.4 添加GraphQL的dependencies
目前在SpringBoot中对GraphQL支持比较好的Package就是graphql-java-kickstart了,它集成了相当多的有用工具,比原生的GraphQL-Java package要好用很多,而且在其中就集成了GraphQL Playground调试工具,测试GraphQL语句非常方便。详细请参阅:https://github.com/graphql-java-kickstart/graphql-spring-boot
添加完上述所需dependencies后,完整的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.4.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_graphql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_graphql</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<kotlin.version>0.3.70</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>