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.

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

Featured playlists

Similar to how we wrote a function for the Hello , we'll do the same thing for a field called FeaturedPlaylists. We recommend collaborating with your consumers on naming decisions!

This will return a list of Playlist types.

Query.cs
public List<Playlist> FeaturedPlaylists()
{
}

Let's write some hard-coded mock data for now. We'll initialize a list of Playlist types and initialize three Playlist instances, passing in IDs and names.

Query.cs
return new List<Playlist>
{
new Playlist("1", "GraphQL Groovin'"),
new Playlist("2", "Graph Explorer Jams"),
new Playlist("3", "Interpretive GraphQL Dance")
};

Lastly, don't forget the description!

[GraphQLDescription("Playlists hand-picked to be featured to all users.")]

Hit save, restart the server, 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.

https://studio.apollographql.com/sandbox/schema

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.

https://studio.apollographql.com/sandbox/schema

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.

https://studio.apollographql.com/sandbox/explorer

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! 🕺💃

https://studio.apollographql.com/sandbox/explorer

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.

https://studio.apollographql.com/sandbox/explorer

Response data table view

Before we move on, let's clean up the Hello World example and remove the Hello function from the Query file.

Query.cs
class Query
{
- public string Hello()
- {
- return "Hello world";
- }
// public List<Playlist> FeaturedPlaylists()
}

Key takeaways

  • The Query type serves as an entry point into the .

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.