Skip to content

Commit a4a2e63

Browse files
graphql
1 parent ff62944 commit a4a2e63

File tree

10 files changed

+137
-4
lines changed

10 files changed

+137
-4
lines changed

build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ repositories {
1515
mavenCentral()
1616
}
1717

18+
if (hasProperty('buildScan')) {
19+
buildScan {
20+
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
21+
termsOfServiceAgree = 'yes'
22+
}
23+
}
24+
1825
dependencies {
1926
implementation 'org.springframework.boot:spring-boot-starter-graphql'
2027
implementation 'org.springframework.boot:spring-boot-starter-web'
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.amigoscode;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public record Author(Integer id,
8+
String name) {
9+
public static List<Author> authors = Arrays.asList(
10+
new Author(1, "Mama Samba"),
11+
new Author(2, "Jamila"),
12+
new Author(3, "Allah")
13+
14+
);
15+
16+
public static Optional<Author> getAuthorById(Integer id) {
17+
return authors.stream()
18+
.filter(b -> b.id.equals(id))
19+
.findFirst();
20+
}
21+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.amigoscode;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public record Book(Integer id,
8+
String name,
9+
Integer pageCount,
10+
Integer authorId) {
11+
public static List<Book> books = Arrays.asList(
12+
new Book(1, "Quran", 604, 3),
13+
new Book(2, "Harry Potter", 700, 2),
14+
new Book(3, "Foobar", 100, 1),
15+
new Book(4, "Spring Boot", 344, 2)
16+
);
17+
18+
public static Optional<Book> getBookById(Integer id) {
19+
return books.stream()
20+
.filter(b -> b.id.equals(id))
21+
.findFirst();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.amigoscode;
2+
3+
import org.springframework.graphql.data.method.annotation.Argument;
4+
import org.springframework.graphql.data.method.annotation.QueryMapping;
5+
import org.springframework.graphql.data.method.annotation.SchemaMapping;
6+
import org.springframework.stereotype.Controller;
7+
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
@Controller
12+
public class BookController {
13+
14+
@QueryMapping
15+
public List<Book> books() {
16+
return Book.books;
17+
}
18+
19+
@QueryMapping
20+
public Optional<Book> bookById(@Argument Integer id) {
21+
return Book.getBookById(id);
22+
}
23+
24+
@SchemaMapping
25+
public Optional<Author> author(Book book) {
26+
return Author.getAuthorById(book.authorId());
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.example.graphqpl;
1+
package com.amigoscode;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66
@SpringBootApplication
7-
public class GraphqplApplication {
7+
public class GraphQLApplication {
88

99
public static void main(String[] args) {
10-
SpringApplication.run(GraphqplApplication.class, args);
10+
SpringApplication.run(GraphQLApplication.class, args);
1111
}
1212

1313
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
spring.application.name=graphqpl
2+
spring.graphql.graphiql.enabled=true
3+
spring.graphql.path=/graphql
4+
spring.graphql.cors.allowed-origins=http://localhost:3000
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Query {
2+
books : [Book]
3+
bookById(id: Int): Book
4+
}
5+
6+
type Book {
7+
id: ID
8+
name: String
9+
pageCount: Int
10+
author: Author
11+
}
12+
13+
type Author {
14+
id: ID
15+
name: String
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.amigoscode;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
6+
import org.springframework.graphql.test.tester.GraphQlTester;
7+
8+
@GraphQlTest(BookController.class)
9+
class BookControllerTest {
10+
11+
@Autowired
12+
private GraphQlTester graphQlTester;
13+
14+
@Test
15+
void canGetBooks() {
16+
graphQlTester
17+
.documentName("books")
18+
.execute()
19+
.path("books")
20+
.entityList(Book.class)
21+
.hasSize(4);
22+
23+
}
24+
}

src/test/java/com/example/graphqpl/GraphqplApplicationTests.java renamed to src/test/java/com/amigoscode/GraphqplApplicationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.graphqpl;
1+
package com.amigoscode;
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query books {
2+
books {
3+
name
4+
pageCount
5+
id
6+
author {
7+
id
8+
name
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)