-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new strawberry.Parent annotation to support static resolvers with…
… non-self types. (#3017)
- Loading branch information
Showing
14 changed files
with
387 additions
and
28 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,26 @@ | ||
Release type: minor | ||
|
||
Adds new strawberry.Parent type annotation to support resolvers without use of self. | ||
|
||
E.g. | ||
|
||
```python | ||
@dataclass | ||
class UserRow: | ||
id_: str | ||
|
||
|
||
@strawberry.type | ||
class User: | ||
@strawberry.field | ||
@staticmethod | ||
async def name(parent: strawberry.Parent[UserRow]) -> str: | ||
return f"User Number {parent.id}" | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def user(self) -> User: | ||
return UserRow(id_="1234") | ||
``` |
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,26 @@ | ||
--- | ||
title: Conflicting Arguments Error | ||
--- | ||
|
||
# Conflicting Arguments Error | ||
|
||
## Description | ||
|
||
This error is thrown when you define a resolver with multiple arguments that | ||
conflict with each other, like "self", "root", or any arguments annotated with | ||
strawberry.Parent. | ||
|
||
For example the following code will throw this error: | ||
|
||
```python | ||
import strawberry | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def hello( | ||
self, root, parent: strawberry.Parent[str] | ||
) -> str: # <-- self, root, and parent all identify the same input | ||
return f"hello world" | ||
``` |
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,54 @@ | ||
from __future__ import annotations | ||
|
||
from functools import cached_property | ||
from typing import TYPE_CHECKING, List, Optional | ||
|
||
from .exception import StrawberryException | ||
from .utils.source_finder import SourceFinder | ||
|
||
if TYPE_CHECKING: | ||
from strawberry.types.fields.resolver import StrawberryResolver | ||
|
||
from .exception_source import ExceptionSource | ||
|
||
|
||
class ConflictingArgumentsError(StrawberryException): | ||
def __init__( | ||
self, | ||
resolver: StrawberryResolver, | ||
arguments: List[str], | ||
): | ||
self.function = resolver.wrapped_func | ||
self.argument_names = arguments | ||
|
||
self.message = ( | ||
f"Arguments {self.argument_names_str} define conflicting resources. " | ||
"Only one of these arguments may be defined per resolver." | ||
) | ||
|
||
self.rich_message = self.message | ||
|
||
self.suggestion = ( | ||
f"Only one of {self.argument_names_str} may be defined per resolver." | ||
) | ||
|
||
self.annotation_message = self.suggestion | ||
|
||
@cached_property | ||
def argument_names_str(self) -> str: | ||
return ( | ||
", ".join(f'"{name}"' for name in self.argument_names[:-1]) | ||
+ " and " | ||
+ f'"{self.argument_names[-1]}"' | ||
) | ||
|
||
@cached_property | ||
def exception_source(self) -> Optional[ExceptionSource]: | ||
if self.function is None: | ||
return None # pragma: no cover | ||
|
||
source_finder = SourceFinder() | ||
|
||
return source_finder.find_argument_from_object( | ||
self.function, self.argument_names[1] # type: ignore | ||
) |
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 @@ | ||
from typing import TypeVar | ||
from typing_extensions import Annotated | ||
|
||
|
||
class StrawberryParent: | ||
... | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
Parent = Annotated[T, StrawberryParent()] | ||
Parent.__doc__ = """Represents a parameter holding the parent resolver's value. | ||
This can be used when defining a resolver on a type when the parent isn't expected | ||
to return the type itself. | ||
Example: | ||
>>> import strawberry | ||
>>> from dataclasses import dataclass | ||
>>> | ||
>>> @dataclass | ||
>>> class UserRow: | ||
... id_: str | ||
... | ||
>>> @strawberry.type | ||
... class User: | ||
... @strawberry.field | ||
... @staticmethod | ||
... async def name(parent: strawberry.Parent[UserRow]) -> str: | ||
... return f"User Number {parent.id}" | ||
... | ||
>>> @strawberry.type | ||
>>> class Query: | ||
... @strawberry.field | ||
... def user(self) -> User: | ||
... return UserRow(id_="1234") | ||
... | ||
""" |
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
Oops, something went wrong.