Unresolved Strawberry Field Error
Description
This error is raised when Strawberry canโt read the strawberry.field() options
in a fieldโs annotation when the type is created:
- on Python 3.11 to 3.13, when the annotation uses a type that isnโt defined yet;
- on every Python version, when the options themselves use names that arenโt defined yet, like a permission class defined after the type.
For example, a type defined further down the module, in a module with
from __future__ import annotations :
from __future__ import annotations
from typing import Annotated
import strawberry
@strawberry.typeclass User: account: Annotated[Account, strawberry.field(permission_classes=[IsAdmin])]
@strawberry.typeclass Account: iban: str Strawberry reads the strawberry.field() options when the type is created, but
before Python 3.14 it canโt evaluate an annotation that uses names that arenโt
defined yet. The type itself is resolved later, when the schema is built, so
instead of silently ignoring options like the permissions above, Strawberry
raises this error.
The same applies to a shared field, like Annotated[Account, ADMIN_ONLY_FIELD] ,
and to an Annotated alias with strawberry.field() , like
AdminOnly[Account] .
How to fix this error
Pass the options as the fieldโs default instead of in Annotated . This works
for any type, including types that reference themselves:
from __future__ import annotations
import strawberry
@strawberry.typeclass User: account: Account = strawberry.field(permission_classes=[IsAdmin])
@strawberry.typeclass Account: iban: str Or define the types the annotation uses before the type that uses them:
from __future__ import annotations
from typing import Annotated
import strawberry
@strawberry.typeclass Account: iban: str
@strawberry.typeclass User: account: Annotated[Account, strawberry.field(permission_classes=[IsAdmin])] Or use strawberry.lazy() for the type, for example for types only imported
under TYPE_CHECKING . It works together with strawberry.field() :
from __future__ import annotations
from typing import TYPE_CHECKING, Annotated
import strawberry
if TYPE_CHECKING: from .accounts import Account
@strawberry.typeclass User: account: Annotated[ Account, strawberry.lazy(".accounts"), strawberry.field(permission_classes=[IsAdmin]), ] Python 3.14 and newer read the options of these annotations without any change.
The names used by the options themselves, like a permission class, a directive or a default value, have to be defined before the type on every Python version, as the options are evaluated when the type is created:
from __future__ import annotations
from typing import Annotated
import strawberry
class IsAdmin(BasePermission): ... # defined before `User`
@strawberry.typeclass User: account: Annotated[Account, strawberry.field(permission_classes=[IsAdmin])] An Annotated alias with strawberry.field() , like AdminOnly[Account] , has
to be defined before the types that use it, on every Python version.