graphql
parent
31bd63b9ad
commit
3b55c97e0f
@ -0,0 +1,31 @@
|
||||
package com.xit.biz.sample.graphql;
|
||||
|
||||
//import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import com.xit.biz.sample.graphql.dto.PostResponse;
|
||||
import com.xit.biz.sample.graphql.entity.Post;
|
||||
import com.xit.biz.sample.graphql.repository.AuthorRepository;
|
||||
import com.xit.biz.sample.graphql.repository.PostRepository;
|
||||
import graphql.kickstart.tools.GraphQLMutationResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MyMutation implements GraphQLMutationResolver {
|
||||
private final PostRepository postRepository;
|
||||
private final AuthorRepository authorRepository;
|
||||
|
||||
public PostResponse writePost(String title, String text, String category) {
|
||||
//public Post writePost(String title, String text, String category) {
|
||||
Post post = new Post();
|
||||
post.setTitle(title);
|
||||
post.setText(text);
|
||||
post.setCategory(category);
|
||||
//post.setAuthor(authorRepository.getOne(1L));
|
||||
|
||||
final Post save = postRepository.save(post);
|
||||
|
||||
return PostResponse.from(save);
|
||||
//return save;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.xit.biz.sample.graphql;
|
||||
|
||||
//import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
import com.xit.biz.sample.graphql.dto.PostResponse;
|
||||
import com.xit.biz.sample.graphql.entity.Post;
|
||||
import com.xit.biz.sample.graphql.repository.PostRepository;
|
||||
import graphql.kickstart.tools.GraphQLQueryResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MyQuery implements GraphQLQueryResolver {
|
||||
private final PostRepository postRepository;
|
||||
|
||||
public List<PostResponse> getRecentPosts(int count, int offset) {
|
||||
final List<Post> all = postRepository.findAll();
|
||||
return PostResponse.from(all);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.xit.biz.sample.graphql;
|
||||
|
||||
//import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
import com.xit.biz.sample.graphql.dto.PostResponse;
|
||||
import com.xit.biz.sample.graphql.entity.Author;
|
||||
import com.xit.biz.sample.graphql.repository.AuthorRepository;
|
||||
import graphql.kickstart.tools.GraphQLResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PostResolver implements GraphQLResolver<PostResponse> {
|
||||
private final AuthorRepository authorRepository;
|
||||
|
||||
public Author getAuthor(PostResponse postResponse) {
|
||||
return authorRepository.findById(postResponse.getAuthor().getId()).orElseThrow(NullPointerException::new);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.xit.biz.sample.graphql.dto;
|
||||
|
||||
import com.xit.biz.sample.graphql.entity.Author;
|
||||
import com.xit.biz.sample.graphql.entity.Post;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PostResponse {
|
||||
private long id;
|
||||
private String title;
|
||||
private String text;
|
||||
private String category;
|
||||
private Author author;
|
||||
|
||||
public static List<PostResponse> from(Collection<Post> entities) {
|
||||
return entities.stream().map(PostResponse::from).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static PostResponse from(Post entity) {
|
||||
return PostResponse.builder()
|
||||
.id(entity.getId())
|
||||
.title(entity.getTitle())
|
||||
.text(entity.getText())
|
||||
.category(entity.getCategory())
|
||||
//.author(entity.getAuthor())
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.xit.biz.sample.graphql.entity;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode(of = "id")
|
||||
public class Author {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String thumbnail;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.xit.biz.sample.graphql.entity;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode(of = "id")
|
||||
public class Post {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
private String text;
|
||||
private String category;
|
||||
|
||||
// @ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
// @JoinColumn(name = "author_id")
|
||||
// private Author author;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.xit.biz.sample.graphql.repository;
|
||||
|
||||
import com.xit.biz.sample.graphql.entity.Author;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AuthorRepository extends JpaRepository<Author, Long> {
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.xit.biz.sample.graphql.repository;
|
||||
|
||||
import com.xit.biz.sample.graphql.entity.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
//@Query("SELECT p FROM Post p JOIN FETCH p.author")
|
||||
@Query("SELECT p FROM Post p")
|
||||
List<Post> findAll();
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
schema {
|
||||
query: Query,
|
||||
mutation: Mutation
|
||||
}
|
||||
|
||||
type CmmUser {
|
||||
cmmUserId: String
|
||||
userId: String!
|
||||
password: String!
|
||||
userName: String!
|
||||
email: String!
|
||||
userMbl: String
|
||||
providerType: String!
|
||||
profileImageUrl: String
|
||||
emailVerifiedYn: String!
|
||||
roleType: RoleType
|
||||
#created_by: String
|
||||
#modified_by: String
|
||||
}
|
||||
|
||||
enum RoleType {
|
||||
USER
|
||||
ADMIN
|
||||
GUEST
|
||||
}
|
||||
|
||||
input CmmUserDto {
|
||||
cmmUserId: String
|
||||
userId: String!
|
||||
password: String!
|
||||
userName: String!
|
||||
email: String!
|
||||
userMbl: String
|
||||
providerType: String!
|
||||
profileImageUrl: String
|
||||
emailVerifiedYn: String!
|
||||
roleType: RoleType
|
||||
#createdBy: String
|
||||
#modifiedBy: String
|
||||
}
|
||||
|
||||
# Root query
|
||||
type Query {
|
||||
getUserByUserId(userId: String!): CmmUser!
|
||||
}
|
||||
|
||||
|
||||
|
||||
# Root Mutation
|
||||
type Mutation {
|
||||
createUser(cmmUserDto: CmmUserDto!): CmmUser!
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue