[JAVA-14663] Update Graphql code for Boot 2.7.x changes and move to new module (#12729)
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
|
||||
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;
|
||||
|
||||
@GraphQlTest(PostController.class)
|
||||
@Import(GraphqlConfiguration.class)
|
||||
class PostControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private GraphQlTester graphQlTester;
|
||||
|
||||
@Test
|
||||
void givenPosts_whenExecuteQueryForRecentPosts_thenReturnResponse() {
|
||||
String documentName = "recent_posts";
|
||||
|
||||
graphQlTester.documentName(documentName)
|
||||
.variable("count", 2)
|
||||
.variable("offset", 0)
|
||||
.execute()
|
||||
.path("$")
|
||||
.matchesJson(expected(documentName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNewPostData_whenExecuteMutation_thenNewPostCreated() {
|
||||
String documentName = "create_post";
|
||||
|
||||
graphQlTester.documentName(documentName)
|
||||
.variable("title", "New Post")
|
||||
.variable("text", "New post text")
|
||||
.variable("category", "category")
|
||||
.variable("authorId", "Author0")
|
||||
.execute()
|
||||
.path("createPost.id").hasValue()
|
||||
.path("createPost.title").entity(String.class).isEqualTo("New Post")
|
||||
.path("createPost.text").entity(String.class).isEqualTo("New post text")
|
||||
.path("createPost.category").entity(String.class).isEqualTo("category");
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String expected(String fileName) {
|
||||
Path path = Paths.get("src/test/resources/graphql-test/" + fileName + "_expected_response.json");
|
||||
return new String(Files.readAllBytes(path));
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = GraphqlApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
{
|
||||
"info": {
|
||||
"_postman_id": "910d9690-f629-4491-bbbd-adb30982a386",
|
||||
"name": "GraphQL collection",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
"name": "mutations",
|
||||
"item": [
|
||||
{
|
||||
"name": "createPost",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "graphql",
|
||||
"graphql": {
|
||||
"query": "mutation createPost ($title: String!, $text: String!, $category: String) {\n createPost (title: $title, text: $text, category: $category) {\n id\n title\n text\n category\n }\n}",
|
||||
"variables": "{\n \"title\": \"\",\n \"text\": \"\",\n \"category\": \"\"\n}"
|
||||
},
|
||||
"options": {
|
||||
"graphql": {}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:9090/springbootapp/graphql",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "9090",
|
||||
"path": [
|
||||
"springbootapp",
|
||||
"graphql"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
],
|
||||
"protocolProfileBehavior": {}
|
||||
},
|
||||
{
|
||||
"name": "queries",
|
||||
"item": [
|
||||
{
|
||||
"name": "get recent posts",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "graphql",
|
||||
"graphql": {
|
||||
"query": "{\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n text\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}",
|
||||
"variables": ""
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:9090/springbootapp/graphql",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "9090",
|
||||
"path": [
|
||||
"springbootapp",
|
||||
"graphql"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "recentPosts - variables",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "graphql",
|
||||
"graphql": {
|
||||
"query": "query recentPosts ($count: Int, $offset: Int) {\n recentPosts (count: $count, offset: $offset) {\n id\n title\n text\n category\n }\n}",
|
||||
"variables": "{\n \"count\": 1,\n \"offset\": 0\n}"
|
||||
},
|
||||
"options": {
|
||||
"graphql": {}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:9090/springbootapp/graphql",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "9090",
|
||||
"path": [
|
||||
"springbootapp",
|
||||
"graphql"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "get recent posts - raw",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "application/graphql",
|
||||
"type": "text"
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "query {\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}"
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:9090/springbootapp/graphql",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "9090",
|
||||
"path": [
|
||||
"springbootapp",
|
||||
"graphql"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
],
|
||||
"protocolProfileBehavior": {}
|
||||
}
|
||||
],
|
||||
"event": [
|
||||
{
|
||||
"listen": "prerequest",
|
||||
"script": {
|
||||
"id": "b54f267b-c450-4f2d-8105-2f23bab4c922",
|
||||
"type": "text/javascript",
|
||||
"exec": [
|
||||
""
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"listen": "test",
|
||||
"script": {
|
||||
"id": "00b575be-03d4-4b29-b137-733ead139638",
|
||||
"type": "text/javascript",
|
||||
"exec": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"variable": [
|
||||
{
|
||||
"id": "20a274e5-6d51-40d6-81cb-af9eb115b21b",
|
||||
"key": "url",
|
||||
"value": "",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"protocolProfileBehavior": {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
mutation createPost ($title: String!, $text: String!, $category: String, $authorId: String!) {
|
||||
createPost (title: $title, text: $text, category: $category, authorId: $authorId) {
|
||||
id
|
||||
title
|
||||
text
|
||||
category
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
query recentPosts ($count: Int, $offset: Int) {
|
||||
recentPosts (count: $count, offset: $offset) {
|
||||
id
|
||||
title
|
||||
text
|
||||
category
|
||||
author {
|
||||
id
|
||||
name
|
||||
thumbnail
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"data": {
|
||||
"recentPosts": [
|
||||
{
|
||||
"id": "Post00",
|
||||
"title": "Post 0:0",
|
||||
"category": "Post category",
|
||||
"text": "Post 0 + by author 0",
|
||||
"author": {
|
||||
"id": "Author0",
|
||||
"name": "Author 0",
|
||||
"thumbnail": "http://example.com/authors/0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "Post10",
|
||||
"title": "Post 1:0",
|
||||
"category": "Post category",
|
||||
"text": "Post 0 + by author 1",
|
||||
"author": {
|
||||
"id": "Author1",
|
||||
"name": "Author 1",
|
||||
"thumbnail": "http://example.com/authors/1"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user