Skip to content

Commit ba42a8b

Browse files
committed
Move mutation and query to types.py
1 parent 80b1471 commit ba42a8b

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/django_idom/hooks.py

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,26 @@
11
from __future__ import annotations
22

33
import asyncio
4-
from dataclasses import dataclass
5-
from typing import (
6-
Any,
7-
Awaitable,
8-
Callable,
9-
DefaultDict,
10-
Generic,
11-
Sequence,
12-
TypeVar,
13-
Union,
14-
cast,
15-
)
4+
from typing import Any, Awaitable, Callable, DefaultDict, Sequence, Union, cast
165

176
from channels.db import database_sync_to_async as _database_sync_to_async
187
from django.db.models.base import Model
198
from django.db.models.query import QuerySet
209
from idom import use_callback, use_ref
2110
from idom.backend.types import Location
2211
from idom.core.hooks import Context, create_context, use_context, use_effect, use_state
23-
from typing_extensions import ParamSpec
2412

25-
from django_idom.types import IdomWebsocket
13+
from django_idom.types import IdomWebsocket, Mutation, Query, _Params, _Result
2614

2715

2816
database_sync_to_async = cast(
2917
Callable[..., Callable[..., Awaitable[Any]]],
3018
_database_sync_to_async,
3119
)
32-
20+
WebsocketContext: Context[IdomWebsocket | None] = create_context(None)
3321
_REFETCH_CALLBACKS: DefaultDict[
3422
Callable[..., Any], set[Callable[[], None]]
3523
] = DefaultDict(set)
36-
WebsocketContext: Context[IdomWebsocket | None] = create_context(None)
37-
_Result = TypeVar("_Result", bound=Union[Model, QuerySet[Any]])
38-
_Params = ParamSpec("_Params")
39-
_Data = TypeVar("_Data")
4024

4125

4226
def use_location() -> Location:
@@ -147,22 +131,6 @@ def reset() -> None:
147131
return Mutation(call, loading, error, reset)
148132

149133

150-
@dataclass
151-
class Query(Generic[_Data]):
152-
data: _Data
153-
loading: bool
154-
error: Exception | None
155-
refetch: Callable[[], None]
156-
157-
158-
@dataclass
159-
class Mutation(Generic[_Params]):
160-
execute: Callable[_Params, None]
161-
loading: bool
162-
error: Exception | None
163-
reset: Callable[[], None]
164-
165-
166134
def _fetch_deferred(data: Any) -> None:
167135
# https://github.com/typeddjango/django-stubs/issues/704
168136
if isinstance(data, QuerySet): # type: ignore[misc]

src/django_idom/types.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
from dataclasses import dataclass
2-
from typing import Awaitable, Callable, Optional
2+
from typing import Any, Awaitable, Callable, Generic, Optional, TypeVar, Union
3+
4+
from django.db.models.base import Model
5+
from django.db.models.query import QuerySet
6+
from typing_extensions import ParamSpec
7+
8+
9+
__all__ = ["_Result", "_Params", "_Data", "IdomWebsocket", "Query", "Mutation"]
10+
11+
_Result = TypeVar("_Result", bound=Union[Model, QuerySet[Any]])
12+
_Params = ParamSpec("_Params")
13+
_Data = TypeVar("_Data")
314

415

516
@dataclass
617
class IdomWebsocket:
18+
"""Websocket returned by the `use_websocket` hook."""
19+
720
scope: dict
821
close: Callable[[Optional[int]], Awaitable[None]]
922
disconnect: Callable[[int], Awaitable[None]]
1023
view_id: str
24+
25+
26+
@dataclass
27+
class Query(Generic[_Data]):
28+
"""Queries generated by the `use_query` hook."""
29+
30+
data: _Data
31+
loading: bool
32+
error: Exception | None
33+
refetch: Callable[[], None]
34+
35+
36+
@dataclass
37+
class Mutation(Generic[_Params]):
38+
"""Mutations generated by the `use_mutation` hook."""
39+
40+
execute: Callable[_Params, None]
41+
loading: bool
42+
error: Exception | None
43+
reset: Callable[[], None]

0 commit comments

Comments
 (0)