<role>You are a code review specialist.</role>
 <context>Perform a comprehensive review of the provided [

import logging
from typing import Any
from functools import partial
from contextlib import asynccontextmanager

import strawberry
from strawberry.types import Info
from strawberry.fastapi import BaseContext, GraphQLRouter
from fastapi import FastAPI
from databases import Database

from settings import Settings

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

class Context(BaseContext):
    db: Database

    def __init__(
        self,
        db: Database,
    ) -> None:
        self.db = db


@strawberry.type
class Author:
    name: str


@strawberry.type
class Book:
    title: str
    author: Author


@strawberry.type
class Query:

    @strawberry.field
    async def books(
        self,
        info: Info[Context, None],
        author_ids: list[int] | None = None,
        search: str | None = None,
        limit: int | None = None,
    ) -> list[Book]:
        try:
            query_parts = [
                "SELECT books.title AS book_title, authors.name AS author_name",
                "FROM books",
                "JOIN authors ON books.author_id = authors.id"
            ]
            conditions = []
            params: dict[str, Any] = {}

            if author_ids:
                conditions.append("books.author_id = ANY(:author_ids)")
                params["author_ids"] = author_ids

            if search:
                conditions.append("books.title ILIKE :search")
                params["search"] = f"%{search}%"

            if conditions:
                query_parts.append("WHERE " + " AND ".join(conditions))

            query_parts.append("ORDER BY books.title")

            if limit:
                query_parts.append("LIMIT :limit")
                params["limit"] = limit

            final_query = " ".join(query_parts)
            logger.debug("Executing query: %s", final_query)

            rows = await info.context.db.fetch_all(query=final_query, values=params)

            return [
                Book(title=row["book_title"], author=Author(name=row["author_name"]))
                for row in rows
            ]
        except Exception as e:
            logger.exception("Failed to fetch books: %s", e)
            return []



CONN_TEMPLATE = "postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
settings = Settings()  # type: ignore
db = Database(
    CONN_TEMPLATE.format(
        user=settings.DB_USER,
        password=settings.DB_PASSWORD,
        port=settings.DB_PORT,
        host=settings.DB_SERVER,
        name=settings.DB_NAME,
    ),
)

@asynccontextmanager
async def lifespan(
    app: FastAPI,
    db: Database,
):
    async with db:
        yield
    await db.disconnect()

schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(  # type: ignore
    schema,
    context_getter=partial(Context, db),
)

app = FastAPI(lifespan=partial(lifespan, db=db))
app.include_router(graphql_app, prefix="/graphql")
].</context>
 <steps> 1. Evaluate the code for efficiency, readability, and maintainability.
 2. Identify bugs, security issues, or performance bottlenecks.
 3. Offer actionable suggestions for improvement.</steps>