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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

It'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.

本文标签: