liveBook · Manning

Query root type

As explained in Building Web APIs with ASP.NET Core

A query root type is the GraphQL operation type that exposes read operations for retrieving data.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines presence and statelessness, then demonstrates resolver methods and server registration for a basic implementation.
Examples
Uses Query.cs methods for BoardGame, Domain, and Mechanic collections with ApplicationDbContext injection.
What it assumes
Assumes a C# project with a data model and ApplicationDbContext, while defining GraphQL schema exposure through implementation.
What you can do after
Build it from scratch by creating Query.cs methods and registering AddQueryType<Query>() in the GraphQL server.
How it connects
Connects GraphQL schema with Mutation, Subscription, Entity Framework, paging, projections, filtering, and sorting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.