Guarded Field Override Error
Description
This error is raised when a type overrides an inherited field that has permission classes or extensions, and the override canβt keep them. It happens in two cases.
A plain annotation overrides the field
import strawberry
@strawberry.interfaceclass Account: name: str email: str = strawberry.field(permission_classes=[IsStaff])
@strawberry.typeclass Company(Account): name: str email: str # raises GuardedFieldOverrideError vat_number: str A plain annotation replaces the inherited field with a new one, like it does for
dataclasses, so the email of a Company would be readable by anyone, while
the email of other types implementing Account is only readable by staff.
Fields that only have directives can be overridden with a plain annotation: directives describe the field in the schema, and types often re-declare the fields of an interface, like the ones using the directives of Apollo Federation.
An override changes the resolver of a field with extensions
@strawberry.interfaceclass Account: email: str = strawberry.field(extensions=[PermissionExtension([IsStaff()])])
@strawberry.typeclass Company(Account): @strawberry.field # raises GuardedFieldOverrideError def email(self) -> str: return self.billing_email An override declared with strawberry.field() keeps the permission classes and
extensions of the field it overrides, unless it passes its own. Extensions can
depend on the resolver of the field, so theyβre only kept when the override
doesnβt add a resolver or remove the one of the inherited field.
How to fix this error
Override the field with strawberry.field() , which keeps the permission classes
and extensions of the inherited field:
@strawberry.typeclass Company(Account): name: str email: str = strawberry.field(description="Billing email") vat_number: str Or remove the annotation to inherit the field as it is:
@strawberry.typeclass Company(Account): vat_number: str When the override changes the resolver, pass the extensions the field should have:
@strawberry.typeclass Company(Account): @strawberry.field(extensions=[PermissionExtension([IsStaff()])]) def email(self) -> str: return self.billing_email To publish the field without them on purpose, pass permission_classes=[] to
remove the permission classes, and extensions=[] to remove the extensions:
@strawberry.typeclass Company(Account): email: str = strawberry.field(permission_classes=[])