#πŸ”’ FastAPI, GraphQL and Strawberry integration issue

10 messages Β· Page 1 of 1 (latest)

coral idol
#

Hello everyone! I'm new to the group and generally to GraphQL. I'm having an issue with my code and I'm lost on what to do.
This is the mutations file I'm currently using:

import strawberry
from app.schemas import BookType, AuthorType
from app.db import get_db
from app.models import Book, Author

@strawberry.type
class Mutation:
    @strawberry.mutation
    async def add_book(self, title: str, author_id: int) -> BookType:
        async with get_db() as db:  # Using the database session dependency
            new_book = Book(title=title, author_id=author_id)  # Create a new book instance
            db.add(new_book)  # Add the new book to the session
            await db.commit()  # Commit the transaction
            await db.refresh(new_book)  # Refresh the instance to reflect the database state
            return new_book  # Return the newly created book

    @strawberry.mutation
    async def add_author(self, name: str) -> AuthorType:
        async with get_db() as db:  # Using the database session dependency
            new_author = Author(name=name)  # Create a new author instance
            db.add(new_author)  # Add the new author to the session
            await db.commit()  # Commit the transaction
            await db.refresh(new_author)  # Refresh the instance to reflect the database state
            return new_author  # Return the newly created author

If I try:

mutation {
  addAuthor(name: "George Orwell") {
    id
    name
  }
}
teal stratusBOT
#

@coral idol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

coral idol
#

I get the following error:
async with get_db() as db: # Using the database session dependency
TypeError: 'async_generator' object does not support the asynchronous context manager protocol

#
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from config import settings
from typing import AsyncGenerator

engine = create_async_engine(settings.DATABASE_URL, echo=True)
SessionLocal = sessionmaker(bind=engine, 
                            class_=AsyncSession, 
                            expire_on_commit=False)

# Function to provide the database session, used as a dependency in FastAPI routes
async def get_db() -> AsyncGenerator[AsyncSession, None]:
    async with SessionLocal() as session:
        yield session
rapid spear
#

you might need to decorate your get_db function with @asynccontextmanager from contextlib

coral idol
coral idol
teal stratusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.