Overview
Let's feature those playlists!
In this lesson, we will write our first entry point to the GraphQL schema using the Query type.
Reviewing the mockup
For this page, we need to retrieve a list of Playlist objects that are featured on the homepage.
We recommend collaborating with your graph consumers on naming decisions. For this particular feature, we want the GraphQL schema to include a featuredPlaylists field under the Query type, which is a list of Playlist objects.
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 field, we defined a separate resolver function, then used the strawberry.field's resolver argument to point to that function.
def get_hello():return "Hello world"@strawberry.typeclass Query:hello: str = strawberry.field(resolver=get_hello)
For the featuredPlaylists field, 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 field 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 field 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 resolver, we'll use the strawberry.field function as a decorator to the method, prefixing it with @.
@strawberry.fielddef featured_playlists() -> list[Playlist]:...
It's also good practice to include self in the arguments of the resolver function.
@strawberry.fielddef featured_playlists(self) -> list[Playlist]:...
Lastly, don't forget the GraphQL description!
@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 resolver function body with the class and its fields.
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!
Querying for featured playlists
Back over to Sandbox, with the Schema page still open, let's check out the Query tab.
We've got a new entry here for our featuredPlaylists field! We can use this page to quickly build a query in Explorer by clicking the Play button next to the field under the Actions column.
This will open up Explorer with the Documentation tab showing the featuredPlaylists field.
In a new workspace tab, let's build the query to ask for the id, name and field for each playlist in featuredPlaylists.
Tip: You can use the plus (⊕) button beside "Fields" to add all fields in one click!
query FeaturedPlaylists {featuredPlaylists {idnamedescription}}
Run the query... and dance it out because we've got our groovy playlists! 🕺💃
Examining the shape of the JSON response, we can see that it follows the shape of our query, which is one of the benefits of GraphQL. We can see under the data key, we have a property called featuredPlaylists (same name as our field), 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.
Before we move on, let's clean up the Hello World example. Remove the hello field from the Query type, and the corresponding get_hello resolver function.
- def get_hello():- return "Hello world"@strawberry.typeclass 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 resolver, in this case the Playlist type is used by the featured_playlists resolver in our Query.
Open up api/schema.py, remove the types argument 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
Querytype serves as an entry point into the GraphQL schema. - You can use
@strawberry.fieldto define resolvers for fields on a type. - Strawberry automatically converts
snake_casefield names tocamelCasein 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 data source for our GraphQL server.
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.