Pablo Schmid
08/11/2021, 5:50 PMextend type Mutation {
userCreate(): UserCreatePayload
}
union UserCreatePayload = UserCreated | UserAlreadyExists
I have a MutationsResolver that handles the userCreate
mutation by calling a UserService
that is transactional.
When trying to create a user, if the user already exists, I'm throwing a UserAlreadyExist
Exception that is handled in the mutation resolver with a try/catch block and then mapped to the UserAlreadyExists type in graphql.
The problem is that as the UserService is transactional, whenever I throw the exception, the transaction is marked a rollbackOnly by spring (which is good) and then the graphql flow goes through the DefaultGraphQLErrorHandler that finally return an error to the end user.
I would like to have the exactly same behavior except for the graphql error handler returning a GraphqlError. Instead I want to return the UserAlreadyExists
type I defined in the graphql schema.
Is there anyway to do so? (I do not want to use sealed classes instead of exceptions in the service as the code gets dirty)
Thanks!Dariusz Kuc
08/11/2021, 7:13 PMPablo Schmid
08/11/2021, 9:18 PMDariusz Kuc
08/11/2021, 9:20 PMgraphql-java
is a GraphQL implementation but doesn't define a server (as that is a higher level concept) -> some libs built on top of graphql-java
providing server functionality
• graphql-kotlin
(I'm one of the maintainers of it)
• graphql-java-kickstart
• netflix-dgs
• spring-graphql
Pablo Schmid
08/11/2021, 9:21 PMgraphql-java-kickstart
is the one I'm using.Dariusz Kuc
08/11/2021, 9:24 PMUserAlreadyExist
from your try/catch block in the reesolver?Pablo Schmid
08/11/2021, 9:28 PMDariusz Kuc
08/11/2021, 9:42 PMgraphql-java-kickstart
Pablo Schmid
08/11/2021, 10:07 PMonException
function is a DataFetcherExceptionHandlerResult and not a DataFetcherResultnoRollbackFor = [MyException.class]
to the Transactional annotation. By I'm not sure this is the safer approach.@transactional
which is conceptually wrong as it is a port adapter. By removing that @transactional
everything worked as expected. There is no need to use the noRollbackFor
.Many Regards.