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:

Defining a private field

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

Example

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

@strawberry.type
class Stringable:
value: strawberry.Private[object]
@strawberry.field
def string(self) -> str:
return str(self.value)
@strawberry.field
def repr(self) -> str:
return repr(self.value)
@strawberry.field
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.

@strawberry.type
class Query:
@strawberry.field
def now(self) -> Stringable:
return Stringable(value=datetime.datetime.now())

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

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