spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

UnexpectedRollbackException for NonNullableFieldWasNullException when using @Transactional

Open viico opened this issue 11 months ago • 0 comments

We want to use the @Transactional annotation in order to have one transaction by Query or Mutation. We added this annotation to a custom AsyncExecutionStrategy.

It works fine except for some exceptions, for example : NonNullableFieldWasNullException. This exception is not logged, it is only visible in the GraphQL response. Without the transactional annotation we have the correct response containing the error. But with the transactional annotation we didn't, an UnexpectedRollbackException is thrown. In this case the error is not logged and we have no idea of what is wrong.

We push a repository to reproduce the problem : https://github.com/viico/unexpectedrollbackexception. You can clone it, the class ProjectQueryResolverTest contains a test which should pass. It passes if we comment the transactional annotation (line 19 of TransactionalAsyncExecutionStrategyWithExceptionHandler class).

In the repo the exception NonNullableFieldWasNullException is thrown because we have a null field (label) which must not be null in the GrapQL schema.

For people who have the same problem : we have a workaround, you can log the error before UnexpectedRollbackException is thrown. The problem is still here but you have a log with the correct error, you can add the log in the custom AsyncExecutionStrategy :

    @Override
    @Transactional
    public CompletableFuture<ExecutionResult> execute(
            final ExecutionContext executionContext,
            final ExecutionStrategyParameters parameters
    ) throws NonNullableFieldWasNullException {
        return super.execute(executionContext, parameters).whenComplete((completeValueInfos, throwable) -> {
            if (throwable != null) {
                log.error("Error during GraphQL request execution", throwable);
            }
        });
    }

viico avatar Mar 19 '24 08:03 viico