-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
|
||
```graphql | ||
mutation { | ||
saveMember(body: { | ||
name: "멤버" | ||
age: 15 | ||
role: ADMIN | ||
}) { | ||
id | ||
age | ||
name | ||
role | ||
} | ||
} | ||
|
||
query { | ||
getMember(id:1) { | ||
id | ||
name | ||
age | ||
role | ||
} | ||
} | ||
|
||
query { | ||
getMemberList { | ||
id | ||
name | ||
age | ||
role | ||
} | ||
} | ||
``` | ||
|
||
|
||
# _Reference_ | ||
- [GraphQL official documentation](https://graphql.org/learn) | ||
- [GraphQL official documentation - schema](https://graphql.org/learn/schema/) | ||
- [Baeldung Blog - Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) | ||
- [tech blog1](https://danawalab.github.io/spring/2022/06/06/Spring-for-GraphQL.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...ng-for-graphql/src/test/java/com/example/demo/controller/MemberGraphQLControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.example.demo.controller; | ||
|
||
import com.example.demo.config.GraphQlConfig; | ||
import com.example.demo.domain.Member; | ||
import com.example.demo.domain.MemberRole; | ||
import com.example.demo.repository.MemberRepository; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureGraphQlTester; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.graphql.test.tester.GraphQlTester; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
@SpringBootTest | ||
@AutoConfigureGraphQlTester | ||
@Import(GraphQlConfig.class) | ||
class MemberGraphQLControllerTest { | ||
|
||
@Autowired | ||
private GraphQlTester graphQlTester; | ||
|
||
@Autowired | ||
private MemberRepository repository; | ||
|
||
|
||
@BeforeEach | ||
void addDummyData() { | ||
repository.save(new Member("member1", MemberRole.NORMAL, 10)); | ||
repository.save(new Member("member2", MemberRole.NORMAL, 20)); | ||
repository.save(new Member("member3", MemberRole.NORMAL, 30)); | ||
repository.save(new Member("member4", MemberRole.ADMIN, 40)); | ||
repository.save(new Member("member5", MemberRole.ADMIN, 50)); | ||
} | ||
|
||
@AfterEach | ||
void clear() { | ||
repository.deleteAll(); | ||
} | ||
|
||
|
||
@Test | ||
void getMember_thenReturnResponse1() { | ||
long id = repository.findAll().stream().findFirst().orElseThrow().getId(); | ||
|
||
String documentName = "getMember"; | ||
|
||
graphQlTester.documentName(documentName) | ||
.variable("id", id) | ||
.execute() | ||
.path("$") | ||
.matchesJson(expected(documentName)); | ||
} | ||
|
||
@Test | ||
void getMember_thenReturnResponse2() { | ||
long id = repository.findAll().stream().findFirst().orElseThrow().getId(); | ||
|
||
String query = String.format("{ getMember(id: %d) { id name age role }}", id); | ||
Member member = graphQlTester.document(query) | ||
.execute() | ||
.path("data.getMember") | ||
.entity(Member.class) | ||
.get(); | ||
|
||
assertThat(member).isNotNull(); | ||
assertThat(member.getAge()).isEqualTo(10); | ||
} | ||
|
||
|
||
@Test | ||
void getMemberList_thenReturnResponse1() { | ||
String documentName = "getMemberList"; | ||
|
||
List<Member> memberList = graphQlTester.documentName(documentName) | ||
.execute() | ||
.path("data.getMemberList[*]") | ||
.entityList(Member.class) | ||
.get(); | ||
|
||
assertThat(memberList.size()).isEqualTo(5); | ||
} | ||
|
||
@Test | ||
void saveMember_thenReturnResponse1() { | ||
String name = "멤버"; | ||
MemberRole role = MemberRole.ADMIN; | ||
int age = 15; | ||
|
||
HashMap<String, Object> map = new HashMap<>(); | ||
map.put("name", name); | ||
map.put("role", role); | ||
map.put("age", age); | ||
|
||
String documentName = "saveMember"; | ||
|
||
graphQlTester.documentName(documentName) | ||
.variable("body", map) | ||
.execute() | ||
.path("$") | ||
.matchesJson(expected(documentName)); | ||
} | ||
|
||
@SneakyThrows | ||
public static String expected(String fileName) { | ||
Path path = Paths.get("src/test/resources/graphql-response/" + fileName + "ExpectedResponse.json"); | ||
return new String(Files.readAllBytes(path)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring-for-graphql/src/test/resources/graphql-response/getMemberExpectedResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"data": { | ||
"getMember": { | ||
"name": "member1", | ||
"age": 10, | ||
"role": "NORMAL" | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
spring-for-graphql/src/test/resources/graphql-response/getMemberListExpectedResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"data": { | ||
"getMemberList": [ | ||
{ | ||
"name": "member1", | ||
"age": 10, | ||
"role": "NORMAL" | ||
}, | ||
{ | ||
"name": "member2", | ||
"age": 20, | ||
"role": "NORMAL" | ||
}, | ||
{ | ||
"name": "member3", | ||
"age": 30, | ||
"role": "NORMAL" | ||
}, | ||
{ | ||
"name": "member4", | ||
"age": 40, | ||
"role": "ADMIN" | ||
}, | ||
{ | ||
"name": "member5", | ||
"age": 50, | ||
"role": "ADMIN" | ||
} | ||
] | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring-for-graphql/src/test/resources/graphql-response/saveMemberExpectedResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"data": { | ||
"saveMember": { | ||
"age": 15, | ||
"name": "멤버", | ||
"role": "ADMIN" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
spring-for-graphql/src/test/resources/graphql-test/getMember.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
query getMember ($id: Long!) { | ||
getMember(id: $id) { | ||
name | ||
age | ||
role | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
spring-for-graphql/src/test/resources/graphql-test/getMemberList.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
query getMemberList { | ||
getMemberList { | ||
name | ||
age | ||
role | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
spring-for-graphql/src/test/resources/graphql-test/saveMember.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mutation saveMember ($body: CreateMemberRequestDto!) { | ||
saveMember(body: $body) { | ||
age | ||
name | ||
role | ||
} | ||
} |