-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from BeatsuDev/feat/pydantic-to-query-builder
feat: add a from_pydantic method that creates a QueryBuilder from a pydantic BaseModel
- Loading branch information
Showing
7 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
from . import query_creator | ||
from .builder import QueryBuilder | ||
from .pydantic_converter import from_pydantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Type | ||
|
||
from pydantic import BaseModel | ||
|
||
from .builder import QueryBuilder | ||
|
||
|
||
def from_pydantic(model: Type[BaseModel]) -> Type[QueryBuilder]: | ||
return type(model.__name__, (QueryBuilder,), {"__annotations__": model.__annotations__}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import gqlrequests | ||
|
||
from typing import List | ||
from pydantic import BaseModel | ||
|
||
class EveryTypeModel(BaseModel): | ||
id: int | ||
age: int | ||
money: float | ||
name: str | ||
company: bool | ||
|
||
def test_create_everytype(): | ||
correct_string = """ | ||
{ | ||
id | ||
age | ||
money | ||
name | ||
company | ||
} | ||
"""[1:] | ||
EveryType = gqlrequests.from_pydantic(EveryTypeModel) | ||
assert EveryType().build() == correct_string | ||
|
||
|
||
class NestedTypeModel(BaseModel): | ||
nested: EveryTypeModel | ||
|
||
def test_create_nestedtype(): | ||
correct_string = """ | ||
{ | ||
nested { | ||
id | ||
age | ||
money | ||
name | ||
company | ||
} | ||
} | ||
"""[1:] | ||
NestedType = gqlrequests.from_pydantic(NestedTypeModel) | ||
assert NestedType().build() == correct_string | ||
|
||
|
||
class ListTypeModel(BaseModel): | ||
nested: List[int] | ||
|
||
def test_create_listtype(): | ||
correct_string = """ | ||
{ | ||
nested | ||
} | ||
"""[1:] | ||
ListType = gqlrequests.from_pydantic(ListTypeModel) | ||
assert ListType().build() == correct_string |