strawberry-graphql / strawberry-sqlalchemy

A SQLAlchemy Integration for strawberry-graphql
MIT License
84 stars 23 forks source link

Input type? #17

Open Bingdom opened 1 year ago

Bingdom commented 1 year ago

Hello,

I was wondering if there's a way to map to an input type?

In Strawberry, you could just do:

@strawberry.type
class User:
    username: str

@strawberry.input
class UserInput(User):
    pass

But doing that with this library doesn't make strawberry recognize it as an input

@_strawberryMapper.type(schema.User)
class User:
    pass

@strawberry.input
class UserInput(User):
    pass

_strawberryMapper.finalize()

Error:

25 TypeError: UserInput fields cannot be resolved. Input field type must be a GraphQL input type.

Upvote & Fund

Fund with Polar

TimDumol commented 1 year ago

That's a use case we haven't considered. You can possibly try something like:

from dataclasses import make_dataclass

@_strawberryMapper.type(schema.User)
class User:
    pass

_strawberryMapper.finalize()

UserInput = strawberry.input(make_dataclass('UserInput', [], bases=(_strawberryMapper.mapped_types['User'],))

Haven't actually tried this code, but maybe it will give you a good starting point.

Bingdom commented 1 year ago

Hi,

Thanks for the help.

Strangely, I'm getting the same problem.

What would be the main difference writing like that vs naturally? Except I suppose the additional methods dataclass provides.

I suspect there might be a property that doesn't get correctly overridden that strawberry uses to identify the type?

Ckk3 commented 10 months ago

Hi, @Bingdom, you can use @strawberry.input with @strawberry_sqlalchemy_mapper.type.

Like this:

@strawberry.input
@strawberry_sqlalchemy_mapper.type(User)
class UserInput:
    __exclude__ = ["id", "password_hash"]

This help me with that limitation, I hope it can help you too!