Database session usage in fastapi strawberry graphql

43 views Asked by At

How to use get_db function in the following Query, i just tried to use as shown below but i get error: 'generator' object has no attribute 'query'

@strawberry.type
class Query:
    @strawberry.field
    async def get_blog(self) -> List[ShowBlogType]:
        db =  get_db()
        blogs = BlogRepository.get_all_blog(db)
        return blogs

while the get_db is:

from databases import Database
from fastapi import FastAPI
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session

DATABASE_URL = "postgresql://root:######@localhost/db_a"

app = FastAPI()

database = Database(DATABASE_URL)

engine = create_engine(DATABASE_URL)

SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)

#Database connection
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
        
Base = declarative_base()
0

There are 0 answers