strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Introduce `.as_input` for object types

Open patrick91 opened this issue 2 years ago • 1 comments

Imagine we have this type:

@strawberry.type
class Frequency:
    kind: str
    times: int

and we want to use it as input, to do so we need to make a copy of it pretty much:

@strawberry.input
class FrequencyInput:
    kind: str
    times: int

But what if we allowed doing something like this:

@strawberry.type
class Frequency:
    kind: str
    times: int


FrequencyInput = Frequency.as_input() # we don't have access to the variable name, but we'll append Input to the original name
FrequencyInput = Frequency.as_input(name="AddFrequencyInput")
FrequencyInput = Frequency.as_input(all_optional=True) # not sure about this yet

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 avatar Nov 17 '23 08:11 patrick91

Would absolutely love see this happen.

Stumbled on this issue when posting a question in strawberry discord community. I think having the brief here would be quite useful too:

We are using sqlalchemy to define db models of the data. On top of this, we need at least two classes to represent strawberry gql output and input. There is also a notion of the data itself (you can argue, however, that this could very well be represented by strawberry output, though, i.e. strawberry.type decorated class). And so we end up needing to define at least 3 classes with essentially the same data.

I have tried to abstract this away by creating what you see in the screenshot, however this does not really help with the fact that you still need to define all those classes separately. Usefulness of it rather lies in the fact that everything that you might need is contained in a single class.

Therefore, it would be a lot of help already if we could knock out needing to define at least one of these classes, namely input.

Screenshot_2023-11-17_at_21 04 54

nazariyv avatar Nov 17 '23 23:11 nazariyv