# springboot-graphql-demo **Repository Path**: zsome/springboot-graphql-demo ## Basic Information - **Project Name**: springboot-graphql-demo - **Description**: springboot集成graphql示例 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2025-04-25 - **Last Updated**: 2025-04-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### spring boot with graphql + h2 演示 输入 http://localhost:8080/graphiql 在浏览器执行graphql查询~ ### 测试步骤 * 创建数据库 + 创建表
``` 1. 确保以下配置处于打开状态: spring.jpa.hibernate.ddl-auto = create spring.datasource.schema=classpath:sql/create.sql 2. 启动应用 ``` * 插入测试数据(也可以不插,使用mutation命令插入) ``` 1. 确保以下配置处于打开状态: spring.jpa.hibernate.ddl-auto = update spring.datasource.data=classpath:sql/insert.sql #spring.datasource.data=classpath:sql/insert.sql 2. 启动应用 ``` ### 测试用例 > 查询所有 ``` query { users{ id, mail, nickname, password, description } } ``` > 查询单个 ``` query { user(nickname: "test1") { id, nickname } } query { userOne(id: 1) { id, nickname } } ``` > 仅查询部分参数 目标方法实际有这么多参数few(Long id, String nickname, String mail, String password, String description) ``` query { few(id: 12, nickname: "test1", mail: "123") { id, nickname, mail } } ``` > 查询方法别名 ``` query { users: few(id: 12, nickname: "test1", mail: "123") { id, nickname, mail } } ``` > 传入数组参数查询 ``` query { getUserList(idList: ["1", "2", "3"]) { id, nickname } } ``` > 多表查询 ``` query { article(title: "活动") { id, author { id, mail, nickname } } } ``` > 包含枚举类 ``` query { user(nickname: "test1") { id, nickname, userTypeEnum{ type, text } } } ``` > 聚合查询 ``` // 错误示例 当字段指定为!时表示非空, 当非空时传出参数也不能为空值 query { user(nickname: "test1") { id nickname } article2(title: "test1") { id content } } // 正确示例 query { user(nickname: "test1") { id nickname } article2(title: "test1", content: "内容") { id content } } ``` > 传入查询对象 ``` 可通过postman的graphQL面板或http://localhost:8080/graphiql自带的UI界面测试 query ($bean: InputUser){ simpleUser(user: $bean) { id, nickname } } // Query Variables { "bean": { "id": 12, "nickname": "123" } } // 实际上上面的配置可转换为如下格式的json数据包 直接发送POST即可达到同样的效果 { "query": "query ($bean: InputUser){simpleUser(user: $bean) {id,nickname }}", "variables": "{ \"bean\": { \"id\": 12, \"nickname\": \"123\" }}" } ``` > 自定义标量 ``` query { user(nickname: "test1") { id, nickname, createTime, updateTime, issueTime } } ``` > 插入记录 ``` mutation { addUser(mail:"223", nickname:"dfsf", password:"dfdsfs") { id } } mutation { addArticle(title: "活动", content: "123", authorId: "1") { title, content } } ```