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 - Implement a resolver function for a "Hello world" example
- Register the
Query
type in our GraphQL server - Compare and contrast the annotation-based, schema-first, and code-first approaches
The Query
type
The Query
type defines a list of everything we're allowed to ask for from our GraphQL server. It's the entry point into our schema!
First, let's create a folder in the root of our project called Types
, where we'll organize all of our GraphQL schema types.
Then, let's add a class called Query
.
namespace Odyssey.MusicMatcher;public class Query{// where Query resolver functions will go}
Note: If you're using VS Code, the C# Dev Kit extension grants you access to a "Solution Explorer" section. Use this to quickly create folders and classes with the boilerplate code you see above.
Inside the Query
class, let's write the resolver function for a field called Hello
. It will be a public
function returning a string
type. Inside the function body, we'll return a hard-coded value of "Hello world"
.
public string Hello(){return "Hello world";}
This is our first resolver, a function that populates the data for a field in our schema. Remember, with the annotation-based approach, Hot Chocolate generates the GraphQL schema based on our resolvers. So right now, our schema looks something like this:
type Query {hello: String!}
Registering the Query
type
Our GraphQL server needs to know about the Query
type. Hopping into the Program.cs
file, find the line where we added the GraphQL server. We'll chain a new function called AddQueryType
and pass in the Query
type.
builder.Services.AddGraphQLServer().AddQueryType<Query>();
The Program.cs
file doesn't know where this Query
type is coming from, so we'll also need to import the namespace at the top of the file.
using Odyssey.MusicMatcher;
Note: This namespace was defined by default in the starter code you cloned. If you look at the top of the Query.cs
file, you can see that the Query
type is defined as part of the Odyssey.MusicMatcher
namespace. If you change this namespace to a different value, you'll also need to change the import in Program.cs
accordingly!
Let's not forget to save our changes and restart the server with dotnet run
.
Practice
Query
type in GraphQL?Key takeaways
- The fields of the
Query
type are entry points into our schema. These are the top-level fields that a GraphQL consumer can query for. - We can connect the
Query
file with our GraphQL server using the functionAddQueryType<Query>()
. - A resolver is a function that populates the data for a field in our schema.
Up next
Our GraphQL server is ready to receive queries. In the next lesson, we'll discover the best way to write and send queries: Apollo Explorer.
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.