Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
996 B
2
Indexable
from sqlalchemy.orm import Session
from typing import List, Optional
from app.models.content import Content
from app.graphql.types.content_type import ContentType
from app.graphql.schema import Info

async def get_content(self, info: Info, content_type: Optional[str] = None) -> List[ContentType]:
    session: Session = info.context.session
    # Ensure this matches the field name in the SQLAlchemy model
    query = session.query(Content)

    #Filter by content type if provided
    if content_type:
        query = query.filter_by(type=content_type)
    
    #Execute the query
    content_list = query.all()

    #Map the results to the ContentType GraphQL type
    return [
        ContentType(
            id=item.id,
            type=item.type,
            text=item.text,
            link=item.link,
            order=item.order,
            created_date=item.created_date,
            updated_date=item.updated_date
        ) for item in content_list
    ]
Leave a Comment