strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.96k stars 527 forks source link

Using pydantic model's methods as field resolvers #2447

Open OlehDziuba opened 1 year ago

OlehDziuba commented 1 year ago

Feature Request Type

Description

Using computable properties or methods of the pydantic model instead of strawberry.field

  class User(BaseModel):
      first_name: str
      last_name: str 

      def full_name(self, separator: str) -> str: 
          return f"{self.first_name}{separator}{self.last_name}"

@strawberry.experimental.pydantic.type(model=User, all_fields=True)
 class UserType: 
     pass

Now the scheme looks like this:

 type UserType{
   first_name: String!
   last_name: String!
 }

But it would be convenient to have a scheme that looks like

type UserType{
  first_name: String!
  last_name: String!
  full_name(separator: String!): String!
} 

Upvote & Fund

Fund with Polar

mecampbellsoup commented 5 months ago

I imagine this is now solved by https://strawberry.rocks/docs/guides/accessing-parent-data...