Преглед на файлове

Merge branch 'develop' of Remy-Liu/graphql-demo into master

Remy-Liu преди 2 години
родител
ревизия
443a3f15b3

+ 26 - 2
README.md

@@ -1,4 +1,28 @@
-# 工程简介
+# GraphQL示例
+## 如何运行
+### 初始化
+1. 在本地MySQL中创建数据库`graphql-demo`
+2. 进入init-db模块,修改`pom.xml`中`flyway`插件中数据库配置,改为自己本地的配置
+```xml
+<configuration>
+    <url>${db.url}</url>
+    <user>${username}</user>
+    <password>${password}</password>
+</configuration>
+```
+3.执行:
+```bash
+mvn flyway:migration
+```
+
+### 运行三个微服务
++ 进入`goods-service`模块,找到`GoodsApplication`,运行主函数,启动商品服务
++ 进入`user-service`模块,找到`UserApplication`,运行主函数,启动用户服务
++ 进入`order-service`模块,找到`OrderApplication`,运行主函数,启动订单服务
+
+### 运行GraphQL聚合服务
++ 进入`graphql-api`模块,找到`GraphqlApplication`,运行主函数,启动GraphQL服务
++ 访问 `http://localhost:8080/playground` 进入 `playground`, 开始使用。
+
 
-# 延伸阅读
 

+ 30 - 0
graphql-api/src/main/java/com/remy/graphql/resolvers/mutation/CommentMutation.java

@@ -0,0 +1,30 @@
+package com.remy.graphql.resolvers.mutation;
+
+import com.remy.common.helper.BeanHelper;
+import com.remy.common.module.CommentDTO;
+import com.remy.common.module.UserDTO;
+import com.remy.graphql.evn.RestAPIEnv;
+import graphql.kickstart.tools.GraphQLMutationResolver;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.concurrent.CompletableFuture;
+
+import static java.util.concurrent.CompletableFuture.supplyAsync;
+
+@Component
+@Slf4j
+public class CommentMutation implements GraphQLMutationResolver {
+
+    @Autowired
+    RestAPIEnv apiEnv;
+
+    CompletableFuture<CommentDTO> createComment(CommentDTO commentDTO) {
+        log.info("start create comment: " + BeanHelper.toJSON(commentDTO));
+        return supplyAsync(() ->
+                new RestTemplate().postForObject(apiEnv.getCommentsEndpoint(),
+                        commentDTO, CommentDTO.class));
+    }
+}

+ 0 - 1
graphql-api/src/main/resources/application.yml

@@ -7,7 +7,6 @@ graphql:
   extended-scalars: BigDecimal, DateTime, Date
   servlet:
     exception-handlers-enabled: true
-#    async-timeout: 500
   graphiql:
     enabled: true
   playground:

+ 2 - 0
graphql-api/src/main/resources/graphqls/mutation.graphqls

@@ -5,4 +5,6 @@ type Mutation{
     createOrder(input:OrderInput): Order!
     # 新增用户
     createUser(input:UserInput): User!
+    # 新增评论
+    createComment(input:CommentInput): Comment!
 }

+ 10 - 0
graphql-api/src/main/resources/graphqls/order.graphqls

@@ -26,10 +26,15 @@ type Order {
 }
 
 type Comment{
+    # 评论ID
     id: ID
+    # 订单号
     orderNo: String!
+    # 用户ID
     userID:ID!
+    # 商品编码
     goodsSerialNo:String!
+    # 评论内容
     content:String!
 }
 
@@ -38,4 +43,9 @@ input OrderInput{
     goodsSerialNo: String!
     # 用户ID
     userID: ID!
+}
+
+input CommentInput{
+    orderNo: String!
+    content: String!
 }

+ 43 - 0
init-db/pom.xml

