Skip to content

Commit

Permalink
Merge pull request #3 from MDoreto/refact-async
Browse files Browse the repository at this point in the history
:build: update: pydantic
  • Loading branch information
MDoreto committed Jan 8, 2024
2 parents 19f4bcf + 0ad313f commit c232048
Show file tree
Hide file tree
Showing 14 changed files with 691 additions and 535 deletions.
4 changes: 2 additions & 2 deletions docs/Tutorial/Guide/Using models.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Setup the fastapi server with a default query, passing the created engine as par

## Creating Models

Now we need to create the models that will represent our DB tables, for now our classes will be declared exactly the same of SQLmodel, but importing MyModel.I recommend to create one file for each table.
Now we need to create the models that will represent our DB tables, for now our classes will be declared exactly the same of SQLmodel, but importing Graphemy.I recommend to create one file for each table.
Create a `models` folder in project's folder and a `account.py` file inside it.

```Python hl_lines="7"
Expand All @@ -63,7 +63,7 @@ set GRAPHEMY_PATH="models"

### Create Tables

You have many ways to seed data into database, in this tutorial we will create tables using a default SQLmodel function that can be called from MyModel, but as we are creating table.
You have many ways to seed data into database, in this tutorial we will create tables using a default SQLmodel function that can be called from Graphemy, but as we are creating table.

```Python hl_lines="27"
# main.py
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ async def hello_world(self, info) -> str:

from fastapi import FastAPI

from graphemy import MyGraphQLRouter
from graphemy import GraphemyRouter

app = FastAPI()

graphql_app = MyGraphQLRouter(query=Query)
graphql_app = GraphemyRouter(query=Query)

app.include_router(graphql_app, prefix='/graphql')
6 changes: 3 additions & 3 deletions examples/tutorial/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from examples.tutorial.models.bank import Bank
from examples.tutorial.models.services import Services
from examples.tutorial.models.transaction import Transaction
from graphemy import MyGraphQLRouter, MyModel
from graphemy import Graphemy, GraphemyRouter

engine = create_engine(
'sqlite://',
Expand All @@ -27,15 +27,15 @@ class Mutation:

app = FastAPI()

graphql_app = MyGraphQLRouter(
graphql_app = GraphemyRouter(
query=Query,
mutation=Mutation,
engine=engine,
folder='examples\\tutorial\\models',
)
app.include_router(graphql_app, prefix='/graphql')

MyModel.metadata.create_all(engine)
Graphemy.metadata.create_all(engine)


with Session(engine) as session:
Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from sqlmodel import Field

from graphemy import MyModel, dl, get_list, get_one
from graphemy import Graphemy, dl, get_list, get_one


class Account(MyModel, table=True):
class Account(Graphemy, table=True):
bank_id: int = Field(primary_key=True)
id: str = Field(primary_key=True)
modality: str
Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/models/bank.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import strawberry
from sqlmodel import Field

from graphemy import MyModel, dl, get_one
from graphemy import Graphemy, dl, get_one


class Bank(MyModel, table=True):
class Bank(Graphemy, table=True):
_default_mutation = True
_delete_mutation = True
__customfields__ = {
Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/models/services.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sqlmodel import Field

from graphemy import MyModel, dl, get_list
from graphemy import Graphemy, dl, get_list


class Services(MyModel, table=True):
class Services(Graphemy, table=True):
id: str = Field(primary_key=True)
description: str

Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from sqlmodel import Field

from graphemy import MyModel, dl, get_list
from graphemy import Graphemy, dl, get_list


class Transaction(MyModel, table=True):
class Transaction(Graphemy, table=True):
bank_id: int | None
account_id: str | None
id: int = Field(primary_key=True)
Expand Down
4 changes: 2 additions & 2 deletions graphemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlmodel import Field

from graphemy.dl import dl
from graphemy.models import MyModel, get_all, get_list, get_one
from graphemy.router import MyGraphQLRouter
from graphemy.models import Graphemy, get_all, get_list, get_one
from graphemy.router import GraphemyRouter
from graphemy.setup import Setup
14 changes: 9 additions & 5 deletions graphemy/dl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import glob
import inspect
import os

from strawberry.dataloader import DataLoader

from .models import MyDate


class MyDataLoader(DataLoader):
def __init__(self, filter_method=None, request=None, **kwargs):
self.filter_method = filter_method
self.request = request
super().__init__(**kwargs)

async def load(self, keys, filters: dict | None = False):
if filters == False:
return await super().load(keys)
Expand All @@ -18,7 +19,10 @@ async def load(self, keys, filters: dict | None = False):
if isinstance(keys, str)
else keys
)
return await super().load(dict_to_tuple(filters))
data = await super().load(dict_to_tuple(filters))
if self.filter_method:
data = self.filter_method(data, self.request)
return data


def dict_to_tuple(data: dict) -> tuple:
Expand Down
Loading

0 comments on commit c232048

Please sign in to comment.