Skip to content

Commit 2e343aa

Browse files
authored
New use_query and use_mutation API (#241)
- Fix #207
1 parent 2949aa6 commit 2e343aa

17 files changed

+161
-227
lines changed

CHANGELOG.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,21 @@ Using the following categories, list your changes in this order:
3434

3535
## [Unreleased]
3636

37-
- Nothing (yet)!
37+
### Changed
38+
39+
- New syntax for `use_query` and `use_mutation` hooks. Here's a quick comparison of the changes:
40+
41+
```python
42+
query = use_query(QueryOptions(thread_sensitive=True), get_items, value=123456, foo="bar") # Old
43+
query = use_query(get_items, {"value":12356, "foo":"bar"}, thread_sensitive=True) # New
44+
45+
mutation = use_mutation(MutationOptions(thread_sensitive=True), remove_item) # Old
46+
mutation = use_mutation(remove_item, thread_sensitive=True) # New
47+
```
48+
49+
### Removed
50+
51+
- `QueryOptions` and `MutationOptions` have been removed. Their values are now passed direct into the hook.
3852

3953
## [3.8.1] - 2024-05-07
4054

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from example.models import TodoItem
22
from reactpy import component
33
from reactpy_django.hooks import use_query
4-
from reactpy_django.types import QueryOptions
54
from reactpy_django.utils import django_query_postprocessor
65

76

@@ -11,13 +10,11 @@ def get_items():
1110

1211
@component
1312
def todo_list():
14-
# These `QueryOptions` are functionally equivalent to ReactPy-Django's default values
13+
# These postprocessor options are functionally equivalent to ReactPy-Django's default values
1514
item_query = use_query(
16-
QueryOptions(
17-
postprocessor=django_query_postprocessor,
18-
postprocessor_kwargs={"many_to_many": True, "many_to_one": True},
19-
),
2015
get_items,
16+
postprocessor=django_query_postprocessor,
17+
postprocessor_kwargs={"many_to_many": True, "many_to_one": True},
2118
)
2219

2320
return item_query.data

docs/examples/python/use-mutation-query-refetch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def submit_event(event):
2626
elif item_query.error or not item_query.data:
2727
rendered_items = html.h2("Error when loading!")
2828
else:
29-
rendered_items = html.ul(html.li(item, key=item.pk) for item in item_query.data)
29+
rendered_items = html.ul(
30+
html.li(item.text, key=item.pk) for item in item_query.data
31+
)
3032

3133
# Handle all possible mutation states
3234
if item_mutation.loading:

docs/examples/python/use-mutation-thread-sensitive.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from reactpy import component, html
22
from reactpy_django.hooks import use_mutation
3-
from reactpy_django.types import MutationOptions
43

54

65
def execute_thread_safe_mutation(text):
@@ -11,8 +10,8 @@ def execute_thread_safe_mutation(text):
1110
@component
1211
def my_component():
1312
item_mutation = use_mutation(
14-
MutationOptions(thread_sensitive=False),
1513
execute_thread_safe_mutation,
14+
thread_sensitive=False,
1615
)
1716

1817
def submit_event(event):

docs/examples/python/use-query-args.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
from reactpy_django.hooks import use_query
33

44

5-
def example_query(value: int, other_value: bool = False):
6-
...
5+
def example_query(value: int, other_value: bool = False): ...
76

87

98
@component
109
def my_component():
11-
query = use_query(
12-
example_query,
13-
123,
14-
other_value=True,
15-
)
10+
query = use_query(example_query, {"value": 123, "other_value": True})
1611

1712
return str(query.data)

docs/examples/python/use-query-postprocessor-change.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from reactpy import component
22
from reactpy_django.hooks import use_query
3-
from reactpy_django.types import QueryOptions
43

54

65
def my_postprocessor(data, example_kwarg=True):
@@ -18,11 +17,9 @@ def execute_io_intensive_operation():
1817
@component
1918
def my_component():
2019
query = use_query(
21-
QueryOptions(
22-
postprocessor=my_postprocessor,
23-
postprocessor_kwargs={"example_kwarg": False},
24-
),
2520
execute_io_intensive_operation,
21+
postprocessor=my_postprocessor,
22+
postprocessor_kwargs={"example_kwarg": False},
2623
)
2724

2825
if query.loading or query.error:

docs/examples/python/use-query-postprocessor-disable.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from reactpy import component
22
from reactpy_django.hooks import use_query
3-
from reactpy_django.types import QueryOptions
43

54

65
def execute_io_intensive_operation():
@@ -11,8 +10,8 @@ def execute_io_intensive_operation():
1110
@component
1211
def my_component():
1312
query = use_query(
14-
QueryOptions(postprocessor=None),
1513
execute_io_intensive_operation,
14+
postprocessor=None,
1615
)
1716

1817
if query.loading or query.error:

docs/examples/python/use-query-postprocessor-kwargs.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from example.models import TodoItem
22
from reactpy import component
33
from reactpy_django.hooks import use_query
4-
from reactpy_django.types import QueryOptions
54

65

76
def get_model_with_relationships():
@@ -17,10 +16,8 @@ def get_model_with_relationships():
1716
@component
1817
def my_component():
1918
query = use_query(
20-
QueryOptions(
21-
postprocessor_kwargs={"many_to_many": False, "many_to_one": False}
22-
),
2319
get_model_with_relationships,
20+
postprocessor_kwargs={"many_to_many": False, "many_to_one": False},
2421
)
2522

2623
if query.loading or query.error or not query.data:

docs/examples/python/use-query-thread-sensitive.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from reactpy import component
22
from reactpy_django.hooks import use_query
3-
from reactpy_django.types import QueryOptions
43

54

65
def execute_thread_safe_operation():
@@ -10,10 +9,7 @@ def execute_thread_safe_operation():
109

1110
@component
1211
def my_component():
13-
query = use_query(
14-
QueryOptions(thread_sensitive=False),
15-
execute_thread_safe_operation,
16-
)
12+
query = use_query(execute_thread_safe_operation, thread_sensitive=False)
1713

1814
if query.loading or query.error:
1915
return None

docs/examples/python/use-query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def todo_list():
1818
rendered_items = html.h2("Error when loading!")
1919
else:
2020
rendered_items = html.ul(
21-
[html.li(item, key=item.pk) for item in item_query.data]
21+
[html.li(item.text, key=item.pk) for item in item_query.data]
2222
)
2323

2424
return html.div("Rendered items: ", rendered_items)

0 commit comments

Comments
 (0)