Should I make my Mutations and Queries @Transactional #1010
-
Hi, I have a question. I'm using Hibernate + Spring Data and I want to be sure that if one of my resolvers already queried something and then another resolver queries the exact same thing, then it will be resolved instantly without query to DB so will be using Hibernate Session L1 Cache. Unsure how GraphQL Kotlin is interoperable with Spring Data. As with regular REST, it's is pretty simple, you have an entry point which is just some controller and this is where you open the session, then the rest just goes deeper calling functions and services until it's all done and returned back to the client. With GraphQL I'm unsure whether the session will be per resolver then, or per whole query so will be opened and reused for all resolvers and their services that they're calling. If I'll be able to utilize the same Hibernate session through the whole query then it will optimize my GraphQL API a lot and dataloader pattern will be needed solely for Batch fetching by IDs only and the rest will be just effective by default. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hello 👋 With traditional servlet based REST APIs you work with endpoints that provide you complete responses and also have access to request level scope that helps to manage your transactions. Once you move to the reactive Webflux based APIs (which we use in GraphQL is slightly different that you requests are always processed through the single In order to have transactions spanning your whole request I think you probably would need to explicitly start the transaction before executing GraphQL engine. We recently abstracted away the generic server logic so you could easily implement your own |
Beta Was this translation helpful? Give feedback.
Hello 👋
With traditional servlet based REST APIs you work with endpoints that provide you complete responses and also have access to request level scope that helps to manage your transactions. Once you move to the reactive Webflux based APIs (which we use in
graphql-kotlin-spring-server
), AFAIK those annotations won't work and you will have to programmatically manage your transactions.GraphQL is slightly different that you requests are always processed through the single
/graphql
endpoint that parses the requests and resolves each field independently of each other. Since single GraphQL request can span multiple queries or mutations, creating transactions per query/mutation may not be wha…