-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more types, use cases and examples
- Loading branch information
Showing
22 changed files
with
372 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from ariadne.asgi import GraphQL | ||
from ariadne.asgi.handlers import GraphQLTransportWSHandler | ||
from starlette.middleware.cors import CORSMiddleware | ||
|
||
from .schema import schema | ||
|
||
|
||
app = GraphQL(schema, debug=True) | ||
app = CORSMiddleware( | ||
GraphQL( | ||
schema, | ||
debug=True, | ||
websocket_handler=GraphQLTransportWSHandler(), | ||
), | ||
allow_origins="*", | ||
) |
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,33 +2,12 @@ | |
|
||
from .models.category import Category | ||
from .models.group import Group | ||
from .models.post import Post | ||
from .models.user import User | ||
|
||
|
||
def get_data() -> dict[str, dict[int, Any]]: | ||
return { | ||
"categories": { | ||
1: Category( | ||
id=1, | ||
name="First category", | ||
parent_id=None, | ||
), | ||
2: Category( | ||
id=2, | ||
name="Second category", | ||
parent_id=None, | ||
), | ||
3: Category( | ||
id=3, | ||
name="Child category", | ||
parent_id=1, | ||
), | ||
4: Category( | ||
id=4, | ||
name="Other child category", | ||
parent_id=1, | ||
), | ||
}, | ||
"groups": { | ||
1: Group( | ||
id=1, | ||
|
@@ -45,26 +24,70 @@ def get_data() -> dict[str, dict[int, Any]]: | |
1: User( | ||
id=1, | ||
username="JohnDoe", | ||
email="[email protected]", | ||
group_id=1, | ||
), | ||
2: User( | ||
id=2, | ||
username="Alice", | ||
email="[email protected]", | ||
group_id=1, | ||
), | ||
3: User( | ||
id=3, | ||
username="Bob", | ||
email="[email protected]", | ||
group_id=2, | ||
), | ||
4: User( | ||
id=4, | ||
username="Mia", | ||
email="[email protected]", | ||
group_id=2, | ||
), | ||
}, | ||
"categories": { | ||
1: Category( | ||
id=1, | ||
name="First category", | ||
parent_id=None, | ||
), | ||
2: Category( | ||
id=2, | ||
name="Second category", | ||
parent_id=None, | ||
), | ||
3: Category( | ||
id=3, | ||
name="Child category", | ||
parent_id=1, | ||
), | ||
4: Category( | ||
id=4, | ||
name="Other child category", | ||
parent_id=1, | ||
), | ||
}, | ||
"posts": { | ||
1: Post( | ||
id=1, | ||
message="Lorem ipsum", | ||
category_id=1, | ||
poster_id=1, | ||
), | ||
2: Post( | ||
id=2, | ||
message="Dolor met", | ||
category_id=2, | ||
poster_id=2, | ||
), | ||
3: Post( | ||
id=3, | ||
message="Sit amet", | ||
category_id=3, | ||
poster_id=3, | ||
), | ||
4: Post( | ||
id=4, | ||
message="Elit", | ||
category_id=4, | ||
poster_id=4, | ||
), | ||
}, | ||
} |
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 dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Post: | ||
id: int | ||
message: str | ||
category_id: int | None | ||
poster_id: int | None |
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 |
---|---|---|
|
@@ -5,5 +5,4 @@ | |
class User: | ||
id: int | ||
username: str | ||
email: str | ||
group_id: int |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from typing import Any | ||
|
||
from . import categories, groups, hello, users | ||
from . import calendar, categories, groups, hello, posts, users | ||
|
||
queries: Any = [ | ||
calendar.Query, | ||
categories.Query, | ||
groups.Query, | ||
hello.Query, | ||
posts.Query, | ||
users.Query, | ||
] |
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,20 @@ | ||
from datetime import datetime | ||
|
||
from ariadne_graphql_modules import GraphQLObject | ||
from graphql import GraphQLResolveInfo | ||
|
||
from ..database import db | ||
from ..scalars.date import DateScalar | ||
from ..scalars.datetime import DateTimeScalar | ||
|
||
|
||
class Query(GraphQLObject): | ||
@GraphQLObject.field() | ||
@staticmethod | ||
async def date(obj, info: GraphQLResolveInfo) -> DateScalar: | ||
return DateScalar(datetime.now().date()) | ||
|
||
@GraphQLObject.field() | ||
@staticmethod | ||
async def datetime(obj, info: GraphQLResolveInfo) -> DateTimeScalar: | ||
return DateTimeScalar(datetime.now()) |
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,25 @@ | ||
from typing import Optional | ||
|
||
from ariadne_graphql_modules import GraphQLID, GraphQLObject | ||
from graphql import GraphQLResolveInfo | ||
|
||
from ..database import db | ||
from ..models.post import Post | ||
from ..types.post import PostType | ||
|
||
|
||
class Query(GraphQLObject): | ||
@GraphQLObject.field(graphql_type=list[PostType]) | ||
@staticmethod | ||
async def posts(obj, info: GraphQLResolveInfo) -> list[Post]: | ||
return await db.get_all("posts") | ||
|
||
@GraphQLObject.field(graphql_type=Optional[PostType]) | ||
@staticmethod | ||
async def post(obj, info: GraphQLResolveInfo, id: GraphQLID) -> Post | None: | ||
try: | ||
id_int = int(id) | ||
except (TypeError, ValueError): | ||
return None | ||
|
||
return await db.get_row("posts", id=id_int) |
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
Empty file.
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,17 @@ | ||
from datetime import date, datetime | ||
from typing import Union, cast | ||
|
||
from ariadne_graphql_modules import GraphQLScalar | ||
|
||
|
||
class DateScalar(GraphQLScalar): | ||
@classmethod | ||
def serialize(cls, value: Union["DateScalar", date]) -> str: | ||
if isinstance(value, cls): | ||
value = cast(date, value.unwrap()) | ||
|
||
return value.strftime("%Y-%m-%d") | ||
|
||
@classmethod | ||
def parse_value(cls, value: str) -> date: | ||
return datetime.strptime("%Y-%m-%d").date() |
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,17 @@ | ||
from datetime import datetime | ||
from typing import Union, cast | ||
|
||
from ariadne_graphql_modules import GraphQLScalar | ||
|
||
|
||
class DateTimeScalar(GraphQLScalar): | ||
@classmethod | ||
def serialize(cls, value: Union["DateTimeScalar", datetime]) -> str: | ||
if isinstance(value, cls): | ||
value = cast(datetime, value.unwrap()) | ||
|
||
return value.isoformat() | ||
|
||
@classmethod | ||
def parse_value(cls, value: str) -> datetime: | ||
return datetime.fromisoformat(value) |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from ariadne_graphql_modules import make_executable_schema | ||
|
||
from .queries import queries | ||
from .subscriptions import subscriptions | ||
|
||
|
||
schema = make_executable_schema(queries, convert_names_case=True) | ||
schema = make_executable_schema( | ||
queries, | ||
subscriptions, | ||
convert_names_case=True, | ||
) |
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,5 @@ | ||
from typing import Any | ||
|
||
from . import events | ||
|
||
subscriptions: Any = [events.Subscription] |
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,26 @@ | ||
import random | ||
from asyncio import sleep | ||
from typing import AsyncGenerator | ||
from datetime import datetime | ||
|
||
from ariadne_graphql_modules import GraphQLSubscription | ||
from graphql import GraphQLResolveInfo | ||
|
||
from ..types.event import EventType | ||
|
||
|
||
class Subscription(GraphQLSubscription): | ||
event: EventType | ||
|
||
@GraphQLSubscription.source("event") | ||
async def source_event(obj, info: GraphQLResolveInfo) -> AsyncGenerator[int, None]: | ||
i = 0 | ||
|
||
while True: | ||
i += 1 | ||
yield i | ||
await sleep(float(random.randint(1, 50)) / 10) | ||
|
||
@GraphQLSubscription.resolver("event") | ||
async def resolve_event(obj: int, info: GraphQLResolveInfo) -> dict: | ||
return {"id": obj, "payload": datetime.now()} |
Oops, something went wrong.