Untitled

 avatar
unknown
plain_text
6 months ago
3.7 kB
2
Indexable
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker, declarative_base
from fastapi import FastAPI, HTTPException
import strawberry
from strawberry.fastapi import GraphQLRouter
from typing import List, Optional
from strawberry.schema.config import StrawberryConfig

# Database setup
DATABASE_URL = ""
Base = declarative_base()
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# SQLAlchemy models (for views created by the stored procedure)
class ProductView(Base):
    __tablename__ = "mdm_product_view"
    record_id = Column("RECORD_ID", Integer, primary_key=True)
    janssen_mstr_prdct_nm = Column(String)
    product_phase = Column(String)
    jnj_full_compound_id = Column(String)
    generic_name = Column(String)
    jnj_flag = Column(String)
    product_status = Column(String)
    current_record = Column(Integer)
    last_active = Column(Integer)
    created_date = Column(String)
    updated_date = Column(String)
    ta_name = Column("TA_NAME", String)
    country_name = Column("COUNTRY_NAME", String)
    status_desc = Column("REQUEST_STATUS", String)
    delete_state = Column("DELETE_STATE", String)

# GraphQL types
@strawberry.type
class StatusType:
    status_desc: str

@strawberry.type
class TherapeuticAreaType:
    ta_name: str

@strawberry.type
class CountryType:
    country_name: str

@strawberry.type
class ProductType:
    record_id: int
    janssen_mstr_prdct_nm: Optional[str]
    product_phase: str
    jnj_full_compound_id: str
    generic_name: str
    jnj_flag: Optional[str]
    product_status: str
    current_record: int
    last_active: int
    created_date: str
    updated_date: str
    delete_state: str
    status: StatusType
    country: CountryType
    therapeutic_area: TherapeuticAreaType

# GraphQL query
@strawberry.type
class Query:
    @strawberry.field
    def products(self, user: str) -> List[ProductType]:
        session = SessionLocal()
        try:
            # Execute the stored procedure
            session.execute(text("CALL jsamdm_qa.productViewSP()"))
            session.commit()

            # Fetch products from the view created by the stored procedure
            products = session.query(ProductView).all()

            # Construct product response
            response = [
                ProductType(
                    record_id=product.record_id,
                    janssen_mstr_prdct_nm=product.janssen_mstr_prdct_nm,
                    product_phase=product.product_phase,
                    jnj_full_compound_id=product.jnj_full_compound_id,
                    generic_name=product.generic_name,
                    jnj_flag=product.jnj_flag,
                    product_status=product.product_status,
                    current_record=product.current_record,
                    last_active=product.last_active,
                    created_date=product.created_date,
                    updated_date=product.updated_date,
                    delete_state=product.delete_state,
                    status=StatusType(status_desc=product.status_desc),
                    country=CountryType(country_name=product.country_name),
                    therapeutic_area=TherapeuticAreaType(ta_name=product.ta_name)
                )
                for product in products
            ]

            return response

        finally:
            session.close()

# GraphQL setup
schema = strawberry.Schema(query=Query, config=StrawberryConfig(auto_camel_case=False))
graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
Editor is loading...
Leave a Comment