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.interface
class Account:
name: str
email: str = strawberry.field(permission_classes=[IsStaff])
@strawberry.type
class 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.interface
class Account:
email: str = strawberry.field(extensions=[PermissionExtension([IsStaff()])])
@strawberry.type
class 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.type
class Company(Account):
name: str
email: str = strawberry.field(description="Billing email")
vat_number: strOr remove the annotation to inherit the field as it is:
@strawberry.type
class Company(Account):
vat_number: strWhen the override changes the resolver, pass the extensions the field should have:
@strawberry.type
class 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.type
class Company(Account):
email: str = strawberry.field(permission_classes=[])