diff --git a/graphql-modules/graphql-error-handling/README.md b/graphql-modules/graphql-error-handling/README.md deleted file mode 100644 index 06a2957ac1..0000000000 --- a/graphql-modules/graphql-error-handling/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling) diff --git a/graphql-modules/graphql-error-handling/pom.xml b/graphql-modules/graphql-error-handling/pom.xml deleted file mode 100644 index 581c5a0f3d..0000000000 --- a/graphql-modules/graphql-error-handling/pom.xml +++ /dev/null @@ -1,80 +0,0 @@ - - - 4.0.0 - graphql-error-handling - 1.0 - graphql-error-handling - jar - - - com.baeldung.graphql - graphql-modules - 1.0.0-SNAPSHOT - - - - - - - org.springframework.boot - spring-boot-dependencies - 2.6.4 - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - com.graphql-java - graphql-spring-boot-starter - ${graphql-spring-boot-starter.version} - - - com.graphql-java - graphql-java-tools - ${graphql-java-tools.version} - - - org.projectlombok - lombok - - - com.h2database - h2 - - - org.springframework.boot - spring-boot-test - test - - - com.graphql-java - graphql-spring-boot-starter-test - ${graphql-spring-boot-starter.version} - test - - - org.skyscreamer - jsonassert - test - - - - - 5.0.2 - 5.2.4 - - - \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java deleted file mode 100644 index 565c9e0a15..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.graphql.error.handling; - -import com.baeldung.graphql.error.handling.exception.GraphQLErrorAdapter; -import graphql.ExceptionWhileDataFetching; -import graphql.GraphQLError; -import graphql.servlet.GraphQLErrorHandler; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -@SpringBootApplication -public class GraphQLErrorHandlerApplication { - public static void main(String[] args) { - SpringApplication.run(GraphQLErrorHandlerApplication.class, args); - } - - @Bean - public GraphQLErrorHandler errorHandler() { - return new GraphQLErrorHandler() { - @Override - public List processErrors(List errors) { - List clientErrors = errors.stream() - .filter(this::isClientError) - .collect(Collectors.toList()); - - List serverErrors = errors.stream() - .filter(e -> !isClientError(e)) - .map(GraphQLErrorAdapter::new) - .collect(Collectors.toList()); - - List e = new ArrayList<>(); - e.addAll(clientErrors); - e.addAll(serverErrors); - return e; - } - - private boolean isClientError(GraphQLError error) { - return !(error instanceof ExceptionWhileDataFetching || error instanceof Throwable); - } - }; - } -} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java deleted file mode 100644 index 4e7be50ae4..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.graphql.error.handling.exception; - -import graphql.ErrorType; -import graphql.GraphQLError; -import graphql.language.SourceLocation; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class AbstractGraphQLException extends RuntimeException implements GraphQLError { - private Map parameters = new HashMap(); - - public AbstractGraphQLException(String message) { - super(message); - } - - public AbstractGraphQLException(String message, Map additionParams) { - this(message); - if (additionParams != null) { - parameters = additionParams; - } - } - - @Override - public String getMessage() { - return super.getMessage(); - } - - @Override - public List getLocations() { - return null; - } - - @Override - public ErrorType getErrorType() { - return null; - } - - @Override - public Map getExtensions() { - return this.parameters; - } -} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/GraphQLErrorAdapter.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/GraphQLErrorAdapter.java deleted file mode 100644 index d982f98db3..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/GraphQLErrorAdapter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.graphql.error.handling.exception; - -import graphql.ErrorType; -import graphql.ExceptionWhileDataFetching; -import graphql.GraphQLError; -import graphql.language.SourceLocation; - -import java.util.List; -import java.util.Map; - -public class GraphQLErrorAdapter implements GraphQLError { - - private GraphQLError error; - - public GraphQLErrorAdapter(GraphQLError error) { - this.error = error; - } - - @Override - public Map getExtensions() { - return error.getExtensions(); - } - - @Override - public List getLocations() { - return error.getLocations(); - } - - @Override - public ErrorType getErrorType() { - return error.getErrorType(); - } - - @Override - public List getPath() { - return error.getPath(); - } - - @Override - public Map toSpecification() { - return error.toSpecification(); - } - - @Override - public String getMessage() { - return (error instanceof ExceptionWhileDataFetching) ? ((ExceptionWhileDataFetching) error).getException().getMessage() : error.getMessage(); - } -} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java deleted file mode 100644 index 0d2ad8c597..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.graphql.error.handling.exception; - -import java.util.Map; - -public class VehicleNotFoundException extends AbstractGraphQLException { - - public VehicleNotFoundException(String message) { - super(message); - } - - public VehicleNotFoundException(String message, Map params) { - super(message, params); - } -} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Mutation.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Mutation.java deleted file mode 100644 index 8463ebf8eb..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Mutation.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.graphql.error.handling.resolver; - -import com.baeldung.graphql.error.handling.domain.Location; -import com.baeldung.graphql.error.handling.domain.Vehicle; -import com.baeldung.graphql.error.handling.service.InventoryService; -import com.coxautodev.graphql.tools.GraphQLMutationResolver; -import org.springframework.stereotype.Component; - -@Component -public class Mutation implements GraphQLMutationResolver { - private InventoryService inventoryService; - - public Mutation(InventoryService inventoryService) { - this.inventoryService = inventoryService; - } - - public Vehicle addVehicle(String vin, Integer year, String make, String model, String trim, Location location) { - return this.inventoryService.addVehicle(vin, year, make, model, trim, location); - } -} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Query.java b/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Query.java deleted file mode 100644 index ece018464a..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/resolver/Query.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.graphql.error.handling.resolver; - -import com.baeldung.graphql.error.handling.domain.Vehicle; -import com.baeldung.graphql.error.handling.service.InventoryService; -import com.coxautodev.graphql.tools.GraphQLQueryResolver; -import org.springframework.stereotype.Component; - -import java.util.List; - -@Component -public class Query implements GraphQLQueryResolver { - private final InventoryService inventoryService; - - public Query(InventoryService inventoryService) { - this.inventoryService = inventoryService; - } - - public List searchAll() { - return this.inventoryService.searchAll(); - } - - public List searchByLocation(String zipcode) { - return this.inventoryService.searchByLocation(zipcode); - } - - public Vehicle searchByVin(String vin) { - return this.inventoryService.searchByVin(vin); - } -} diff --git a/graphql-modules/graphql-error-handling/src/main/resources/import.sql b/graphql-modules/graphql-error-handling/src/main/resources/import.sql deleted file mode 100644 index 62907a86c3..0000000000 --- a/graphql-modules/graphql-error-handling/src/main/resources/import.sql +++ /dev/null @@ -1,7 +0,0 @@ -insert into LOCATION values('07092', 'Mountainside', 'NJ'); -insert into LOCATION values ('94118', 'San Francisco', 'CA'); -insert into LOCATION values ('10002', 'New York', 'NY'); - -insert into VEHICLE (vin, year, make, model, trim, fk_location) values('KM8JN72DX7U587496', 2007, 'Hyundai', 'Tucson', null, '07092'); -insert into VEHICLE (vin, year, make, model, trim, fk_location) values('JTKKU4B41C1023346', 2012, 'Toyota', 'Scion', 'Xd', '94118'); -insert into VEHICLE (vin, year, make, model, trim, fk_location) values('1G1JC1444PZ215071', 2000, 'Chevrolet', 'CAVALIER VL', 'RS', '07092'); \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplicationIntegrationTest.java b/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplicationIntegrationTest.java deleted file mode 100644 index 069a08ce02..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplicationIntegrationTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.graphql.error.handling; - -import com.graphql.spring.boot.test.GraphQLResponse; -import com.graphql.spring.boot.test.GraphQLTestTemplate; -import org.json.JSONException; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.skyscreamer.jsonassert.JSONAssert; -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.test.context.junit4.SpringRunner; - -import java.io.IOException; - -import static com.baeldung.graphql.error.handling.TestUtils.readFile; -import static java.lang.String.format; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = GraphQLErrorHandlerApplication.class) -public class GraphQLErrorHandlerApplicationIntegrationTest { - - @Autowired - private GraphQLTestTemplate graphQLTestTemplate; - - private static final String GRAPHQL_TEST_REQUEST_PATH = "graphql/request/%s.graphql"; - private static final String GRAPHQL_TEST_RESPONSE_PATH = "graphql/response/%s.json"; - - @Test - public void whenUnknownOperation_thenRespondWithRequestError() throws IOException, JSONException { - String graphqlName = "request_error_unknown_operation"; - GraphQLResponse actualResponse = graphQLTestTemplate.postForResource(format(GRAPHQL_TEST_REQUEST_PATH, graphqlName)); - String expectedResponse = readFile(format(GRAPHQL_TEST_RESPONSE_PATH, graphqlName)); - - JSONAssert.assertEquals(expectedResponse, actualResponse.getRawResponse().getBody(), true); - } - - @Test - public void whenInvalidSyntaxRequest_thenRespondWithRequestError() throws IOException, JSONException { - String graphqlName = "request_error_invalid_request_syntax"; - GraphQLResponse actualResponse = graphQLTestTemplate.postForResource(format(GRAPHQL_TEST_REQUEST_PATH, graphqlName)); - String expectedResponse = readFile(format(GRAPHQL_TEST_RESPONSE_PATH, graphqlName)); - - JSONAssert.assertEquals(expectedResponse, actualResponse.getRawResponse().getBody(), true); - } - - @Test - public void whenRequestAllNonNullField_thenRespondPartialDataWithFieldError() throws IOException, JSONException { - String graphqlName = "field_error_request_non_null_fields_partial_response"; - GraphQLResponse actualResponse = graphQLTestTemplate.postForResource(format(GRAPHQL_TEST_REQUEST_PATH, graphqlName)); - String expectedResponse = readFile(format(GRAPHQL_TEST_RESPONSE_PATH, graphqlName)); - - JSONAssert.assertEquals(expectedResponse, actualResponse.getRawResponse().getBody(), true); - } -} diff --git a/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/TestUtils.java b/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/TestUtils.java deleted file mode 100644 index 557f1d9c91..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/java/com/baeldung/graphql/error/handling/TestUtils.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.graphql.error.handling; - -import org.apache.commons.io.IOUtils; -import org.springframework.core.io.ClassPathResource; - -import java.io.IOException; -import java.nio.charset.Charset; - -public class TestUtils { - public static String readFile(String path) throws IOException { - return IOUtils.toString( - new ClassPathResource(path).getInputStream(), Charset.defaultCharset() - ); - } -} diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/field_error_request_non_null_fields_partial_response.graphql b/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/field_error_request_non_null_fields_partial_response.graphql deleted file mode 100644 index 17affc50cb..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/field_error_request_non_null_fields_partial_response.graphql +++ /dev/null @@ -1,10 +0,0 @@ -# trim is non null but one of the record has null value for trim -query { - searchAll { - vin - year - make - model - trim - } -} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/field_error_request_non_null_fields_partial_response.json b/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/field_error_request_non_null_fields_partial_response.json deleted file mode 100644 index 760190128e..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/field_error_request_non_null_fields_partial_response.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "data": { - "searchAll": [ - null, - { - "vin": "JTKKU4B41C1023346", - "year": 2012, - "make": "Toyota", - "model": "Scion", - "trim": "Xd" - }, - { - "vin": "1G1JC1444PZ215071", - "year": 2000, - "make": "Chevrolet", - "model": "CAVALIER VL", - "trim": "RS" - } - ] - }, - "errors": [ - { - "message": "Cannot return null for non-nullable type: 'String' within parent 'Vehicle' (/searchAll[0]/trim)", - "path": [ - "searchAll", - 0, - "trim" - ], - "errorType": "DataFetchingException", - "locations": null, - "extensions": null - } - ] -} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_invalid_request_syntax.json b/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_invalid_request_syntax.json deleted file mode 100644 index 2835b42133..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_invalid_request_syntax.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "data": null, - "errors": [ - { - "message": "Invalid Syntax", - "locations": [ - { - "line": 5, - "column": 8, - "sourceName": null - } - ], - "errorType": "InvalidSyntax", - "path": null, - "extensions": null - } - ] -} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_unknown_operation.json b/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_unknown_operation.json deleted file mode 100644 index b5872fc80f..0000000000 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/response/request_error_unknown_operation.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "data": null, - "errors": [ - { - "errorType": "OperationNotSupported", - "locations": [ - { - "line": 1, - "column": 1, - "sourceName": null - } - ], - "extensions": null, - "message": "Schema is not configured for subscriptions.", - "path": null - } - ] -} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/resources/init_script.sql b/graphql-modules/graphql-error-handling/src/test/resources/init_script.sql deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/graphql-modules/pom.xml b/graphql-modules/pom.xml index 95af87843d..4f0a0f7fb5 100644 --- a/graphql-modules/pom.xml +++ b/graphql-modules/pom.xml @@ -18,9 +18,8 @@ graphql-dgs - graphql-error-handling graphql-java graphql-spqr - \ No newline at end of file + diff --git a/spring-boot-modules/spring-boot-graphql/README.md b/spring-boot-modules/spring-boot-graphql/README.md index 8223360597..93387741b8 100644 --- a/spring-boot-modules/spring-boot-graphql/README.md +++ b/spring-boot-modules/spring-boot-graphql/README.md @@ -9,6 +9,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) - [Expose GraphQL Field with Different Name](https://www.baeldung.com/graphql-field-name) +- [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling) ### GraphQL sample queries diff --git a/spring-boot-modules/spring-boot-graphql/pom.xml b/spring-boot-modules/spring-boot-graphql/pom.xml index b89bc42d2f..4130881f0e 100644 --- a/spring-boot-modules/spring-boot-graphql/pom.xml +++ b/spring-boot-modules/spring-boot-graphql/pom.xml @@ -18,6 +18,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-data-jpa + org.springframework.boot spring-boot-starter-graphql @@ -26,11 +30,21 @@ org.projectlombok lombok + + com.h2database + h2 + + org.springframework.boot spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-webflux + test + org.springframework.graphql spring-graphql-test diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java new file mode 100644 index 0000000000..1fc2b623cd --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.graphql.error.handling; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GraphQLErrorHandlerApplication { + public static void main(String[] args) { + System.setProperty("spring.profiles.default", "error-handling"); + SpringApplication.run(GraphQLErrorHandlerApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java new file mode 100644 index 0000000000..21aa1c7d47 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java @@ -0,0 +1,43 @@ +package com.baeldung.graphql.error.handling.controller; + +import com.baeldung.graphql.error.handling.domain.Location; +import com.baeldung.graphql.error.handling.domain.Vehicle; +import com.baeldung.graphql.error.handling.service.InventoryService; +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; +import org.springframework.graphql.data.method.annotation.QueryMapping; +import org.springframework.stereotype.Controller; + +import java.util.List; + +@Controller +public class VehicleController { + + private final InventoryService inventoryService; + + public VehicleController(InventoryService inventoryService) { + this.inventoryService = inventoryService; + } + + @QueryMapping + public List searchAll() { + return this.inventoryService.searchAll(); + } + + @QueryMapping + public List searchByLocation(@Argument String zipcode) { + return this.inventoryService.searchByLocation(zipcode); + } + + @QueryMapping + public Vehicle searchByVin(@Argument String vin) { + return this.inventoryService.searchByVin(vin); + } + + @MutationMapping + public Vehicle addVehicle(@Argument String vin, @Argument Integer year, + @Argument String make, @Argument String model, @Argument String trim, + @Argument Location location) { + return this.inventoryService.addVehicle(vin, year, make, model, trim, location); + } +} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java new file mode 100644 index 0000000000..8c69b3df24 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/AbstractGraphQLException.java @@ -0,0 +1,26 @@ +package com.baeldung.graphql.error.handling.exception; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class AbstractGraphQLException extends RuntimeException { + + private Map parameters = new HashMap<>(); + + public AbstractGraphQLException(String message) { + super(message); + } + + public AbstractGraphQLException(String message, Map additionParams) { + this(message); + if (additionParams != null) { + parameters = additionParams; + } + } + + public Map getExtensions() { + return parameters.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/CustomExceptionResolver.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/CustomExceptionResolver.java new file mode 100644 index 0000000000..35ee5e75c0 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/CustomExceptionResolver.java @@ -0,0 +1,34 @@ +package com.baeldung.graphql.error.handling.exception; + +import graphql.GraphQLError; +import graphql.GraphqlErrorBuilder; +import graphql.schema.DataFetchingEnvironment; +import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter; +import org.springframework.graphql.execution.ErrorType; +import org.springframework.stereotype.Component; + +@Component +public class CustomExceptionResolver extends DataFetcherExceptionResolverAdapter { + + @Override + protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) { + if (ex instanceof VehicleNotFoundException) { + return GraphqlErrorBuilder.newError() + .errorType(ErrorType.NOT_FOUND) + .message(ex.getMessage()) + .path(env.getExecutionStepInfo().getPath()) + .location(env.getField().getSourceLocation()) + .build(); + } else if (ex instanceof AbstractGraphQLException) { + return GraphqlErrorBuilder.newError() + .errorType(ErrorType.INTERNAL_ERROR) + .message(ex.getMessage()) + .path(env.getExecutionStepInfo().getPath()) + .location(env.getField().getSourceLocation()) + .extensions(((AbstractGraphQLException) ex).getExtensions()) + .build(); + } else { + return null; + } + } +} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/InvalidInputException.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/InvalidInputException.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/InvalidInputException.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/InvalidInputException.java diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java new file mode 100644 index 0000000000..95e0b03048 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleNotFoundException.java @@ -0,0 +1,9 @@ +package com.baeldung.graphql.error.handling.exception; + +public class VehicleNotFoundException extends RuntimeException { + + public VehicleNotFoundException(String message) { + super(message); + } + +} diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/repository/InventoryRepository.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/repository/InventoryRepository.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/repository/InventoryRepository.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/repository/InventoryRepository.java diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/repository/LocationRepository.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/repository/LocationRepository.java similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/repository/LocationRepository.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/repository/LocationRepository.java diff --git a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java similarity index 83% rename from graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java index 7064b08760..9b8d3716d6 100644 --- a/graphql-modules/graphql-error-handling/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java @@ -15,8 +15,8 @@ import java.util.*; @Service public class InventoryService { - private InventoryRepository inventoryRepository; - private LocationRepository locationRepository; + private final InventoryRepository inventoryRepository; + private final LocationRepository locationRepository; public InventoryService(InventoryRepository inventoryRepository, LocationRepository locationRepository) { this.inventoryRepository = inventoryRepository; @@ -29,7 +29,7 @@ public class InventoryService { if (existingVehicle.isPresent()) { Map params = new HashMap<>(); params.put("vin", vin); - throw new VehicleAlreadyPresentException("Failed to add vehicle. Vehicle with vin " + vin + " already present.", params); + throw new VehicleAlreadyPresentException("Failed to add vehicle. Vehicle with vin already present.", params); } Vehicle vehicle = Vehicle.builder() .vin(vin) @@ -58,10 +58,7 @@ public class InventoryService { } public Vehicle searchByVin(String vin) { - return this.inventoryRepository.findById(vin).orElseThrow(() -> { - Map params = new HashMap<>(); - params.put("vin", vin); - return new VehicleNotFoundException("Vehicle with vin " + vin + " not found.", params); - }); + return this.inventoryRepository.findById(vin) + .orElseThrow(() -> new VehicleNotFoundException("Vehicle with vin: " + vin + " not found.")); } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Author.java similarity index 78% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Author.java index e4597504af..acad5ca858 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Author.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import lombok.Data; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorController.java similarity index 92% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorController.java index bbc1466f8a..656b2ca927 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorController.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import org.springframework.graphql.data.method.annotation.SchemaMapping; import org.springframework.stereotype.Controller; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorDao.java similarity index 91% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorDao.java index 37946e57dd..5b602a43de 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/AuthorDao.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import java.util.List; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java similarity index 66% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java index 34bdeebe7b..3910b4331b 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java @@ -1,12 +1,16 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; @SpringBootApplication -@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) +@EnableAutoConfiguration(exclude = { + SecurityAutoConfiguration.class, + HibernateJpaAutoConfiguration.class +}) public class GraphqlApplication { public static void main(String[] args) { diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlConfiguration.java similarity index 97% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlConfiguration.java index 30cb71c43c..01cb3713eb 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlConfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Post.java similarity index 83% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Post.java index 65f189162a..64045c240b 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/Post.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import lombok.Data; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostController.java similarity index 97% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostController.java index 2df17bf5f6..770d274a47 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostController.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.MutationMapping; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostDao.java similarity index 94% rename from spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostDao.java index a0724efaad..3a27508230 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/PostDao.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import java.util.List; import java.util.stream.Collectors; diff --git a/graphql-modules/graphql-error-handling/src/main/resources/application.yml b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml similarity index 65% rename from graphql-modules/graphql-error-handling/src/main/resources/application.yml rename to spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml index 155e133a62..298eeb16c1 100644 --- a/graphql-modules/graphql-error-handling/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml @@ -1,23 +1,24 @@ -graphql: - servlet: - mapping: /graphql +server: + port: 8081 spring: + graphql: + schema: + locations: classpath:error-handling/graphql/ datasource: url: "jdbc:h2:mem:graphqldb" driverClassName: "org.h2.Driver" username: sa password: - initialization-mode: always platform: h2 - jpa: show-sql: true properties: hibernate: dialect: org.hibernate.dialect.H2Dialect ddl-auto: none + globally_quoted_identifiers: true h2: - console.enabled: true \ No newline at end of file + console.enabled: true diff --git a/graphql-modules/graphql-error-handling/src/main/resources/graphql/inventory.graphqls b/spring-boot-modules/spring-boot-graphql/src/main/resources/error-handling/graphql/inventory.graphqls similarity index 100% rename from graphql-modules/graphql-error-handling/src/main/resources/graphql/inventory.graphqls rename to spring-boot-modules/spring-boot-graphql/src/main/resources/error-handling/graphql/inventory.graphqls diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql b/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql new file mode 100644 index 0000000000..647d17a76d --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql @@ -0,0 +1,7 @@ +insert into "location" values('07092', 'Mountainside', 'NJ'); +insert into "location" values ('94118', 'San Francisco', 'CA'); +insert into "location" values ('10002', 'New York', 'NY'); + +insert into "vehicle" ("vin", "year", "make", "model", "trim", "fk_location") values('KM8JN72DX7U587496', 2007, 'Hyundai', 'Tucson', null, '07092'); +insert into "vehicle" ("vin", "year", "make", "model", "trim", "fk_location") values('JTKKU4B41C1023346', 2012, 'Toyota', 'Scion', 'Xd', '94118'); +insert into "vehicle" ("vin", "year", "make", "model", "trim", "fk_location") values('1G1JC1444PZ215071', 2000, 'Chevrolet', 'CAVALIER VL', 'RS', '07092'); \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java new file mode 100644 index 0000000000..b9b88e921b --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java @@ -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)); + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/PostControllerIntegrationTest.java similarity index 98% rename from spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java rename to spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/PostControllerIntegrationTest.java index 1cb008b7be..bccf5099bd 100644 --- a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/PostControllerIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.graphql; +package com.baeldung.graphql.intro; import lombok.SneakyThrows; import org.junit.jupiter.api.Test; diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java similarity index 75% rename from spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java rename to spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java index 2b8ce5f35d..87bebf644c 100644 --- a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java @@ -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; diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_invalid_request_syntax.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/handled_exception_request.graphql similarity index 71% rename from graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_invalid_request_syntax.graphql rename to spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/handled_exception_request.graphql index 98920eb17a..1564a6dafb 100644 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_invalid_request_syntax.graphql +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/handled_exception_request.graphql @@ -1,9 +1,10 @@ query { - searchByVin(vin: "error) { + searchByVin(vin: "123"){ vin year make model trim } -} \ No newline at end of file +} + diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/no_exception_request.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/no_exception_request.graphql new file mode 100644 index 0000000000..756dca44eb --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/no_exception_request.graphql @@ -0,0 +1,8 @@ +query { + searchByVin(vin: "KM8JN72DX7U587496"){ + vin + year + make + model + } +} \ No newline at end of file diff --git a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_unknown_operation.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/non_null_field_request.graphql similarity index 59% rename from graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_unknown_operation.graphql rename to spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/non_null_field_request.graphql index fb6c3d1039..ca2fe1cc8d 100644 --- a/graphql-modules/graphql-error-handling/src/test/resources/graphql/request/request_error_unknown_operation.graphql +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/non_null_field_request.graphql @@ -1,9 +1,10 @@ -subscription { - searchByVin(vin: "75024") { +query { + searchAll { vin year make model trim } -} \ No newline at end of file +} + diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/unhandled_exception_request.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/unhandled_exception_request.graphql new file mode 100644 index 0000000000..8571f4b31c --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/request/unhandled_exception_request.graphql @@ -0,0 +1,10 @@ +query { + searchByLocation(zipcode: "123"){ + vin + year + make + model + trim + } +} + diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/no_exception_response.json b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/no_exception_response.json new file mode 100644 index 0000000000..7bbff5f1be --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/no_exception_response.json @@ -0,0 +1,8 @@ +{ + "searchByVin": { + "vin": "KM8JN72DX7U587496", + "year": 2007, + "make": "Hyundai", + "model": "Tucson" + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/non_null_field_response.json b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/non_null_field_response.json new file mode 100644 index 0000000000..0369c64463 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-files/response/non_null_field_response.json @@ -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" + } + ] +}