How to test GraphQL endpoint using separate database for testing in FastAPI Strawberry?

I have set up 2 databases in my project. One is primary which I would use for day to day use and the other is just for testing. The problem is that my Strawberry fields all use primary database and I don’t know how can I properly replace that primary db with testing db.

I know that in FastAPI there’s something like dependency injection with the help of which I can replace primary database connection dependency with testing database connection dependency but I don’t seem to find similar solution in Strawberry.

I am using sqlalchemy with 2 postgres databases(primary and for testing), alembic for migrations and pytest with requests to perform tests.

What I’m trying to achieve:

  1. I want to apply migrations to my database for testing with alembic.
  2. Replace primary database session in all the Strawberry fields with test database session.
  3. Perform tests on GraphQL endpoints with requests module.

Leave a Comment