Skip to content

Commit 7b9577d

Browse files
authored
apply more graphql features (#13)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent e38d857 commit 7b9577d

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

e2e/test-suite.yaml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ items:
1818
statusCode: 400
1919
- name: toLower
2020
request:
21-
api: /lower?text=Hello
21+
api: /lower
22+
query:
23+
text: Hello
2224
header:
2325
Authorization: "{{ .param.auth }}"
2426
expect:
@@ -42,12 +44,26 @@ items:
4244
Content-Type: application/json
4345
body: |
4446
{
45-
"query": "query xxx {\n bookById(id: \"book-1\") {\n id\n name\n }\n}",
46-
"operationName": "xxx"
47+
"query": "query xxx($id: ID) {\n bookById(id: $id) {\n id\n name\n }\n}",
48+
"operationName": "xxx",
49+
"variables": {"id": "book-1"}
4750
}
4851
expect:
4952
bodyFieldsExpect:
5053
data.bookById.name: Effective Java
54+
- name: allBooks
55+
request:
56+
api: /graphql
57+
method: POST
58+
header:
59+
Content-Type: application/json
60+
body: |
61+
{
62+
"query": "query xxx { books { name } }"
63+
}
64+
expect:
65+
verify:
66+
- len(data.data.books) >= 3
5167
- name: queryBookById-not-found
5268
request:
5369
api: /graphql
@@ -62,3 +78,13 @@ items:
6278
expect:
6379
verify:
6480
- data.bookById == nil
81+
- name: addBook
82+
request:
83+
api: /graphql
84+
method: POST
85+
header:
86+
Content-Type: application/json
87+
body: |
88+
{
89+
"query": "mutation size { addBook(name: \"name\"\n pageCount: 1)}"
90+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package io.github.devopsws.demo.model;
22

33
import java.util.Arrays;
4+
import java.util.ArrayList;
45
import java.util.List;
56

67
public record Book (String id, String name, int pageCount, String authorId) {
78

8-
private static List<Book> books = Arrays.asList(
9+
private static List<Book> books = new ArrayList<Book>(Arrays.asList(
910
new Book("book-1", "Effective Java", 416, "author-1"),
1011
new Book("book-2", "Hitchhiker's Guide to the Galaxy", 208, "author-2"),
1112
new Book("book-3", "Down Under", 436, "author-3")
12-
);
13+
));
1314

1415
public static Book getById(String id) {
1516
return books.stream()
1617
.filter(book -> book.id().equals(id))
1718
.findFirst()
1819
.orElse(null);
1920
}
21+
22+
public static Book[] allBooks() {
23+
return books.toArray(new Book[]{});
24+
}
25+
26+
public static int addBook(Book b) {
27+
books.add(new Book(books.size()+"", b.name, b.pageCount, b.authorId));
28+
return books.size();
29+
}
2030
}

src/main/java/io/github/devopsws/demo/service/GraphQL.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.graphql.data.method.annotation.Argument;
44
import org.springframework.graphql.data.method.annotation.QueryMapping;
5+
import org.springframework.graphql.data.method.annotation.MutationMapping;
56
import org.springframework.graphql.data.method.annotation.SchemaMapping;
67
import org.springframework.stereotype.Controller;
78
import io.github.devopsws.demo.model.Book;
@@ -16,4 +17,14 @@ public class GraphQL {
1617
public Book bookById(@Argument String id) {
1718
return Book.getById(id);
1819
}
20+
21+
@QueryMapping
22+
public Book[] books() {
23+
return Book.allBooks();
24+
}
25+
26+
@MutationMapping
27+
public int addBook(@Argument String name, @Argument int pageCount) {
28+
return Book.addBook(new Book("", name, pageCount, ""));
29+
}
1930
}

src/main/resources/graphql/schema.graphqls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
type Query {
22
bookById(id: ID): Book
3+
books: [Book]
4+
}
5+
6+
type Mutation {
7+
addBook(name: String, pageCount: Int!): Int
38
}
49

510
type Book {

0 commit comments

Comments
 (0)