It would be nice if strawberry.field(default_factory=)
was supported for private fields.
Consider the following code:
@strawberry.type
class Widget:
private_keys: strawberry.Private[list[int]] = strawberry.field(default_factory=list)
When used this results in the following error:
strawberry.exceptions.private_strawberry_field.PrivateStrawberryFieldError: Field private_keys on type Widget cannot be both private and a strawberry.field
Known work-arounds, you can use dataclasses.field
or annotate the entire class as a dataclass
like so:
@strawberry.type
class Widget:
private_keys: strawberry.Private[list[int]] = dataclasses.field(default_factory=list)
@strawberry.type
@dataclasses.dataclass
class Widget:
data: Optional[strawberry.scalars.JSON] = strawberry.field(default_factory=dict)
Given that we've seen similar issues for input types where the same work arounds apply, perhaps strawberry.field's default value/factory should mimic dataclasses implementation more.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too