@@ -0,0 +1,43 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>graphql-demo</artifactId>
+        <groupId>com.remy</groupId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>init-db</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.flywaydb</groupId>
+                <artifactId>flyway-maven-plugin</artifactId>
+                <version>7.11.1</version>
+                <configuration>
+                    <url><![CDATA[jdbc:mysql://localhost:3306/graphql-demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false]]></url>
+                    <user>graphql</user>
+                    <password>graphql</password>
+                    <configFileEncoding>UTF-8</configFileEncoding>
+                    <driver>com.mysql.cj.jdbc.Driver</driver>
+                </configuration>
+                <dependencies>
+                    <dependency>
+                        <groupId>mysql</groupId>
+                        <artifactId>mysql-connector-java</artifactId>
+                        <version>8.0.22</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 15 - 36
graphql-demo.sql → init-db/src/main/resources/db/migration/V1__init-db.sql

@@ -1,18 +1,3 @@
--- --------------------------------------------------------
--- 主机:                           127.0.0.1
--- 服务器版本:                        5.7.28-log - MySQL Community Server (GPL)
--- 服务器操作系统:                      Win64
--- HeidiSQL 版本:                  11.2.0.6213
--- --------------------------------------------------------
-
-/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
-/*!40101 SET NAMES utf8 */;
-/*!50503 SET NAMES utf8mb4 */;
-/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
-/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-
--- 导出  表 graphql-demo.t_goods 结构
 CREATE TABLE IF NOT EXISTS `t_goods` (
     `id` bigint(20) NOT NULL AUTO_INCREMENT,
     `create_time` datetime(6) DEFAULT NULL,
@@ -27,8 +12,7 @@ CREATE TABLE IF NOT EXISTS `t_goods` (
     PRIMARY KEY (`id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
 
--- 正在导出表  graphql-demo.t_goods 的数据:~6 rows (大约)
-/*!40000 ALTER TABLE `t_goods` DISABLE KEYS */;
+
 INSERT INTO `t_goods` (`id`, `create_time`, `update_time`, `description`, `detail`, `img`, `item_count`, `name`, `price`, `serial_no`) VALUES
 (1, '2021-06-21 16:49:12.094000', '2021-06-21 16:49:12.094000', 'GU女装起居套装(短袖)', 'GU女装起居套装(短袖)', 'https://graphql.cn/img/logo.svg', 99, 'GU女装', 74.00, 'pYmhjY58'),
 (2, '2021-06-21 16:52:30.200000', '2021-06-21 16:52:30.200000', '九阳电热饭盒可插电加热蒸煮便携', '九阳电热饭盒可插电加热蒸煮便携', 'https://graphql.cn/img/logo.svg', 125, '九阳电热饭盒', 159.00, '3qlihuWg'),
@@ -37,9 +21,8 @@ INSERT INTO `t_goods` (`id`, `create_time`, `update_time`, `description`, `detai
 (5, '2021-06-21 18:48:31.944000', '2021-06-21 18:48:31.944000', '现货德国WMF福滕宝实木盖研磨器2件装', 'Apple/苹果 iPhone 12', 'https://graphql.cn/img/logo.svg', 20, 'iPhone 12', 6299.00, 'INEnt8OV'),
 (6, '2021-06-21 18:52:05.178000', '2021-06-21 18:52:05.178000', '现货原版The Watercolorist\'s Essential Notebook水彩画家的必备笔记', '现货原版The Watercolorist\'s Essential Notebook水彩画家的必备笔记', 'https://graphql.cn/img/logo.svg', 66, 'Essential Notebook', 118.00, 'H5JJjJSj'),
 (7, '2021-06-21 18:53:48.153000', '2021-06-21 18:53:48.153000', 'LEGO乐高机械族42125 法拉利488GTE赛车拼搭积木男孩女孩收藏', 'LEGO乐高机械族42125 法拉利488GTE赛车拼搭积木男孩女孩收藏', 'https://graphql.cn/img/logo.svg', 66, '乐高法拉利488', 1869.00, '1K7gdmNM');
-/*!40000 ALTER TABLE `t_goods` ENABLE KEYS */;
 
--- 导出  表 graphql-demo.t_order 结构
+
 CREATE TABLE IF NOT EXISTS `t_order` (
     `id` bigint(20) NOT NULL AUTO_INCREMENT,
     `create_time` datetime(6) DEFAULT NULL,
@@ -53,8 +36,7 @@ CREATE TABLE IF NOT EXISTS `t_order` (
     PRIMARY KEY (`id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4;
 
--- 正在导出表  graphql-demo.t_order 的数据:~12 rows (大约)
-/*!40000 ALTER TABLE `t_order` DISABLE KEYS */;
+
 INSERT INTO `t_order` (`id`, `create_time`, `update_time`, `discount`, `discount_amount`, `goods_serial_no`, `order_no`, `original_amount`, `userid`) VALUES
 (13, '2021-06-22 10:23:06.601000', '2021-06-22 10:23:06.601000', 0.90, 66.60, 'pYmhjY58', '4bvd9i9pte', 74.00, 3),
 (14, '2021-06-22 10:23:38.583000', '2021-06-22 10:23:38.583000', 0.90, 143.10, '3qlihuWg', 'jnZ5nnHiK1', 159.00, 3),
@@ -69,9 +51,8 @@ INSERT INTO `t_order` (`id`, `create_time`, `update_time`, `discount`, `discount
 (23, '2021-06-22 10:24:57.448000', '2021-06-22 10:24:57.448000', 0.80, 59.20, 'pYmhjY58', 'wo9irE1ykw', 74.00, 5),
 (24, '2021-06-22 10:25:02.539000', '2021-06-22 10:25:02.539000', 0.80, 94.40, 'H5JJjJSj', 'RWLr4PxacG', 118.00, 5),
 (25, '2021-06-22 10:25:15.570000', '2021-06-22 10:25:15.570000', 0.80, 719.20, '5gqeGUEO', 'GivJBRVA7f', 899.00, 5);
-/*!40000 ALTER TABLE `t_order` ENABLE KEYS */;
 
--- 导出  表 graphql-demo.t_user 结构
+
 CREATE TABLE IF NOT EXISTS `t_user` (
     `id` bigint(20) NOT NULL AUTO_INCREMENT,
     `create_time` datetime(6) DEFAULT NULL,
@@ -88,15 +69,15 @@ CREATE TABLE IF NOT EXISTS `t_user` (
     UNIQUE KEY `UK_jhib4legehrm4yscx9t3lirqi` (`username`)
     ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
 
--- 正在导出表  graphql-demo.t_user 的数据:~2 rows (大约)
-/*!40000 ALTER TABLE `t_user` DISABLE KEYS */;
+
 INSERT INTO `t_user` (`id`, `create_time`, `update_time`, `age`, `birthday`, `gender`, `img`, `level`, `nickname`, `password`, `username`) VALUES
 (3, '2021-06-18 10:26:10.914000', '2021-06-18 10:26:10.914000', 30, '1985-02-22', 'MALE', 'https://www.baidu.com', 1, '小明', '3A548574B23819E3AF577B795AB8A147', 'remylee'),
 (4, '2021-06-21 19:02:34.540000', '2021-06-21 19:02:34.540000', 22, '1999-12-21', 'MALE', 'https://graphql.cn/img/logo.svg', 2, '杰克', '4D81FF7831351A0AEB58CC1719429002', 'jack'),
-(5, '2021-06-21 19:03:18.924000', '2021-06-21 19:03:18.924000', 21, '2000-02-12', 'FEMALE', 'https://graphql.cn/img/logo.svg', 3, '柔丝', '08DF54DA47C5E343DA8ABD541FEF87F6', 'rose');
-/*!40000 ALTER TABLE `t_user` ENABLE KEYS */;
+(5, '2021-06-21 19:03:18.924000', '2021-06-21 19:03:18.924000', 21, '2000-02-12', 'FEMALE', 'https://graphql.cn/img/logo.svg', 3, '柔丝', '08DF54DA47C5E343DA8ABD541FEF87F6', 'rose'),
+(6, '2021-07-11 13:24:14.252000', '2021-07-11 13:24:14.252000', 33, '1988-05-20', 'MALE', 'http://www.baidu.com', 1, '汤姆', '439B4C7705C02A550474CA2BCF7EA9AE', 'tom');
+
+
 
--- 导出  表 graphql-demo.user_addresses 结构
 CREATE TABLE IF NOT EXISTS `user_addresses` (
     `user_id` bigint(20) NOT NULL,
     `addresses` varchar(255) DEFAULT NULL,
@@ -104,15 +85,13 @@ CREATE TABLE IF NOT EXISTS `user_addresses` (
     CONSTRAINT `FKpws20r8ion9y4yrp5828uad38` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
--- 正在导出表  graphql-demo.user_addresses 的数据:~2 rows (大约)
-/*!40000 ALTER TABLE `user_addresses` DISABLE KEYS */;
 INSERT INTO `user_addresses` (`user_id`, `addresses`) VALUES
 (3, '上海'),
 (3, '北京'),
-(3, '重庆');
-/*!40000 ALTER TABLE `user_addresses` ENABLE KEYS */;
+(3, '重庆'),
+(6, '南京'),
+(6, '天津'),
+(6, '天水'),
+(6, '洛杉矶'),
+(6, '格尔木');
 
-/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
-/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
-/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
-/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;

+ 16 - 0
init-db/src/main/resources/db/migration/V2__add-comment.sql

@@ -0,0 +1,16 @@
+CREATE TABLE IF NOT EXISTS `t_comment`
+(
+    `id`              bigint(20) NOT NULL AUTO_INCREMENT,
+    `create_time`     datetime(6)  DEFAULT NULL,
+    `update_time`     datetime(6)  DEFAULT NULL,
+    `content`         varchar(255) DEFAULT NULL,
+    `goods_serial_no` varchar(255) DEFAULT NULL,
+    `order_no`        varchar(255) DEFAULT NULL,
+    `userid`          bigint(20)   DEFAULT NULL,
+    PRIMARY KEY (`id`)
+) ENGINE = InnoDB
+  DEFAULT CHARSET = utf8mb4;
+
+INSERT INTO `t_comment` (`id`, `create_time`, `update_time`, `content`, `goods_serial_no`, `order_no`, `userid`)
+VALUES
+(1, '2021-07-11 12:53:34.622000', '2021-07-11 12:53:34.622000', '这个东西非常好用哦', 'pYmhjY58', '4bvd9i9pte', 3);

+ 16 - 0
init-db/src/main/resources/db/migration/V3__add-attentions.sql

@@ -0,0 +1,16 @@
+CREATE TABLE `user_attention_ids` (
+  `user_id` bigint(20) NOT NULL,
+  `attention_ids` bigint(20) DEFAULT NULL,
+  KEY `FKjruoxsm5isj8qrkofpgwuhhqd` (`user_id`),
+  CONSTRAINT `FKjruoxsm5isj8qrkofpgwuhhqd` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+INSERT INTO `user_attention_ids` (`user_id`, `attention_ids`)
+VALUES
+	(3,4),
+	(3,5),
+	(4,3),
+	(5,4),
+	(6,3),
+	(6,4),
+	(6,5);

+ 1 - 5
pom.xml

@@ -10,6 +10,7 @@
         <module>base-module</module>
         <module>graphql-api</module>
         <module>common-module</module>
+        <module>init-db</module>
     </modules>
     <groupId>com.remy</groupId>
     <artifactId>graphql-demo</artifactId>
@@ -110,11 +111,6 @@
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
-            <dependency>
-                <groupId>com.graphql-java</groupId>
-                <artifactId>graphql-java-extended-scalars</artifactId>
-                <version>16.0.0</version>
-            </dependency>
             <dependency>
                 <groupId>org.apache.commons</groupId>
                 <artifactId>commons-lang3</artifactId>

+ 2 - 2
user-service/src/main/resources/application.yml

@@ -7,8 +7,8 @@ spring:
     username: graphql
     password: graphql
   jpa:
-    generate-ddl: true
-#    show-sql: true
+#    generate-ddl: true
+    show-sql: true
 #    properties:
 #      hibernate:
 #        format_sql: true

BIN
初识GraphQL.pptx