Queries
In GraphQL you use queries to fetch data from a server. In Strawberry you can define the data your server provides by defining query types.
By default all the fields the API exposes are nested under a root Query type.
This is how you define a root query type in Strawberry:
This creates a schema where the root type Query has one single field called name.
As you notice we don't provide a way to fetch data. In order to do so we need to
provide a resolver
, a function that knows how to fetch data for a specific
field.
For example in this case we could have a function that always returns the same name:
So now, when requesting the name field, the get_name
function will be called.
Alternatively a field can be declared using a decorator:
Arguments
GraphQL fields can accept arguments, usually to filter out or retrieve specific objects:
Additional metadata can be added to arguments, for example a custom name and
description using strawberry.argument
with
typing.Annotated :