Building and consuming GraphQL in your .NET application

GraphQL is a single API face for different clients like mobile, desktop apps, tablets… It’s built by Facebook.

It’s not a magical library that solves all our Web API issues but it helps us to provide our clients a very flexible Web API.

I wish we would provide GraphQL the EF DbContext and it would solve everything. But things are not so easy!

In this document we will see what’s GraphQL, what it solves, how to build a simple GraphQL back-end for our ASP.NET Core project and also how to consume this GraphQL.

Before starting, notice that you can find the demonstrated project on https://github.com/ebicoglu/AspNetCoreGraphQL-MyHotel

img

GraphQL & Client transmission

img

GraphQL & Clients

img

Rest API & GraphQL pipelines

Note that GraphQL doesn’t need HTTP to work. HTTP is a transporting way to send data to the API. Each request will have unique URLs that’s why HTTP caching is not possible!

img

img

GraphQL Language Basics

You can query popular APIs using GraphQL in your browser from GraphQL Playground

So I’ll play with GitHub’s GraphQL schema. The below query gets the user with the username “ebicoglu” and including repositories of the user. And we name this query “TestQuery”.

query TestQuery {
  graphQLHub
  github {
    user(username: "ebicoglu") {
      id
      login
      company
      avatar_url
      repos {
        name
      }
    }
  }
}

![img](https://miro.medium.com/max/3