Misplaced Field Error
Description
This error is thrown when strawberry.field() is assigned to a field of a
strawberry.pydantic type, for example the following code will throw this
error:
import pydanticimport strawberry
@strawberry.pydantic.typeclass User(pydantic.BaseModel): name: str email: str = strawberry.field(description="The user's email") This happens because Pydantic treats the strawberry.field() like a dataclass
field: it keeps its default value and discards the rest of its options, like
descriptions and permissions.
The same error is thrown when:
-
strawberry.pydantic.fieldis used on a field of the model, in its annotation or as its default value, likeage: Annotated[int, strawberry.pydantic.field(name="yearsOld")].strawberry.pydantic.fieldonly adds fields with a resolver, so its options would be ignored. - a method is decorated with
@strawberry.fieldin a base class that isnβt decorated, or in a Pydantic model that makes Pydantic ignore Strawberry fields withignored_types. Pydantic types only read@strawberry.fieldmethods from regular Strawberry types and interfaces, so the field would silently be missing from the schema.
How to fix this error
You can fix this error by customizing the fields of the model with
strawberry.field() in their annotation, and adding fields with a resolver with
@strawberry.pydantic.field :
from typing import Annotated
import pydanticimport strawberry
@strawberry.pydantic.typeclass User(pydantic.BaseModel): name: str email: Annotated[str, strawberry.field(description="The user's email")] age: Annotated[int, strawberry.field(name="yearsOld")]
@strawberry.pydantic.field def shout(self) -> str: return self.name.upper() For a base class that isnβt a Pydantic model, you can also decorate it with
@strawberry.type or @strawberry.interface , as Pydantic types inherit the
fields with a resolver of regular Strawberry types and interfaces.