commit
4a887226a1
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.graphql</groupId>
|
||||
<artifactId>graphql-spqr</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>graphql-java</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.leangen.graphql</groupId>
|
||||
<artifactId>graphql-spqr-spring-boot-starter</artifactId>
|
||||
<version>${graphql-spqr-spring-boot-starter-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot-version>2.6.2</spring-boot-version>
|
||||
<graphql-spqr-spring-boot-starter-version>0.0.6</graphql-spqr-spring-boot-starter-version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootApp.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.spqr;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.spqr;
|
||||
|
||||
import io.leangen.graphql.annotations.GraphQLArgument;
|
||||
import io.leangen.graphql.annotations.GraphQLMutation;
|
||||
import io.leangen.graphql.annotations.GraphQLQuery;
|
||||
import io.leangen.graphql.spqr.spring.annotations.GraphQLApi;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@GraphQLApi
|
||||
public class BookService implements IBookService {
|
||||
|
||||
Set<Book> books = new HashSet<>();
|
||||
|
||||
@GraphQLQuery(name = "getBookWithTitle")
|
||||
public Book getBookWithTitle(@GraphQLArgument(name = "title") String title) {
|
||||
return books.stream()
|
||||
.filter(book -> book.getTitle()
|
||||
.equals(title))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@GraphQLQuery(name = "getAllBooks", description = "Get all books")
|
||||
public List<Book> getAllBooks() {
|
||||
return books.stream()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "addBook")
|
||||
public Book addBook(@GraphQLArgument(name = "newBook") Book book) {
|
||||
books.add(book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "updateBook")
|
||||
public Book updateBook(@GraphQLArgument(name = "modifiedBook") Book book) {
|
||||
books.remove(book);
|
||||
books.add(book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@GraphQLMutation(name = "deleteBook")
|
||||
public boolean deleteBook(@GraphQLArgument(name = "book") Book book) {
|
||||
return books.remove(book);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.spqr;
|
||||
|
||||
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);
|
||||
}
|
|
@ -57,6 +57,11 @@
|
|||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.leangen.graphql</groupId>
|
||||
<artifactId>spqr</artifactId>
|
||||
<version>0.11.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.sprq;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class GraphqlControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
BookService bookService;
|
||||
|
||||
private static final String GRAPHQL_PATH = "/graphql";
|
||||
|
||||
@Test
|
||||
public void givenNoBooks_whenReadAll_thenStatusIsOk() throws Exception {
|
||||
|
||||
String getAllBooksQuery = "{ getAllBooks {id author title } }";
|
||||
|
||||
this.mockMvc.perform(post(GRAPHQL_PATH).content(toJSON(getAllBooksQuery))
|
||||
.contentType(
|
||||
MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.getAllBooks").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddBook_thenStatusIsOk() throws Exception {
|
||||
|
||||
String addBookMutation = "mutation { addBook(newBook: {id: 123, author: \"J.R.R. Tolkien\", "
|
||||
+ "title: \"The Lord of the Rings\"}) { id author title } }";
|
||||
|
||||
this.mockMvc.perform(post(GRAPHQL_PATH).content(toJSON(addBookMutation))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.addBook.id").value("123"))
|
||||
.andExpect(jsonPath("$.addBook.author").value("J.R.R. Tolkien"))
|
||||
.andExpect(jsonPath("$.addBook.title").value("The Lord of the Rings"));
|
||||
}
|
||||
|
||||
private String toJSON(String query) throws JSONException {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("query", query);
|
||||
return jsonObject.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue