4. Hello world
5m

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 function for a "Hello world" example
  • Register the Query type in our
  • 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 . 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 types.

Then, let's add a class called Query.

Types/Query.cs
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 function for a 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".

Types/Query.cs
public string Hello()
{
return "Hello world";
}

This is our first resolver, a function that populates the data for a in our schema. Remember, with the annotation-based approach, Hot Chocolate generates the based on our . So right now, our schema looks something like this:

Generated GraphQL schema
type Query {
hello: String!
}

Registering the Query type

Our needs to know about the Query type. Hopping into the Program.cs file, find the line where we added the . We'll chain a new function called AddQueryType and pass in the Query type.

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

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

What is the purpose of the Query type in GraphQL?
What is a resolver in GraphQL?
How is the Query type registered in the GraphQL server in the annotation-based approach?

Key takeaways

  • The of the Query type are entry points into our schema. These are the top-level that a consumer can for.
  • We can connect the Query file with our using the function AddQueryType<Query>().
  • A is a function that populates the data for a in our schema.

Up next

Our is ready to receive queries. In the next lesson, we'll discover the best way to write and send queries: Apollo Explorer.

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.