Skip to content

Commit

Permalink
apply more graphql features (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Dec 18, 2023
1 parent e38d857 commit 7b9577d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
32 changes: 29 additions & 3 deletions e2e/test-suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ items:
statusCode: 400
- name: toLower
request:
api: /lower?text=Hello
api: /lower
query:
text: Hello
header:
Authorization: "{{ .param.auth }}"
expect:
Expand All @@ -42,12 +44,26 @@ items:
Content-Type: application/json
body: |
{
"query": "query xxx {\n bookById(id: \"book-1\") {\n id\n name\n }\n}",
"operationName": "xxx"
"query": "query xxx($id: ID) {\n bookById(id: $id) {\n id\n name\n }\n}",
"operationName": "xxx",
"variables": {"id": "book-1"}
}
expect:
bodyFieldsExpect:
data.bookById.name: Effective Java
- name: allBooks
request:
api: /graphql
method: POST
header:
Content-Type: application/json
body: |
{
"query": "query xxx { books { name } }"
}
expect:
verify:
- len(data.data.books) >= 3
- name: queryBookById-not-found
request:
api: /graphql
Expand All @@ -62,3 +78,13 @@ items:
expect:
verify:
- data.bookById == nil
- name: addBook
request:
api: /graphql
method: POST
header:
Content-Type: application/json
body: |
{
"query": "mutation size { addBook(name: \"name\"\n pageCount: 1)}"
}
14 changes: 12 additions & 2 deletions src/main/java/io/github/devopsws/demo/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package io.github.devopsws.demo.model;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

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

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

public static Book getById(String id) {
return books.stream()
.filter(book -> book.id().equals(id))
.findFirst()
.orElse(null);
}

public static Book[] allBooks() {
return books.toArray(new Book[]{});
}

public static int addBook(Book b) {
books.add(new Book(books.size()+"", b.name, b.pageCount, b.authorId));
return books.size();
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/github/devopsws/demo/service/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import io.github.devopsws.demo.model.Book;
Expand All @@ -16,4 +17,14 @@ public class GraphQL {
public Book bookById(@Argument String id) {
return Book.getById(id);
}

@QueryMapping
public Book[] books() {
return Book.allBooks();
}

@MutationMapping
public int addBook(@Argument String name, @Argument int pageCount) {
return Book.addBook(new Book("", name, pageCount, ""));
}
}
5 changes: 5 additions & 0 deletions src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
type Query {
bookById(id: ID): Book
books: [Book]
}

type Mutation {
addBook(name: String, pageCount: Int!): Int
}

type Book {
Expand Down

0 comments on commit 7b9577d

Please sign in to comment.