Type hints are a modern feature of Python available since 3.5 whose existence is heavily influenced by the features of type-safe languages such as Rust. To learn more about type hints in Python, see Real Python’s Type hinting walkthrough .
When using Strawberry to build graphQL APIs, as was shown in
Schema basics , type hints
are required within classes decorated by
@strawberry.input and functions decorated by
strawberry.mutation . These type hints are sourced as the keywords
float , and from packages imported directly from the Python standard
typing , that has been available since Python 3.5,
datetime , and
Mapping to graphQL types
The complete mapping of the required type hints for the relevant graphQL types is as follows:
datetime , and
decimal are all part of the Python standard
library. There is also
typing.Dict that possesses no mapping since it is the
entire structure of the graphQL query itself that is a dictionary.
There are a few different ways in which these Python type hints can be used to express the required Strawberry graphQL type annotation.
- For versions of Python >= 3.10, it is possible to annotate an array of types
list[Type]. However, for all previous versions,
typing.List[Type]must be used instead.
- The annotation
|is shorthand for
typing.Union, allowing either of
TypeA | TypeBinterchangably.
- The annotation
typing.Optional[Type]is shorthand for
typing.Union[None, Type], which is itself equivalent to
None | Type.
A complete example of this, extending upon Schema basics , might be the following:
selfwithin a resolver’s definition, whether decorated as
@strawberry.mutation, never needs a type hint because it can be inferred.
Optionalis the way to tell Strawberry that a field is nullable. Without it, every field is assumed to be non-null. This is in contrast to graphene wherein every field is assumed nullable unless
- Type hinting doesn’t stop at being a requirement for Strawberry to function,
it is also immensely helpful for collaborating developers. By specifying the
get_books_by_author, an IDE equipped with PyLance will be able to infer that
bookwithin the list comprehension is a dictionary and so will understand that
.get()is a method function of the
dictclass. This helps the readability and maintainability of written code.
Yet, much like the low-level languages of C, Java, and Rust, the graphQL query language is statically typed since the data types defined by the schema must be known prior to compiling the API code in order to define a definite schema to query against.
In the low-level statically typed languages mentioned above, every function must have the types of both their arguments and returns explicitly declared so that the compiler can interpret their behaviours correctly and ensure type safety and consistency.
Strawberry takes inspiration from these languages by requiring that all of its types, fields, resolvers, and mutations declare the types of their arguments and returns. Through this, the schema is generated in a standard and efficient way that aligns with the style-direction of Python and programming as a whole.