-
-
Notifications
You must be signed in to change notification settings - Fork 541
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
Comments
strawberry.Parent
not supporting forward refs
@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 |
Great question @caspervk and sorry for the bad documentation! Here's some code grabbed directly from my company's repository that is running: @strawberry.type
class Foo:
@strawberry.field
@staticmethod
async def bar(
foo: strawberry.Parent[Annotated["Foo", strawberry.lazy(".foo")]], info: types.Info
) -> str | None:
pass It's unfortunately mad verbose, but the challenge is that we need to have the resolver defined before we've even finished defining the strawberry.type, so we need a forward declaration of sorts. Patrick might be able to find a more clever succinct definition, but this is what we've been using so far. If that solves your problem, let me know, and I can send out a PR updating the docs. |
Thank you! It works! 👑 I didn't even think of using |
@mattalbr, please advise why I am unable to get this working for the Query object. The reference (parent) is always None. I am at version 0.237.3.
|
Hi @tokarenko -- it's hard to tell from just that snippet, but based on your class being called "Query" I'm assuming that's the top level query class for your schema. If so, you'll need to pass the root value in when you call https://strawberry.rocks/docs/types/schema#root_value-optionalany--none If the value is present when you name the field "root" and not when you use the |
@mattalbr , thank you for the instructions. It works. When the top level Query class is added to |
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.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
The text was updated successfully, but these errors were encountered: