11
11
Sequence ,
12
12
Union ,
13
13
cast ,
14
- overload ,
15
14
)
16
15
from uuid import uuid4
17
16
34
33
FuncParams ,
35
34
Inferred ,
36
35
Mutation ,
37
- MutationOptions ,
38
36
Query ,
39
- QueryOptions ,
40
37
SyncPostprocessor ,
41
38
UserData ,
42
39
)
43
- from reactpy_django .utils import generate_obj_name , get_pk
40
+ from reactpy_django .utils import django_query_postprocessor , generate_obj_name , get_pk
44
41
45
42
if TYPE_CHECKING :
46
43
from channels_redis .core import RedisChannelLayer
@@ -106,19 +103,19 @@ def use_connection() -> ConnectionType:
106
103
107
104
def use_query (
108
105
query : Callable [FuncParams , Awaitable [Inferred ]] | Callable [FuncParams , Inferred ],
109
- / ,
110
- * args : FuncParams . args ,
106
+ * ,
107
+ kwargs : dict [ str , Any ] | None = None ,
111
108
thread_sensitive : bool = True ,
112
- postprocessor : AsyncPostprocessor | SyncPostprocessor | None = None ,
109
+ postprocessor : (
110
+ AsyncPostprocessor | SyncPostprocessor | None
111
+ ) = django_query_postprocessor ,
113
112
postprocessor_kwargs : dict [str , Any ] | None = None ,
114
- ** kwargs : FuncParams .kwargs ,
115
113
) -> Query [Inferred ]:
116
114
"""This hook is used to execute functions in the background and return the result, \
117
115
typically to read data the Django ORM.
118
116
119
117
Args:
120
118
query: A callable that returns a Django `Model` or `QuerySet`.
121
- args: Positional arguments to passed into the `query` function.
122
119
kwargs: Keyword arguments to passed into the `query` function.
123
120
thread_sensitive: Whether to run the query in thread-sensitive mode. \
124
121
This setting only applies to sync query functions.
@@ -131,13 +128,20 @@ def use_query(
131
128
is used to prevent Django's lazy query execution and supports `many_to_many` \
132
129
and `many_to_one` as `postprocessor_kwargs`.
133
130
postprocessor_kwargs: Keyworded arguments passed into the `postprocessor` function.
131
+
132
+ Returns:
133
+ A `Query` object containing the query result, loading state, error state, and a `refetch` \
134
+ function to re-execute the query.
134
135
"""
135
136
136
137
should_execute , set_should_execute = use_state (True )
137
138
data , set_data = use_state (cast (Inferred , None ))
138
139
loading , set_loading = use_state (True )
139
140
error , set_error = use_state (cast (Union [Exception , None ], None ))
140
141
query_ref = use_ref (query )
142
+ kwargs = kwargs or {}
143
+ postprocessor_kwargs = postprocessor_kwargs or {}
144
+
141
145
if query_ref .current is not query :
142
146
raise ValueError (f"Query function changed from { query_ref .current } to { query } ." )
143
147
@@ -146,12 +150,12 @@ async def execute_query() -> None:
146
150
try :
147
151
# Run the query
148
152
if asyncio .iscoroutinefunction (query ):
149
- new_data = await query (* args , * *kwargs )
153
+ new_data = await query (** kwargs )
150
154
else :
151
155
new_data = await database_sync_to_async (
152
156
query ,
153
157
thread_sensitive = thread_sensitive ,
154
- )(* args , * *kwargs )
158
+ )(** kwargs )
155
159
156
160
# Run the postprocessor
157
161
if postprocessor :
@@ -202,15 +206,15 @@ def register_refetch_callback() -> Callable[[], None]:
202
206
_REFETCH_CALLBACKS [query ].add (refetch )
203
207
return lambda : _REFETCH_CALLBACKS [query ].remove (refetch )
204
208
205
- # The query's user API
209
+ # Return Query user API
206
210
return Query (data , loading , error , refetch )
207
211
208
212
209
213
def use_mutation (
210
214
mutation : (
211
215
Callable [FuncParams , bool | None ] | Callable [FuncParams , Awaitable [bool | None ]]
212
216
),
213
- / ,
217
+ * ,
214
218
thread_sensitive : bool = True ,
215
219
refetch : Callable [..., Any ] | Sequence [Callable [..., Any ]] | None = None ,
216
220
) -> Mutation [FuncParams ]:
@@ -230,6 +234,10 @@ def use_mutation(
230
234
hook) or a sequence of query functions that need a `refetch` if the \
231
235
mutation succeeds. This is useful for refreshing data after a mutation \
232
236
has been performed.
237
+
238
+ Returns:
239
+ A `Mutation` object that can be called to execute the mutation. This object \
240
+ also contains loading state, error state, and a `reset` function.
233
241
"""
234
242
235
243
loading , set_loading = use_state (False )
@@ -287,7 +295,7 @@ def reset() -> None:
287
295
set_loading (False )
288
296
set_error (None )
289
297
290
- # The mutation's user API
298
+ # Return mutation user API
291
299
return Mutation (schedule_mutation , loading , error , reset )
292
300
293
301
@@ -331,11 +339,13 @@ async def _set_user_data(data: dict):
331
339
await model .asave ()
332
340
333
341
query : Query [dict | None ] = use_query (
334
- QueryOptions (postprocessor = None ),
335
342
_get_user_data ,
336
- user = user ,
337
- default_data = default_data ,
338
- save_default_data = save_default_data ,
343
+ kwargs = {
344
+ "user" : user ,
345
+ "default_data" : default_data ,
346
+ "save_default_data" : save_default_data ,
347
+ },
348
+ postprocessor = None ,
339
349
)
340
350
mutation = use_mutation (_set_user_data , refetch = _get_user_data )
341
351
0 commit comments