Private Fields

Private (external) fields can provide local context for later resolution. These fields will act as plain fields so will not be exposed in the GraphQL API.

Some uses include:

  • Context that relies upon field inputs.
  • Avoiding fully materializing an object hierarchy (lazy resolution)

Defining a private field

Specifying a field with strawberry.Private[...] will desigate it as internal and not for GraphQL.


Consider the following type, which can accept any Python object and handle converting it to string, representation, or templated output:

class Stringable:
value: strawberry.Private[object]
def string(self) -> str:
return str(self.value)
def repr(self) -> str:
return repr(self.value)
def format(self, template: str) -> str:
return template.format(my=self.value)

The Private[...] type lets Strawberry know that this field is not a GraphQL field. "value" is a regular field on the class, but it is not exposed on the GraphQL API.

class Query:
def now(self) -> Stringable:
return Stringable(

Queries can then select the fields and formats desired, but formatting only happens as requested:

now {
format(template: "{my.year}")
"data": {
"now": {
"format": "2022",
"string": "2022-09-03 17:03:04.923068",
"repr": "datetime.datetime(2022, 9, 3, 17, 3, 4, 923068)"

Was this helpful? What can we improve?

Edit on Github

Newsletter 💌

Do you want to receive the latest updates on Strawberry? Subscribe to our newsletter!