Skip to content

Commit

Permalink
Merge pull request #53 from gita/graphql-fixes
Browse files Browse the repository at this point in the history
added composite indices
  • Loading branch information
samanyougarg authored Jul 18, 2021
2 parents aacb42c + f6fd541 commit 5ed3043
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions bhagavad_gita_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def delete_all_data():
"""
deletes all data
"""
response = typer.prompt("are you sure you want to delete all the data? [y/n]")
response = typer.prompt("Are you sure you want to delete all the data? [y/n]")
if response == "y":
typer.echo("deleting ...")
typer.echo("Deleting...")
Base.metadata.drop_all(bind=engine)
typer.echo("deleted.")
typer.echo("Deleted.")


@app.command()
Expand Down
3 changes: 0 additions & 3 deletions bhagavad_gita_api/graphql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

from graphene import Int, List, ObjectType, String
from graphene_sqlalchemy import SQLAlchemyObjectType

Expand Down Expand Up @@ -162,7 +160,6 @@ class Meta:
exclude_fields = ("verses",)

def resolve_verses(parent, info, **kwargs):
time.time()

if "limit" in kwargs.keys():
query = (
Expand Down
6 changes: 5 additions & 1 deletion bhagavad_gita_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import Depends, FastAPI, HTTPException, Security, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security.api_key import APIKeyHeader
from graphql.execution.executors.asyncio import AsyncioExecutor
from sqlalchemy.orm import Session
from starlette.graphql import GraphQLApp

Expand Down Expand Up @@ -45,7 +46,10 @@ async def get_api_key(
allow_headers=["*"],
)

app.add_route("/graphql", GraphQLApp(schema=graphene.Schema(query=Query)))
app.add_route(
"/graphql",
GraphQLApp(executor_class=AsyncioExecutor, schema=graphene.Schema(query=Query)),
)
app.include_router(api_router, prefix=settings.API_V2_STR)


Expand Down
27 changes: 20 additions & 7 deletions bhagavad_gita_api/models/gita.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import Index
from sqlalchemy.types import UnicodeText

from bhagavad_gita_api.db.base_class import Base
Expand All @@ -16,6 +17,8 @@ class GitaCommentary(Base):
author_id = Column(Integer, ForeignKey("gita_authors.id"))
language_id = Column(Integer, ForeignKey("gita_languages.id"))

__table_args__ = (Index("ix_commentary", "author_name", "language", "verse_id"),)


class GitaTranslation(Base):
__tablename__ = "gita_translations"
Expand All @@ -28,6 +31,8 @@ class GitaTranslation(Base):
author_id = Column(Integer, ForeignKey("gita_authors.id"))
language_id = Column(Integer, ForeignKey("gita_languages.id"))

__table_args__ = (Index("ix_translation", "author_name", "language", "verse_id"),)


class GitaLanguage(Base):
__tablename__ = "gita_languages"
Expand All @@ -37,6 +42,8 @@ class GitaLanguage(Base):
translations = relationship(GitaTranslation, lazy="joined")
commentaries = relationship(GitaCommentary, lazy="joined")

__table_args__ = (Index("ix_language", "language"),)


class GitaAuthor(Base):
__tablename__ = "gita_authors"
Expand All @@ -46,30 +53,36 @@ class GitaAuthor(Base):
translations = relationship(GitaTranslation, backref="gitaAuthor")
commentaries = relationship(GitaCommentary, backref="gitaAuthor")

__table_args__ = (Index("ix_author", "name"),)


class GitaVerse(Base):
__tablename__ = "gita_verses"

id = Column(Integer, primary_key=True, index=True, autoincrement=True)
slug = Column(UnicodeText, index=True)
verse_number = Column(Integer, index=True)
chapter_number = Column(Integer, index=True)
text = Column(UnicodeText, index=True)
id = Column(Integer, primary_key=True, autoincrement=True)
slug = Column(UnicodeText)
verse_number = Column(Integer)
chapter_number = Column(Integer)
text = Column(UnicodeText)
chapter_id = Column(Integer, ForeignKey("gita_chapters.id"))
translations = relationship(GitaTranslation, backref="gita_verses", lazy="joined")
commentaries = relationship(GitaCommentary, backref="gita_verses", lazy="joined")

__table_args__ = (Index("ix_verse", "chapter_number", "verse_number", "slug"),)


class GitaChapter(Base):
__tablename__ = "gita_chapters"

id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(UnicodeText, index=True)
slug = Column(UnicodeText, index=True)
name = Column(UnicodeText)
slug = Column(UnicodeText)
name_transliterated = Column(UnicodeText)
name_translated = Column(UnicodeText)
verses_count = Column(Integer)
chapter_number = Column(Integer)
name_meaning = Column(UnicodeText)
chapter_summary = Column(UnicodeText)
verses = relationship(GitaVerse, backref="gita_chapters", lazy="joined")

__table_args__ = (Index("ix_chapter", "chapter_number", "slug"),)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gita-api = 'bhagavad_gita_api.main:cli'

[tool.isort]
profile = "black"
known_third_party = ["dotenv", "fastapi", "graphene", "graphene_sqlalchemy", "pydantic", "pytz", "rich", "sqlalchemy", "starlette", "typer", "uvicorn"]
known_third_party = ["dotenv", "fastapi", "graphene", "graphene_sqlalchemy", "graphql", "pydantic", "pytz", "rich", "sqlalchemy", "starlette", "typer", "uvicorn"]

[tool.black]
line-length = 88
Expand Down

0 comments on commit 5ed3043

Please sign in to comment.