liveBook · Manning

Schema

As explained in Building Web APIs with ASP.NET Core

A GraphQL schema is a typed definition of the data and operations that a GraphQL API exposes to clients.

A GraphQL schema defines the data an API exposes to clients and the operations clients may perform. In a board-game API, it provides a way to make existing data and CRUD functionality available through GraphQL. section 10.2.3

The schema is organized around root operation types:

  • Query exposes ways to retrieve data. It is required. section 10.2.3
  • Mutation exposes insert, update, and delete operations. It is optional. section 10.2.3
  • Subscription lets clients receive notifications about events. It is optional and, unlike Query and Mutation, must retain state for each active subscription. section 10.2.3

For a straightforward CRUD API, start with Query and Mutation; a Subscription is not needed. A Query class can provide methods that return board games, domains, and mechanics from ApplicationDbContext. The example query methods also carry paging, projection, filtering, and sorting 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;
    }
}

The Query class shows how retrieval methods and their attributes form the read side of the schema. section 10.2.3

Mutation methods handle writes; the board-game, domain, and mechanic examples include authorization attributes on update and delete methods. Register the Query and Mutation types with HotChocolate’s GraphQL server, add the supporting authorization and data features, then map the GraphQL endpoint. section 10.2.3

Listing 10.6 Program.cs file: GraphQL services
using MyBGList.GraphQL;                #1
 
// [...]
 
builder.Services.AddGraphQLServer()    #2
    .AddAuthorization()                #3
    .AddQueryType<Query>()             #4
    .AddMutationType<Mutation>()       #5
    .AddProjections()                  #6
    .AddFiltering()                    #7
    .AddSorting();                     #8

The service registration shows where the two root types and their supporting features are added. section 10.2.3

Do not confuse this API schema with a database schema. The database schema describes stored structures such as tables, fields, relationships, and constraints; EF Core migrations can generate and update it from a C# data model. The GraphQL schema instead describes what clients can access and do through the API. section 4.0 section 10.2.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines root operation types and implements Query and Mutation classes and registration, while deferring Subscription implementation.
Examples
Uses board-game, domain, and mechanic retrieval methods, authorized mutations, and GraphQL server registration.
What it assumes
Assumes an ASP.NET Core project with an EF Core context and models, while defining the GraphQL root types.
What you can do after
Build it from scratch with HotChocolate Query and Mutation types and map a GraphQL endpoint.
How it connects
Connects Query, Mutation, Subscription, CRUD, authorization, and Entity Framework Core.

Also explained in

Compare how 44 books explain schema →

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.