Skip to content

LiteObject/Demo.GraphQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo GraphQL with C#

GraphQL is a query language for API, and a server-side runtime for executing queries using a type system define for data. GraphQL isn't tied to any specific database or storage engine and is instead backed by existing code and data.

GraphQL logo

GraphQL is a new API standard that provides a more efficient, powerful, and flexible alternative to REST. It was developed and open sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world.

Example

At its core, GraphQL enables declarative data fetching where a client can specify exactly what data it needs from an API.

Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data that a client asked for.

Tools

  • Prisma creates tools and that replaces traditional ORMs. Prisma enriches the GraphQL ecosystem and community by creating high-quality tools and software.
  • Hasura is an open source engine that connects to your databases & microservices and auto-generates a production-ready GraphQL backend. Hasura gives you realtime GraphQL APIs that are high-performance, scalable, extensible & secure (with authorization baked in).

GraphQL vs REST API

Over-Fetching and Under-Fetching

api/users/{id}
api/users/{id}/posts
api/users/{id}/followers

There are two libraries to expose GraphQL endpoint:

  • 🏆graphql-dotnet/graphql-dotnet
  • ChilliCream/hotchocolate
    • STEP #1: dotnet add package HotChocolate.AspNetCore
    • STEP #2: Define the types
    • STEP #3: Add a Query type
    • STEP #4: Add GraphQL services
      builder.Services
         .AddGraphQLServer()
         .AddQueryType<Query>();
    • STEP #5: Map the GraphQL endpoint
      var app = builder.Build();
      // map GraphQL
      app.MapGraphQL();
      app.Run();

Operations: GraphQL vs REST

Operation GraphQL REST
Read Query GET
Write Mutation PUT, POST, PATCH, DELETE
Event Subscription N/A

Links:

Releases

No releases published

Packages

No packages published

Languages