admin管理员组文章数量:1391918
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?
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?
Share Improve this question asked Mar 12 at 10:03 Matthew WilcoxsonMatthew Wilcoxson 3,6221 gold badge45 silver badges48 bronze badges2 Answers
Reset to default 0It's currently not possible to do this. For now (strawberry-graphql=0.262.2) you'll have to add in a None
value as above. A future version could be something like:
@strawberry.input
class UserUpdateInput:
name: str = strawberry.UNSET
description: str = strawberry.UNSET
See strawberry issue at 3804
This isn't specific to strawberry; it's a general GraphQL limitation. There are two ways to make an input optional:
nullable:
name: str | None = UNSET
->name: String
with a default value:
name: str = ""
->name: String! = ""
The latter is non-nullable, but it doesn't change the fact that the client can send a valid value - null or empty string respectively. Strawberry is one the few frameworks that at least makes its easy to distinguish an explicit null from client omission.
本文标签:
版权声明:本文标题:python - In strawberry, how do I make a strawberry.input non-nullable but optional in an update mutation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759973a2623695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论