GraphQL

Thiwanka Nuwan
JRC Tech Drive
Published in
3 min readOct 22, 2019

GraphQL is developed by Facebook for optimized Rest API calls. When Facebook launched its mobile application, engineers noticed the application had slowness issues. They found the application generated too many network requests to the server. So they had to provide a solution for it. It’s was GraphQ, started in 2012 as an internal project and now it’s an open-source project.

What is GraphQL?

GraphQL is a server-side Query Language and Execution Engine. It can create API endpoints for clients. The client can see the API endpoint schema and they can write custom queries as required and then get back exact data required.

Example for Rest and GraphQL

Rest API:

GET Request Markup: http://localhost:8000/book/{Id}

Request: http://localhost:8000/books/1

GraphQL API

GET Request: http://localhost:8000/graphql?query={book(id: “1”) { title, author { lastName }}}

It allows limiting response data using GraphQL.

Why we need it?

GraphQL can minimize the number of API endpoints in a project. Further, it is used to optimize application performance by reducing the number of network calls.

If you have an existing web API and you need to implement a mobile application, in this case, you are using rest API and you have to rewrite the API method for mobile application. But, if GraphQL is used, then it is not required to rewrite the API method. There, the existing web API method for mobile applications can be used.

Example for Rest and GraphQL

How to Implement GraphQL in Server Side using .net

Implementing GraphQL in server-side using .Net can be done following the below steps.

1. Create .NET core project

2. Create a Query request model.

PM> Install-Package GraphQL

PM> Install-Package graphiql

3. Create GraphQL controller

RealEstateSchema.cs

PropertyType.cs

PropertyQuery.cs

Note: In this example, data is hardcoded. But, it is also possible to retrieve data from a database.

StartUp.cs

4. Once the setup is completed following the above-mentioned steps, you can run the GraphQL example.

http://localhost:8000/graphql/

--

--