Hiện tại e đang tìm hiểu thằng graphql nhưng khi e khởi tạo và chạy project thì không thể kết nối được với database, khi query hay mutation đều trả về null, mong mọi người chỉ ra lỗi sai cho e để e có thể sửa ạ, em cảm ơn.
Đây là cấu trúc PJ của e
Dưới đây là code của e:
Book.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "book")
@Getter
@Setter
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String description;
private boolean published;
private String publishedDate;
@ManyToOne
@JoinColumn(name = "author_id", nullable = false, updatable = false)
private Author author;
public Book() {
// TODO Auto-generated constructor stub
}
public Book(String title, String description, boolean published, String publishedDate, Author author) {
this.title = title;
this.description = description;
this.published = published;
this.publishedDate = publishedDate;
this.author = author;
}
@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", description=" + description + ", published=" + published
+ ", publishedDate=" + publishedDate + ", author=" + author + "]";
}
}
Author.java
Author.java
package com.example.model;
import javax.persistence.Id;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "author")
@Getter
@Setter
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private int age;
public Author() {
}
public Author(long id) {
this.id = id;
}
public Author(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Author [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
BookResokver.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLResolver;
import com.example.model.Author;
import com.example.model.Book;
import com.example.repository.AuthorRepository;
import com.example.repository.BookRepository;
@Component
public class BookResolver implements GraphQLResolver<Book>{
@Autowired
private AuthorRepository authorRepository;
public BookResolver(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}
public Author getAuthor(Book book) {
return authorRepository.findById(book.getAuthor().getId()).orElseThrow(null);
}
}
Mutation.java
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister.NotFoundException;
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.example.model.Author;
import com.example.model.Book;
import com.example.repository.AuthorRepository;
import com.example.repository.BookRepository;
@Component
public class Mutation implements GraphQLMutationResolver {
@Autowired
private AuthorRepository authorRepository;
private BookRepository bookReporitory;
public Mutation(AuthorRepository authorRepository, BookRepository bookReporitory) {
this.authorRepository = authorRepository;
this.bookReporitory = bookReporitory;
}
public Author createAuthor(String name, int age) {
Author author = new Author();
author.setName(name);
author.setAge(age);
authorRepository.save(author);
return author;
}
public Book createBook(String title, String description, boolean published, String publishedDate, Long authorId) {
Book book = new Book();
book.setAuthor(new Author(authorId));
book.setTitle(title);
book.setDescription(description);
book.setPublished(published);
book.setPublishedDate(publishedDate);
return book;
}
public boolean deleteBook(Long id) {
bookReporitory.deleteById(id);
return true;
}
public Book updateBook(Long id, String title, String description, boolean published, String publishedDate) throws NotFoundException {
Optional<Book> optionalBook = bookReporitory.findById(id);
if(optionalBook.isPresent()) {
Book book = optionalBook.get();
if(title != null)
book.setTitle(title);
if(description != null)
book.setDescription(description);
if(publishedDate != null)
book.setPublishedDate(publishedDate);
bookReporitory.save(book);
return book;
}
throw new NotFoundException();
}
}
Query.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.example.model.Author;
import com.example.model.Book;
import com.example.repository.AuthorRepository;
import com.example.repository.BookRepository;
@Component
public class Query implements GraphQLQueryResolver {
private AuthorRepository authorRepository;
private BookRepository bookRepository;
@Autowired
public Query(AuthorRepository authorRepository, BookRepository bookRepository) {
this.authorRepository = authorRepository;
this.bookRepository = bookRepository;
}
public Iterable<Author> findAllAuthors() {
return authorRepository.findAll();
}
public Iterable<Book> findAllBooks() {
return bookRepository.findAll();
}
// public long countAuthors() {
// return authorRepository.count();
//
// }
// public long countBooks() {
// return bookRepository.count();
//
// }
}
application.properties
server.port = 8085
spring.datasource.url=jdbc:postgresql://localhost:5432/graphqldb
spring.datasource.username=graphqluser_1
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto= update
# Graphql
graphql.servlet.mapping: /apis/graphql
graphql.servlet.enabled=true
graphql.servlet.corsEnabled=true