介绍

上一篇介绍到用第三方库集成GraphQL,目前spring-graphql项目已经出到1.0.0-M3版本,属于内部预览版,此处尝鲜验证

集成过程

引入依赖

由于SpringBoot 2.6.0还未发布,因此需要引入较多依赖


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.experimental
        graphql-spring-boot-starter
        1.0.0-M3
    
    
    
        org.springframework.data
        spring-data-commons
        2.6.0-RC1
    
    
        org.springframework.data
        spring-data-jpa
        2.6.0-RC1
    
    
    
        org.hibernate.javax.persistence
        hibernate-jpa-2.1-api
        1.0.2.Final
    
    
    
        com.querydsl
        querydsl-jpa
        4.4.0
    
    
    
        com.querydsl
        querydsl-apt
        4.4.0
    
    
    
        javax.annotation
        javax.annotation-api
    
    
        com.querydsl
        querydsl-core
    
    
        com.querydsl
        querydsl-jpa
    
    
        mysql
        mysql-connector-java
        runtime
    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

引入里程碑仓库

由于还没有GA,所以要引入里程碑仓库


    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/milestone
    
    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/snapshot
        
            true
        
    

引入插件

由于使用了dsl动态生成,还要引入注解处理器


    
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                
                    
                        org.projectlombok
                        lombok
                    
                
            
        
        
        
            com.mysema.maven
            apt-maven-plugin
            1.1.3
            
                
                    
                        process
                    
                    
                        target/generated-sources/java
                        com.querydsl.apt.jpa.JPAAnnotationProcessor
                    
                
            
        
    

SpringBoot 配置

编写配置文件

spring:
  datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: "****"
      url: jdbc:mysql://localhost:3306/sakila?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL55Dialect
    properties:
      hibernate:
        format_sql: true
        use_sql_comments: true
  graphql:
    path: /graphql
    schema:
      locations: classpath:graphql/
      fileExtensions: .graphqls, .gqls
      printer:
        enabled: true
server:
  port: 8080

实体类

package top.fjy8018.graphsqldemo.entity;

import lombok.Data;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;

/**
 * 演员表实体类
 *
 * @author F嘉阳
 * @date 2021/11/5 10:34
 */
@Data
@Entity
@Table(name = "actor", schema = "sakila")
public class ActorEntity {
    @Id
    @Column(name = "actor_id", nullable = false)
    private Integer actorId;

    @Column(name = "first_name", nullable = false, length = 45)
    private String firstName;

    @Column(name = "last_name", nullable = false, length = 45)
    private String lastName;

    @Column(name = "last_update", nullable = false)
    private Date lastUpdate;

}

DAO

DAO直接继承dsl相关处理器,可自动配对增删改查方法,相比第三方库更加简便

package top.fjy8018.graphsqldemo.repository;

import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.graphql.data.GraphQlRepository;
import top.fjy8018.graphsqldemo.entity.ActorEntity;

/**
 * 演员表DAO
 *
 * @author F嘉阳
 * @date 2021/11/5 10:35
 */
@GraphQlRepository
public interface ActorEntityRepository extends
        CrudRepository, QuerydslPredicateExecutor {
}

Graphql资源定义

resources/graphql下定义接口和实体文件

接口声明和实体定义schema.graphqls

type Query {
    findOneActor(actorId : ID!): ActorEntity
    actorList: [ActorEntity]
}

type ActorEntity {
    actorId: ID!
    firstName: String!
    lastName: String!
    lastUpdate: String
}

接口定义acotrEntity.graphql

query findOneActor($id: ID!) {
    findOneActor(actorId: $id) {
        actorId
        firstName
        lastName
        lastUpdate
    }
}

query actorList {
    actorList{
        actorId
    }
}

启动测试

Spring-Graphql默认不包含可视化界面,此处使用postman进行测试

唯一查询

image-20211115151701244

列表查询

image-20211115151810849

总结

使用Spring官方组件好处在于和Spring生态集成度很高,如果本身就采用Jpa方式进行业务开发,迁移更加方便,需要开发的代码也很少。

样例源码地址

最后修改:2024 年 08 月 04 日
如果觉得我的文章对你有用,请随意赞赏