Add SPQR project.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.sprq;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Book {
|
||||
private Integer id;
|
||||
private String author;
|
||||
private String title;
|
||||
|
||||
public Book(Integer id, String author, String title) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Book book = (Book) o;
|
||||
return id.equals(book.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.sprq;
|
||||
|
||||
import io.leangen.graphql.annotations.GraphQLArgument;
|
||||
import io.leangen.graphql.annotations.GraphQLMutation;
|
||||
import io.leangen.graphql.annotations.GraphQLQuery;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class BookResolver {
|
||||
|
||||
@Autowired
|
||||
IBookService bookService;
|
||||
|
||||
@GraphQLQuery(name = "getBookWithTitle")
|
||||
public Book getBookWithTitle(@GraphQLArgument(name = "title") String title) {
|
||||
return bookService.getBookWithTitle(title);
|
||||
}
|
||||
|
||||
@GraphQLQuery(name = "getAllBooks", description = "Get all books")
|
||||
public List<Book> getAllBooks() {
|
||||
return bookService.getAllBooks();
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "addBook")
|
||||
public Book addBook(@GraphQLArgument(name = "newBook") Book book) {
|
||||
return bookService.addBook(book);
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "updateBook")
|
||||
public Book updateBook(@GraphQLArgument(name = "modifiedBook") Book book) {
|
||||
return bookService.updateBook(book);
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "deleteBook")
|
||||
public void deleteBook(@GraphQLArgument(name = "book") Book book) {
|
||||
bookService.deleteBook(book);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.sprq;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class BookService implements IBookService {
|
||||
|
||||
Set<Book> books = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public Book getBookWithTitle(String title) {
|
||||
return books.stream()
|
||||
.filter(book -> book.getTitle()
|
||||
.equals(title))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getAllBooks() {
|
||||
return books.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book addBook(Book book) {
|
||||
books.add(book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book updateBook(Book book) {
|
||||
books.remove(book);
|
||||
books.add(book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteBook(Book book) {
|
||||
return books.remove(book);
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.sprq;
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import graphql.GraphQLException;
|
||||
import graphql.schema.GraphQLSchema;
|
||||
import io.leangen.graphql.GraphQLSchemaGenerator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class GraphqlController {
|
||||
|
||||
private final GraphQL graphQL;
|
||||
|
||||
@Autowired
|
||||
public GraphqlController(BookResolver bookResolver) {
|
||||
GraphQLSchema schema = new GraphQLSchemaGenerator().withBasePackages("com.baeldung")
|
||||
.withOperationsFromSingleton(bookResolver)
|
||||
.generate();
|
||||
this.graphQL = new GraphQL.Builder(schema).build();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/graphql")
|
||||
public Map<String, Object> execute(@RequestBody Map<String, String> request, HttpServletRequest raw) throws GraphQLException {
|
||||
ExecutionResult result = graphQL.execute(request.get("query"));
|
||||
return result.getData();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.sprq;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IBookService {
|
||||
Book getBookWithTitle(String title);
|
||||
|
||||
List<Book> getAllBooks();
|
||||
|
||||
Book addBook(Book book);
|
||||
|
||||
Book updateBook(Book book);
|
||||
|
||||
boolean deleteBook(Book book);
|
||||
}
|
||||
Reference in New Issue
Block a user