Skip to content

GraphQL sample using Hot Chocolate and ASP.NET Core minimal hosting

Notifications You must be signed in to change notification settings

khalidabuhakmeh/RidingTheGraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL and ASP.NET Core Minimal Hosting

This demo uses Entity Framework Core (In-Memory), Hot Chocolate, and Bogus to create a GraphQL demo that exposes your DbContext over an API.

It gives you the following:

  • Projections (i.e. select id from table)
  • Sorting (asc desc)
  • Filtering (where clauses)
  • Pagination (first 5 of query)

Getting Started

This is a self contained project, so restore the packages and run the project. I'm targeting .NET 6, so anything higher should work.

dotnet restore && dotnet run

GraphQL Query To Start

When the app starts, it will redirect you to Banana Cake Pop the GraphQL query interface for Hot Chocolate. Try playing around with the query below. It retrieves people and the building they are in.

{
  people(
      order: { id: ASC }, 
      where: { building : { id : { gt : 500 } } },
      first: 3
    ) 
    {
    totalCount,
    pageInfo {
      hasNextPage,
      hasPreviousPage,
      startCursor,
      endCursor
    },
    nodes {
      id,
      fullName,
      building {
        id,
        name
      }
    }    
  }
}

Notes

  • DbContextFactory is necessary to allow for future parallel queries.
  • IQueryable is important but not necessary
  • Attribute ordering on the Query is really important.
  • Changes to attributes can change the entire schema, so it's best to always refresh and check the schema if your query errors out.
  • Projections are important when working with Entity Framework, as you won't be able to load the related data without it, or you'd have to enable Lazy Loading (don't enable lazy loading).

About

GraphQL sample using Hot Chocolate and ASP.NET Core minimal hosting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages