7. The Query entry point


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.

def get_hello():
return "Hello world"
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.

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.

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 @.

def featured_playlists() -> list[Playlist]:

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

def featured_playlists(self) -> list[Playlist]:

Lastly, don't forget the description!

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.

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.


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.


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.


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 {

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


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.


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.

- def get_hello():
- return "Hello world"
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.

- 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 .


Share your questions and comments about this lesson

This course is currently in

. 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.