-
-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to set a custom info class for a schema (#3592)
* Add ability to use custom info objects schema-wide * Add RELEASE.md * Improve RELEASE.md example * Use 'Info' class directly instead of default factory for dataclass field * Add no cover pragma + change to TypeError over ValueError * Update the RELEASE example * Update documentation to make note of the new 'info_class' config option * Use 'strawberry.Info' instead of 'strawberry.types.Info' in info_class docs Co-authored-by: Jonathan Ehwald <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add __post_init__ tests for StrawberryConfig * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Allow for the use of custom info classes within directives * Add testing to ensure custom info classes are included in directives and extensions --------- Co-authored-by: Jonathan Ehwald <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
13bd97b
commit 7287047
Showing
8 changed files
with
189 additions
and
4 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,51 @@ | ||
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): | ||
@property | ||
def selected_group_id(self) -> int | None: | ||
"""Get the ID of the group you're logged in as.""" | ||
return self.context["request"].headers.get("Group-ID") | ||
|
||
|
||
@strawberry.type | ||
class Group: | ||
id: strawberry.ID | ||
name: str | ||
|
||
|
||
@strawberry.type | ||
class User: | ||
id: strawberry.ID | ||
name: str | ||
group: Group | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def user(self, id: strawberry.ID, info: CustomInfo) -> Product: | ||
kwargs = {"id": id, "name": ...} | ||
|
||
if info.selected_group_id is not None: | ||
# Get information about the group you're a part of, if | ||
# available. | ||
kwargs["group"] = ... | ||
|
||
return User(**kwargs) | ||
|
||
|
||
schema = strawberry.Schema( | ||
Query, | ||
config=StrawberryConfig(info_class=CustomInfo), | ||
) | ||
``` |
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
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,39 @@ | ||
import pytest | ||
|
||
from strawberry.schema.config import StrawberryConfig | ||
from strawberry.types.info import Info | ||
|
||
|
||
def test_config_post_init_auto_camel_case(): | ||
config = StrawberryConfig(auto_camel_case=True) | ||
|
||
assert config.name_converter.auto_camel_case is True | ||
|
||
|
||
def test_config_post_init_no_auto_camel_case(): | ||
config = StrawberryConfig(auto_camel_case=False) | ||
|
||
assert config.name_converter.auto_camel_case is False | ||
|
||
|
||
def test_config_post_init_info_class(): | ||
class CustomInfo(Info): | ||
test: str = "foo" | ||
|
||
config = StrawberryConfig(info_class=CustomInfo) | ||
|
||
assert config.info_class is CustomInfo | ||
assert config.info_class.test == "foo" | ||
|
||
|
||
def test_config_post_init_info_class_is_default(): | ||
config = StrawberryConfig() | ||
|
||
assert config.info_class is Info | ||
|
||
|
||
def test_config_post_init_info_class_is_not_subclass(): | ||
with pytest.raises(TypeError) as exc_info: | ||
StrawberryConfig(info_class=object) | ||
|
||
assert str(exc_info.value) == "`info_class` must be a subclass of strawberry.Info" |
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