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

strawberry.Parent not supporting forward refs #3481

Open
andrewkruse opened this issue May 1, 2024 · 1 comment
Open

strawberry.Parent not supporting forward refs #3481

andrewkruse opened this issue May 1, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@andrewkruse
Copy link

andrewkruse commented May 1, 2024

I would like the strawberry documentation on accessing parent with function resolvers on this page tweaked to be more clear, or maybe corrected?

From what I understand in the docs, its suggesting you end up with the following. However, this doesn't even run? I have tried swapping the definitions both directions, they have the same issue. I had to resort to the self method on a method resolver, which seems less desirable to me since the docs specifically call out that it might not work quite right everywhere.

and it works like it should in Python, but there might be cases where it doesn’t properly follow Python’s semantics

def get_full_name(parent: strawberry.Parent[User2]) -> str:
    return f"{parent.first_name} {parent.last_name}"

@strawberry.type
class User2:
    first_name: str
    last_name: str
    full_name: str = strawberry.field(resolver=get_full_name)
Traceback (most recent call last):
  File "/Users/.../Library/Application Support/JetBrains/IntelliJIdea2024.1/plugins/python/helpers/pydev/pydevd.py", line 1535, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../Library/Application Support/JetBrains/IntelliJIdea2024.1/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/.../src/api/python-graphql-poc/src/main.py", line 21, in <module>
    def get_full_name(parent: strawberry.Parent[User2]) -> str:
                                                ^^^^^
NameError: name 'User2' is not defined

Perhaps there's a quirk in here where the structure of my file is part of the problem since everything is top level? I am using FastAPI, uvicorn and strawberry.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@patrick91 patrick91 changed the title Review/Revise & Maybe Correct Strawberry Docs On Parent Resolving strawberry.Parent not supporting forward refs May 2, 2024
@patrick91 patrick91 added the bug Something isn't working label May 2, 2024
@patrick91
Copy link
Member

@andrewkruse I think you found a bug, this should work:

from __future__ import annotations   # NOTE: this

import strawberry


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


@strawberry.type
class User:
    first_name: str
    last_name: str
    full_name: str = strawberry.field(resolver=get_full_name)


@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
        return User(first_name="John", last_name="Doe")


schema = strawberry.Schema(query=Query)

but it doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants