FastAPI Synchronous/Blocking Resolvers #3088
Replies: 1 comment 3 replies
-
@donalddalton so I'm not familiar with how FastAPI handles the execution on different thread pools, but sync resolvers should just work, but I'm assuming that having the route handler as async means that FastAPI can't do the thread pooling, right? It should be pretty straight-forward to implement a sync version of the Router, but I don't know if it is worth adding it to Strawberry, it would definitely be nicer to use the thread pooling logic for resolvers, but I feel like it might be quite a bit of work 😊 @donalddalton would it be an issue for you to have a custom version of the router that's sync? Also, are you planning on using dataloaders? |
Beta Was this translation helpful? Give feedback.
-
Hello! My team recently started using Strawberry to migrate a moderately sized FastAPI application from REST to GraphQL. To do this, we are making use of Strawberry's FastAPI GraphQLRouter. It seems as though this router assumes resolvers are implemented as asynchronous/non-blocking functions, but this is unfortunately not the case for the database access layer (pyscopg2/sqlalchemy) our resolvers call into. This is problematic because it means our queries are being executed on the "main" event loop thread and completely block the server for the duration of each request. Ideally, we'd like to avoid refactoring our app to use asyncio and instead have GraphQL requests execute on an external thread pool (FastAPI does this transparently for synchronous endpoint functions). I see that Strawberry supports both sync/async request handling for other web frameworks but I have not found anything for FastAPI (e.g. a synchronous version of GraphQLRouter). Any guidance/commentary on this topic would be greatly appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions