Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update accessing parent data guide with lazy type declaration #3714

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/guides/accessing-parent-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ pass us the parent of the field, we need to add a new argument with type
`strawberry.Parent[ParentType]`, like so:

```python
def get_full_name(parent: strawberry.Parent[User]) -> str:
def get_full_name(parent: strawberry.Parent[Annotated["User", strawberry.lazy(".")]]) -> str:
return f"{parent.first_name} {parent.last_name}"
```

`strawberry.Parent` tells Strawberry to pass the parent value of the field, in
this case it would be the `User`.
this case it would be the `User`. Because the `User` class has not been defined
yet at the point that we define the resolver, we need to use a [lazy](
https://strawberry.rocks/docs/types/lazy) type declaration.

> **Note:** `strawberry.Parent` accepts a type argument, which will then be used
> by your type checker to check your code!
Expand All @@ -73,7 +75,7 @@ Historically Strawberry only supported passing the parent value by adding a
parameter called `root`:

```python
def get_full_name(root: User) -> str:
def get_full_name(root: Annotated["User", strawberry.lazy(".")]) -> str:
return f"{root.first_name} {root.last_name}"
```

Expand All @@ -83,7 +85,7 @@ follows Strawberry's philosophy of using type annotations. Also, with
work:

```python
def get_full_name(user: strawberry.Parent[User]) -> str:
def get_full_name(user: strawberry.Parent[Annotated["User", strawberry.lazy(".")]]) -> str:
return f"{user.first_name} {user.last_name}"
```

Expand All @@ -102,7 +104,7 @@ class User:
last_name: str

@strawberry.field
def full_name(self, parent: strawberry.Parent[User]) -> str:
def full_name(self, parent: strawberry.Parent[Annotated["User", strawberry.lazy(".")]]) -> str:
return f"{parent.first_name} {parent.last_name}"
```

Expand Down Expand Up @@ -212,7 +214,7 @@ class User:

@strawberry.field
@staticmethod
def full_name(parent: strawberry.Parent[User]) -> str:
def full_name(parent: strawberry.Parent[Annotated["User", strawberry.lazy(".")]]) -> str:
return f"{parent.first_name} {parent.last_name}"
```

Expand Down
Loading