from typing import Optional
import strawberry
from sqlmodel import (
    SQLModel,
    Field,
    create_engine,
    select,
    Session
)


engine = create_engine('sqlite:///database.db')


class Person(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    nome: str
    idade: int


SQLModel.metadata.create_all(engine)

def create_app(nome: str, idade:int):
    person = Person(nome=nome, idade=idade)
    with Session(engine) as session:
        session.add(person)
        session.commit()
        session.refresh(person)

    return person

@strawberry.type
class Pessoa:
    id: Optional[int]
    nome: str
    idade: int


@strawberry.type
class Query:

    @strawberry.field
    def all_pessoa(self) -> list[Pessoa]:
        query = select(Person)
        with Session(engine) as session:
            result = session.execute(query).scalars().all()
        return result


@strawberry.type
class Mutation:
    create_pessoa: Pessoa = strawberry.field(resolver=create_app)

schema = strawberry.Schema(query=Query, mutation=Mutation)
strawberry.field sqlmodel.create_engine sqlmodel.Session sqlmodel.SQLModel.metadata.create_all sqlmodel.Field sqlmodel.select strawberry.Schema
[((150, 188), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite:///database.db"""'], {}), "('sqlite:///database.db')\n", (163, 188), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((320, 356), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (348, 356), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((1026, 1075), 'strawberry.Schema', 'strawberry.Schema', ([], {'query': 'Query', 'mutation': 'Mutation'}), '(query=Query, mutation=Mutation)\n', (1043, 1075), False, 'import strawberry\n'), ((251, 288), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (256, 288), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((978, 1015), 'strawberry.field', 'strawberry.field', ([], {'resolver': 'create_app'}), '(resolver=create_app)\n', (994, 1015), False, 'import strawberry\n'), ((449, 464), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (456, 464), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((777, 791), 'sqlmodel.select', 'select', (['Person'], {}), '(Person)\n', (783, 791), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n'), ((805, 820), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (812, 820), False, 'from sqlmodel import SQLModel, Field, create_engine, select, Session\n')]