Strawberry supports using Python’s Generic typing to dynamically create
reusable types.
Strawberry will automatically generate the correct GraphQL schema from the
combination of the generic type and the type arguments. Generics are supported
in Object types, Input types, and Arguments to queries, mutations, and scalars.
Arguments to queries and mutations can also be made generic by creating Generic
Input types. Here we’ll define an input type that can serve as a collection of
anything, then create a specialization by using as a filled-in argument on a
mutation.
Note: Pay attention to the fact that both CollectionInput and
PostInput are Input types. Providing posts: CollectionInput[Post] to
add_posts (i.e. using the non-input Post type) would have resulted in an
error:
PostCollectionInput fields cannot be resolved. Input field type must be a
GraphQL input type
Using multiple specializations of a Generic type will work as expected. Here we
define a Point2D type and then specialize it for both int s and float s.