strawberry-sqlalchemy icon indicating copy to clipboard operation
strawberry-sqlalchemy copied to clipboard

Input type?

Open Bingdom opened this issue 2 years ago • 3 comments

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.

Bingdom avatar May 18 '23 07:05 Bingdom

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.

TimDumol avatar May 18 '23 07:05 TimDumol

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?

Bingdom avatar May 22 '23 05:05 Bingdom

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!

Ckk3 avatar Sep 06 '23 00:09 Ckk3