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
}
}