graphql
parent
8cb4fe3573
commit
443c77d363
@ -1,31 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
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;
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
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;
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
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,4 @@
|
||||
schema {
|
||||
query: Query,
|
||||
mutation: Mutation
|
||||
}
|
Loading…
Reference in New Issue