Graphql入门
程序员文章站
2023-11-11 17:22:22
Graphql入门这是一篇可以让没有任何graphql基础的人基本可以上手写出一套增删改查(偏后端,Springboot+jpa)一、首先要引入graphql的pom配置文件com.graphql-java graphql-spring-boot-starter 5.0.2
Graphql入门
这是一篇可以让没有任何graphql基础的人基本可以上手写出一套增删改查+模糊分页(偏后端,Springboot+jpa)
一、首先要引入graphql的pom配置文件
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
application配置:
graphiql:
enabled: true
二、在idea下载graphql插件,并在resources下创建graphql包以及graphql类型的配置文件
类似这样。
前期准备工作就差不多了
上主菜
三、主要的代码
controller:
// An highlighted block
@Component
@RequestMapping("query")
public class UserQueryResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
public List<User> findSearchUsers(User user,int page,int size){
return userService.findSearchUsers(user,page,size);
}
public User saveUser(User user){
User save = userService.save(user);
return userService.save(user);
}
/*public User updateUser(User user){
User save = userRepository.save(user);
return userRepository.u(user);
}*/
}
service:
@Component
public class UserServiceImpl implements UserService{
@Autowired
UserRepository userRepository;
@Override
public List<User> findSearchUsers(User user,int page,int size){
if (page==0){
page = 1;
}
if (size==0){
size=20;
}
Specification<User> mapwarp = createSpecification(user);
PageRequest pageRequest = PageRequest.of(page-1, size);
List<User> content = userRepository.findAll(mapwarp,pageRequest).getContent();
return content;
}
/**
* 动态条件构建
* @param user
* @return
*/
private Specification<User> createSpecification(User user) {
return new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<Predicate>();
if (!StringUtils.isEmpty(user.getUserName())) {
predicateList.add(cb.like(root.get("userName").as(String.class), "%"+(String)user.getUserName()+"%"));
}
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
}
jpa:
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {
}
entity实体类:
@Data
@ToString
pulic class User{
private String userId;
private String password;
private String userName;
private String userSex;
}
graphql 配置文件:
蓝色字体与方法名保持一致
type Query {
findSearchUsers(user:inputUser,page:Int,size:Int):[User]
type Mutation{
saveUser(user:Useradd!):User
}
type User {
userId: String
userName: String
userSex: String
}
input inputUser {
userId: String
userName: String
userSex: String
}
input Useradd {
userId: String
userName: String
userSex: String
}
四、前端查询
http://localhost:8080/graphiql
上面的界面就是graphql的可视化界面,出现这样一个界面已经成功了一大半了
查询用query,修改用mutation
查询
query{
findSearchUsers(user:{},page:1,size:5){
userId
userSex
userName
}
}
修改
mutation{
saveUser(user:{
userId:""
userSex:""
userName:""
})
{
userId
}
}
{
userId
}
是可以看返回的id
完结撒花✿✿ヽ(°▽°)ノ✿
本文地址:https://blog.csdn.net/ZraInCyy/article/details/107359066