Hot Chocolatev14

Queries

The query type in GraphQL represents a read-only view of all of our entities and ways to retrieve them. A query type is required for every GraphQL server.

SDL
type Query {
books: [Book!]!
author(id: Int!): Author
}

Clients can query one or more fields through the query type.

GraphQL
query {
books {
title
author
}
author(id: 1) {
name
}
}

Queries are expected to be side-effect free and are therefore parallelized by the execution engine.

Usage

A query type can be defined like the following.

C#
public class Query
{
public Book GetBook()
{
return new Book { Title = "C# in depth", Author = "Jon Skeet" };
}
}
C#
builder.Services
.AddGraphQLServer()
.AddQueryType<Query>();
Warning

Only one query type can be registered using AddQueryType(). If we want to split up our query type into multiple classes, we can do so using type extensions.

Learn more about extending types

A query type is just a regular object type, so everything that applies to an object type also applies to the query type (this is true for all root types).

Learn more about object types