7. The Query entry point
5m

Overview

Let's feature those playlists!

In this lesson, we will write our first entry point to the using the Query type.

Reviewing the mockup

A grid of featured playlists

For this page, we need to retrieve a list of Playlist objects that are featured on the homepage.

We recommend collaborating with your consumers on naming decisions. For this particular feature, we want the to include a featuredPlaylists under the Query type, which is a list of Playlist objects.

Desired GraphQL schema
type Query {
featuredPlaylists: [Playlist!]!
}

Featured playlists

Remember, the Query type is an entry point into our schema, so let's head over to the api/query.py file.

For the hello , we defined a separate function, then used the strawberry.field's resolver to point to that function.

api/query.py
def get_hello():
return "Hello world"
@strawberry.type
class Query:
hello: str = strawberry.field(resolver=get_hello)

For the featuredPlaylists , we'll explore a slightly different approach, using the strawberry.field as a decorator instead.

Under the Query class, we'll add a function called featured_playlists, which will return a list of Playlist types.

api/query.py
class Query:
def featured_playlists() -> list[Playlist]:
...

Strawberry automatically converts snake_case names to camelCase in the schema, so we can keep using the Pythonic snake_case naming convention.

Let's write some hard-coded mock data for now. We'll return a list of three Playlist instances, passing in IDs, names, and we can leave the playlist's description empty.

api/query.py
return [
Playlist(id="1", name="GraphQL Groovin'", description=None),
Playlist(id="2", name="Graph Explorer Jams", description=None),
Playlist(id="3", name="Interpretive GraphQL Dance", description=None),
]

To make this function a , we'll use the strawberry.field function as a decorator to the method, prefixing it with @.

api/query.py
@strawberry.field
def featured_playlists() -> list[Playlist]:
...

It's also good practice to include self in the of the function.

api/query.py
@strawberry.field
def featured_playlists(self) -> list[Playlist]:
...

Lastly, don't forget the description!

api/query.py
@strawberry.field(
description="Playlists hand-picked to be featured to all users."
)
def featured_playlists(self) -> list[Playlist]:

This decorator approach is useful for co-locating the function body with the class and its .

Finally, we'll need to import the Playlist type at the top of the file.

api/query.py
from .types.playlist import Playlist

Hit save and let's get !

Querying for featured playlists

Back over to Sandbox, with the Schema page still open, let's check out the Query tab.

http://localhost:8000

Schema page with Query page open

We've got a new entry here for our featuredPlaylists ! We can use this page to quickly build a in Explorer by clicking the Play button next to the field under the Actions column.

http://localhost:8000

Arrow to Play button under Actions

This will open up Explorer with the Documentation tab showing the featuredPlaylists .

In a new workspace tab, let's build the to ask for the id, name and field for each playlist in featuredPlaylists.

http://localhost:8000

Explorer page with featured playlists query

Tip: You can use the + button beside "" to add all fields in one click!

GraphQL operation
query FeaturedPlaylists {
featuredPlaylists {
id
name
description
}
}

Run the query... and dance it out because we've got our groovy playlists! 🕺💃

http://localhost:8000

Response data

Examining the shape of the JSON response, we can see that it follows the shape of our , which is one of the benefits of . We can see under the data key, we have a property called featuredPlaylists (same name as our ), which returns a list. For each object in the list, we have fields associated with a Playlist type: id, name and description.

We can also see this in table view for a more compact, easy to scan view.

http://localhost:8000

Response data table view

Before we move on, let's clean up the Hello World example. Remove the hello from the Query type, and the corresponding get_hello function.

api/query.py
- def get_hello():
- return "Hello world"
@strawberry.type
class Query:
- hello: str = strawberry.field(resolver=get_hello)

Now that the Playlist type is reachable through an entrypoint in the Query type, we can also clean up our schema initialization. Strawberry automatically adds types to our schema if they are used by any , in this case the Playlist type is used by the featured_playlists in our Query.

Open up api/schema.py, remove the types from the schema and the Playlist import at the top.

schema.py
- from .types.playlist import Playlist
...
-schema = strawberry.Schema(query=Query, types=[Playlist])
+schema = strawberry.Schema(query=Query)

Key takeaways

  • The Query type serves as an entry point into the .
  • You can use @strawberry.field to define for on a type.
  • Strawberry automatically converts snake_case names to camelCase in the schema.

Up next

Those were fun playlists, but they're all hard-coded! In the next lesson, we'll dive into the Spotify Web API, a REST API we'll be using as the for our .

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.