-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Release type: minor | ||
|
||
You can now configure your schemas to provide a custom subclass of | ||
`strawberry.types.Info` to your types and queries. | ||
|
||
```py | ||
import strawberry | ||
from strawberry.schema.config import StrawberryConfig | ||
|
||
from .models import ProductModel | ||
|
||
|
||
class CustomInfo(strawberry.Info): | ||
def is_selected(field: str) -> bool: | ||
"""Check if the field is selected on the top-level of the query.""" | ||
return field in [sel.name for sel in info.selected_fields] | ||
|
||
|
||
@strawberry.type | ||
class Product: | ||
id: strawberry.ID | ||
orders: list[Order] | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def product(self, id: strawberry.ID, info: CustomInfo) -> Product: | ||
kwargs = {"id": id} | ||
|
||
if info.is_selected("orders"): | ||
# Do some expensive operation here that we wouldn't want to | ||
# do if the field wasn't selected. | ||
kwargs["orders"] = ... | ||
|
||
return Product(**kwargs) | ||
|
||
|
||
schema = strawberry.Schema( | ||
Query, | ||
config=StrawberryConfig(info_class=CustomInfo), | ||
) | ||
``` |