4. Hello world
5m

Overview

For now, we'll keep things simple and try a classic "Hello world" example for our schema. Don't worry, we'll jump into playlists and tracks later on!

In this lesson, we will:

  • Learn about the Query type
  • Create a schema based on the Query type

The Query type

The Query type defines a list of everything we're allowed to ask for from our . It's the entry point into our schema!

Everything API-related will live inside the api folder. We'll keep root types such as Query and later on Mutation in the root of the api folder. All other will live in the api/types folder.

Let's create a file called query.py at the root of api.

At the top, let's import the strawberry module.

api/query.py
import strawberry

Then, we'll add a class called Query.

api/query.py
class Query:
...

Right now, this is just a normal Python class. To define it as a type, we apply the strawberry.type function.

strawberry.type

This function tells Strawberry that the class is a type and should be included in the schema. It also finds all the properties of a class and adds them as of the GraphQL type.

But it does more than that! Strawberry uses dataclasses under the hood to create a fully functional class with constructors, equality checks, and more.

strawberry.type takes in multiple , but two commonly-used arguments are:

  • description: sets the type description to help API consumers make sense of what the type is or how it should be used.
  • name: sets the name of the type in the . By default, Strawberry will generate the name by converting the name of the class to PascalCase (a common convention).

Let's go ahead and define the Query type as a type. Just before the class definition, we'll apply the strawberry.type function and use it as a decorator. (That means we have to add the @ prefix!)

api/query.py
@strawberry.type
class Query:

Inside the Query class, let's define the hello property, which returns a str type. (You can remove the ...!)

api/query.py
hello: str

This is the bare minimum we need for a valid . The GraphQL schema equivalent in would look something like this:

The equivalent GraphQL schema
type Query {
hello: String!
}

Defining a resolver

We still need a way to return data for this , using a . To define a resolver, we use the strawberry.field function.

strawberry.field

This function tells Strawberry that a particular property of a class is a and can be resolved with a specific function.

strawberry.field takes in multiple but three commonly-used arguments are:

  • resolver: the function name of the responsible for returning data for the .
  • description: sets the type description to help API consumers make sense of what the type is or how it should be used.
  • name: sets the name of the type in the . By default, Strawberry will generate the name by converting the name of the property to camelCase.

Defining the hello resolver

Let's see this in action.

First, we'll define a new function, outside of the Query class, called get_hello. It returns a hard-coded string "Hello world".

api/query.py
def get_hello():
return "Hello world"

Next, we'll need to point to this function as the for the hello property. Inside the Query class, find the hello property and set it to the value of the strawberry.field function, passing in the resolver and setting it to get_hello.

api/query.py
hello: str = strawberry.field(resolver=get_hello)

Perfect, we've got the contents of the schema and the in place.

Creating a schema

Our needs to know about the Query type. In the previous lesson, we used ... as a placeholder for our schema. Let's update that to point to a real schema that includes the Query type.

First, we'll create a new file called schema.py in the api folder.

📦 odyssey-intro-strawberry
┣ 📂 api
┃ ┣ 📄 __init__.py
┃ ┣ 📄 query.py
┃ ┗ 📄 schema.py
┣ 📂 data
┃ ┗ 📄 openapi.json
┣ 📄 main.py
┣ 📄 pyproject.toml
┣ 📄 README.md
┣ 📄 requirements-dev.txt
┗ 📄 requirements.txt

Inside, we'll import strawberry and the Query class we just created.

api/schema.py
import strawberry
from .query import Query

To create a schema, we use the strawberry.Schema class. This class takes in a query (the entry point into our schema). We'll point it to the Query type. We'll store this in a called schema.

api/schema.py
schema = strawberry.Schema(query=Query)

We'll be adding more types to the schema throughout the course.

Updating the GraphQL endpoint

Now we can update our FastAPI app to use this schema! Jump back to the main.py file.

Let's import the schema object at the top of the file.

main.py
from api.schema import schema

Then, replace the ... placeholder with schema.

main.py
- graphql_router = GraphQLRouter(..., path="/", graphql_ide="apollo-sandbox")
+ graphql_router = GraphQLRouter(schema, path="/", graphql_ide="apollo-sandbox")

Our should be ready to go!

Practice

What is the purpose of the Query type in GraphQL?
How is the Query type registered in the GraphQL server in the annotation-based approach?

Key takeaways

  • The of the Query type are entry points into our schema. These are the top-level that a consumer can for.
  • We use the strawberry.type to define a class as a type and should be included in the schema. It also finds all the properties of a class and adds them as of the GraphQL type.
  • We use the strawberry.field function to define a particular property of a class as a and can be resolved with a specific function.
  • We can create a schema using the strawberry.Schema class and pass the Query type to it.

Up next

Our is ready to receive queries. In the next lesson, we'll discover the best way to write and send queries: Apollo Explorer.

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.