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 pydantic
import strawberry
@strawberry.pydantic.type
class 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:

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 pydantic
import strawberry
@strawberry.pydantic.type
class 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.

Contributors:

Edit this page on GitHub