[JAVA-14660] Update code for Graphql error handling (#12839)

This commit is contained in:
Haroon Khan
2022-10-10 20:16:12 +01:00
committed by GitHub
parent d6918ce11d
commit 4897b0afa0
51 changed files with 317 additions and 473 deletions
@@ -0,0 +1,90 @@
package com.baeldung.graphql.error.handling;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.graphql.test.tester.HttpGraphQlTester;
import org.springframework.test.context.ActiveProfiles;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static graphql.ErrorType.NullValueInNonNullableField;
import static org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR;
import static org.springframework.graphql.execution.ErrorType.NOT_FOUND;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = GraphQLErrorHandlerApplication.class)
@ActiveProfiles("error-handling")
public class GraphQLErrorHandlerIntegrationTest {
private static final String GRAPHQL_TEST_REQUEST_PATH = "src/test/resources/graphql-files/request/%s_request.graphql";
private static final String GRAPHQL_TEST_RESPONSE_PATH = "src/test/resources/graphql-files/response/%s_response.json";
@Autowired
private HttpGraphQlTester graphQlTester;
@Test
void whenMandatoryFieldNull_thenRespondWithResponseError() throws IOException {
String nonNullFieldScenario = "non_null_field";
graphQlTester.document(fileToRequest(nonNullFieldScenario))
.execute()
.errors()
.expect(error -> error.getErrorType() == NullValueInNonNullableField)
.verify()
.path("$.data")
.matchesJson(fileToResponse(nonNullFieldScenario));
}
@Test
void whenUnhandledException_thenRespondWithGenericError() throws IOException {
String unhandledExceptionScenario = "unhandled_exception";
graphQlTester.document(fileToRequest(unhandledExceptionScenario))
.execute()
.errors()
.expect(error -> error.getErrorType() == INTERNAL_ERROR)
.verify()
.path("$.data")
.valueIsNull();
}
@Test
void whenHandledException_thenRespondWithCustomErrorDetails() throws IOException {
String handledExceptionScenario = "handled_exception";
graphQlTester.document(fileToRequest(handledExceptionScenario))
.execute()
.errors()
.expect(error -> error.getErrorType() == NOT_FOUND
&& "Vehicle with vin: 123 not found.".equals(error.getMessage()))
.verify()
.path("$.data")
.matchesJson("{\n"
+ " \"searchByVin\": null\n"
+ " }");
}
@Test
void whenNoException_thenRespondWithNoError() throws IOException {
String noExceptionScenario = "no_exception";
graphQlTester.document(fileToRequest(noExceptionScenario))
.execute()
.path("$.data")
.matchesJson(fileToResponse(noExceptionScenario));
}
private static String fileToRequest(String fileName) throws IOException {
Path path = Paths.get(String.format(GRAPHQL_TEST_REQUEST_PATH, fileName));
return new String(Files.readAllBytes(path));
}
private static String fileToResponse(String fileName) throws IOException {
Path path = Paths.get(String.format(GRAPHQL_TEST_RESPONSE_PATH, fileName));
return new String(Files.readAllBytes(path));
}
}
@@ -1,4 +1,4 @@
package com.baeldung.graphql;
package com.baeldung.graphql.intro;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
@@ -1,5 +1,6 @@
package com.baeldung.graphql;
package com.baeldung.graphql.intro;
import com.baeldung.graphql.intro.GraphqlApplication;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@@ -0,0 +1,10 @@
query {
searchByVin(vin: "123"){
vin
year
make
model
trim
}
}
@@ -0,0 +1,8 @@
query {
searchByVin(vin: "KM8JN72DX7U587496"){
vin
year
make
model
}
}
@@ -0,0 +1,10 @@
query {
searchAll {
vin
year
make
model
trim
}
}
@@ -0,0 +1,10 @@
query {
searchByLocation(zipcode: "123"){
vin
year
make
model
trim
}
}
@@ -0,0 +1,8 @@
{
"searchByVin": {
"vin": "KM8JN72DX7U587496",
"year": 2007,
"make": "Hyundai",
"model": "Tucson"
}
}
@@ -0,0 +1,19 @@
{
"searchAll": [
null,
{
"vin": "JTKKU4B41C1023346",
"year": 2012,
"make": "Toyota",
"model": "Scion",
"trim": "Xd"
},
{
"vin": "1G1JC1444PZ215071",
"year": 2000,
"make": "Chevrolet",
"model": "CAVALIER VL",
"trim": "RS"
}
]
}