[Python] In strawberry, how do I make a strawberry.input non-nullable but optional in an...

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have two fields in an input:

    @strawberry.input
    class UserUpdateInput:
    name: str | None = strawberry.UNSET
    description: str | None = strawberry.UNSET


    name - in the backend database - is not nullable. description is nullable.

    Above is almost what I need:

    • The fields are optional in a mutation.
    • They are strawberry.UNSET when not specified.
    • Description can be set to None, or any string.

    But now name can also be set to None which is wrong. But taking out the None type-hint (i.e name: str = strawberry.UNSET) makes name no longer optional.

    In GraphQL I'd like (something like) this:

    mutation Update {
    updateUser(id:1, userUpdateInput: {name:"tom",description:"too old for this s..."}) {
    id
    }
    }


    or this:

    mutation Update {
    updateUser(id:1, userUpdateInput: {description:null}) {
    id
    }
    }


    but not:

    mutation Update {
    updateUser(id:1, userUpdateInput: {name:null}) {
    id
    }
    }


    Is there a way to specify both optional and non-nullable in strawberry's schema?

    Continue reading...

Compartilhe esta Página