Untitled
unknown
plain_text
6 months ago
4.7 kB
2
Indexable
from sqlalchemy import create_engine, text from sqlalchemy.orm import sessionmaker, declarative_base from fastapi import FastAPI, Depends, 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 class Product(Base): __tablename__ = "mdm_product" record_id = Column("RECORD_ID", Integer, primary_key=True) # Other fields here... # GraphQL types @strawberry.type class StatusType: status_id: int status_desc: str @strawberry.type class TherapeuticAreaType: ta_id: int ta_name: str @strawberry.type class CountryType: country_id: int country_name: str @strawberry.type class FieldDetailType: field_id: int field_name: str @strawberry.type class ProductFieldType: record_id: int field_id: int max_field_value: Optional[str] # Corrected definition field_detail: FieldDetailType @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 status: StatusType country: CountryType therapeutic_area: TherapeuticAreaType product_fields: List[ProductFieldType] # Added product_fields relationship # 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 generated view after running the stored procedure products = ( session.query(Product) .filter( Product.is_deleted != 2, # Check if product is not marked as deleted ) .all() ) # Construct product response response = [] for product in products: # Map product fields and details into GraphQL response product_fields = [ ProductFieldType( record_id=pf.record_id, field_id=pf.field_id, max_field_value=pf.field_value, field_detail=FieldDetailType( field_id=pf.field_detail.field_id, field_name=pf.field_detail.field_name, ), ) for pf in product.product_fields ] response.append(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, status=StatusType( status_id=product.status.STATUS_ID, status_desc=product.status.STATUS_DESC, ), country=CountryType( country_id=product.country.COUNTRY_ID, country_name=product.country.COUNTRY_NAME, ), therapeutic_area=TherapeuticAreaType( ta_id=product.therapeutic_area.TA_ID, ta_name=product.therapeutic_area.TA_NAME, ), product_fields=product_fields, # Add product_fields to the response )) 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