A query root type is the required GraphQL operation type for read operations. It centralizes data-retrieval fields, with each public method exposing a way for clients to retrieve an entity. Query root types are stateless; Mutation and Subscription are optional operation types. section 10.2.3
For the board-game API, create a public Query class under /GraphQL/. Add one method per exposed entity, inject ApplicationDbContext as a service, and return the corresponding IQueryable collection. section 10.2.3
The query fields follow a consistent implementation pattern:
- GetBoardGames returns context.BoardGames as IQueryable<BoardGame>. section 10.2.3
- GetDomains and GetMechanics expose their matching data-model collections. section 10.2.3
- Use paging, projection, filtering, and sorting attributes to add client-query capabilities to each field. section 10.2.3
Register the query root
Register the class with AddGraphQLServer().AddQueryType<Query>(). Add projections, filtering, and sorting services when the query methods use those attributes. section 10.2.3
Listing 10.2 /GraphQL/Query.cs file
using MyBGList.Models;
namespace MyBGList.GraphQL
{
public class Query
{
[Serial]
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<BoardGame> GetBoardGames( #1
[Service] ApplicationDbContext context)
=> context.BoardGames;
[Serial]
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Domain> GetDomains( #2
[Service] ApplicationDbContext context)
=> context.Domains;
[Serial]
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Mechanic> GetMechanics( #3
[Service] ApplicationDbContext context)
=> context.Mechanics;
}
}
Use the complete Query.cs listing to inspect the entity methods and their paging, projection, filtering, and sorting attributes. section 10.2.3
