Skip to content

Commit

Permalink
Merge pull request #173 from entur/graphql-java-update
Browse files Browse the repository at this point in the history
Graphql java update
  • Loading branch information
assadriaz authored Apr 2, 2024
2 parents abdf72c + ac256ce commit 23a09bb
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 230 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
<spring-cloud-gcp.version>4.8.3</spring-cloud-gcp.version>
<geotools.version>27.2</geotools.version>
<orika-core.version>1.5.4</orika-core.version>
<!-- downgrade since super pom uses 18.5 -->
<graphql-java.version>11.0</graphql-java.version>
<graphql-java.version>20.2</graphql-java.version>
<hazelcast-hibernate53.version>5.1.0</hazelcast-hibernate53.version>
<hazelcast.version>5.2.4</hazelcast.version>
<swagger-jersey2.version>1.6.2</swagger-jersey2.version>
Expand Down Expand Up @@ -175,6 +174,12 @@
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>${graphql-java.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>${graphql-java.version}</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ public class GraphQLNames {

public static final String FIND_STOPPLACE = "stopPlace";

public static final String STOPPLACES_REGISTER = "StopPlaceRegister";
public static final String STOPPLACES_MUTATION = "StopPlaceMutation";
public static final String DELETE_GROUP_OF_STOPPLACES="deleteGroupOfStopPlaces";

/** Check if authorized to edit entity */
public static final String CHECK_AUTHORIZED = "checkAuthorized";
public static final String FIND_STOPPLACE_BY_BBOX = "stopPlaceBBox";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.collect.Sets;
import graphql.ErrorType;
import graphql.ErrorClassification;
import graphql.ExceptionWhileDataFetching;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
Expand Down Expand Up @@ -57,6 +57,9 @@
import java.util.Map;
import java.util.Set;

import static graphql.ErrorType.DataFetchingException;
import static graphql.ErrorType.InvalidSyntax;
import static graphql.ErrorType.ValidationError;
import static java.util.stream.Collectors.toList;

@Component
Expand Down Expand Up @@ -94,7 +97,6 @@ public void init() {
.instrumentation(new MaxQueryDepthInstrumentation(MAX_DEPTH))
.build();


}

private GraphQL graphQL;
Expand Down Expand Up @@ -149,33 +151,34 @@ private Response getGraphQLResponse(String query, Map<String, Object> variables,
Response.ResponseBuilder res = Response.status(Response.Status.OK);
HashMap<String, Object> content = new HashMap<>();
try {
final ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(query).variables(variables).build();
final ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(query)
.root(null)
.variables(variables)
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);

if (!executionResult.getErrors().isEmpty()) {
List<GraphQLError> errors = executionResult.getErrors();

Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
for (GraphQLError error : errors) {
switch (error.getErrorType()) {
case InvalidSyntax:
case ValidationError:
status = Response.Status.BAD_REQUEST;
break;
case DataFetchingException:
ExceptionWhileDataFetching exceptionWhileDataFetching = ((ExceptionWhileDataFetching) error);
if (exceptionWhileDataFetching.getException() != null) {
status = getStatusCodeFromThrowable(exceptionWhileDataFetching.getException());
break;
}
status = Response.Status.OK;
break;
final ErrorClassification errorClassification = error.getErrorType();
if (InvalidSyntax.equals(errorClassification) || ValidationError.equals(errorClassification)) {
status = Response.Status.BAD_REQUEST;
} else if (DataFetchingException.equals(errorClassification)) {
ExceptionWhileDataFetching exceptionWhileDataFetching = ((ExceptionWhileDataFetching) error);
if (exceptionWhileDataFetching.getException() != null) {
status = getStatusCodeFromThrowable(exceptionWhileDataFetching.getException());
continue;
}
status = Response.Status.OK;
}
}

res = Response.status(status);

if (errors.stream().anyMatch(error -> error.getErrorType().equals(ErrorType.DataFetchingException))) {
if (errors.stream().anyMatch(error -> error.getErrorType().equals(DataFetchingException))) {
logger.warn("Detected DataFetchingException from errors: {} Setting transaction to rollback only", errors);
transactionStatus.setRollbackOnly();
}
Expand Down
Loading

0 comments on commit 23a09bb

Please sign in to comment.