Replies: 4 comments 2 replies
-
I'd go for option 2, especially since we have a good-ish support for MyPy, that plus a good convention should be enough for basic usages, and we still allow for more advanced use cases :) I also want to see if we can make MyPy think that a strawberry type is a protocol too, so that when returning a compatible type it won't complain :) |
Beta Was this translation helpful? Give feedback.
-
I think option 2 is viable, but as it stands now, option 1 looks like the easiest fix from what the current functionality is. A variable rename seems easier than type-enforcement, but maybe the type-enforcement is a feature of the library you'd like to have for users. Nonetheless, a variable rename to something like The big point to get across, is that this variable -or the value passed into it- is not the same as If you choose option 1, I think it's important to conceptually differentiate the resolver method by telling users it's like a static method, but in this case, the first parameter will always be the resolved object from the parent resolver, whatever the value actually is. Even if this isn't documented at the first use of All that being said, I definitely would like the functionality of option 2 or 3. Ensuring a proper instance on Option 1 seems the easiest most immediate fix, but Option 2 and 3 seem like the more pythonic way to do things. |
Beta Was this translation helpful? Give feedback.
-
Ran into this recently, and one thing that's frustrating is that there is currently no way for users to implement option 1 because of how https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/types/fields/resolver.py is implemented to explicitly match "self" "root" and "info". As a user, if you use a library that returns a different type, your only option right now is to use "self" which is IMO very confusing since it's not accurate. IMO a nice way to implement 1) would be to make a type annotation similar to @strawberry.type
class Foo:
@strawberry.field
@staticmethod # Needed for MyPy
async def bar(can_be_whatever_name_youd_like: strawberry.Source[CanBeWhateverYoudLike], info: strawberry.Info):
pass I'm open to writing a PR implementing strawberry.Source if it's something you're open to @patrick91 |
Beta Was this translation helpful? Give feedback.
-
Closing the loop here (or really continuing the discussion), #3017 is merged implementing A. What parent aliases do we recommend, and thus choose for our documentation? My personal take is that I find An example I experienced: the strawberry-sqlalchemy library that my team uses, similar to a lot of ORM mapper libraries, returns a different type than the resolver's annotation. Because the With that said, my preference is: A. Use Had a nice conversation with Patrick about this yesterday, who I know has his own thoughts, but I figured it would be good to move the discussion here so others can weigh in, and whatever we do we might want to wait a minute to see how |
Beta Was this translation helpful? Give feedback.
-
I wanted to ask people about how they feel about class resolvers having a
self
argument or not? It's not strictly necessary because resolver functions are all actually treated as static methods and so the first parameter is the value that was resolved from the "parent" in the schema.For example:
The fact that resolvers for Object Types don't have to return an instance of that type can be a useful optimisation tool (especially when dealing with things that often look like the type i.e. Django models) but in my experience (from Graphene) it does lead to a lot of confusion because it's not very "pythonic".
As far as I see it we have a few options:
self
as the first argument in documentation. Instead useroot
orsource
to make it clear that it's not an instance of the current type.self
consistently in documentation and also make sure that resolvers that return another type always return an instance of that type so thatself
is always "correct". Document the fact that you can return anything from a resolver as an advanced feature only.self
is always "correct".My current thinking is that we go with option 2 but what do other people think?
Beta Was this translation helpful? Give feedback.
All reactions