GraphQL

GraphQL

GraphQL is the technology created by Facebook that enables the frontend to negotiate with the backend for the data it wants. GraphQL is a client-side query language coupled with a pattern — formally known as a “schema”. It allows for content negotiation which means you will only have one endpoint. It also allows you to query for exactly the fields you want. The limitations of the current REST APIs Multiple Endpoints : When we need to implement the RestAPI some times it needs multiple endpoints to consume the new features. Over fetching: When more data is delivered upon fetching than is required. In these cases, you’re unable to utilize all of it. Under fetching: When the data you get back is not enough. Often times, the data is so little that you have to run another query. These limitations are overcome with the help of GraphQL Setting up GraphQL in .Net Core We will need to do the following

  • Create a new solution
  • Create an ASP.Net Core Web Application with API
  • Install the GraphQL NuGet package

GraphQL consists of a schema definition, besides the schema, there is a central concept called resolvers. A resolver knows what to do with an incoming query. A resolver is mapped to a certain resource. To query a resource you would type : { resource } If you need to specify more fields you want under the resource like : { resource { columnOne columnTwo } } The end result is always a JSON response it matches your query. Example of a Query with a parameter is given below:

 

Result of the above query in GraphQL is on the right of the above picture

Benefits of GraphQL

GraphQL gives the following advantages to the Developer: #1: Easier for front-end teams The obvious benefit is that you will need less code to get the same information on the front-end. In addition to this, it’ll be much easier to understand for front-end developers as they will be able to take a look at the object and understand what it will return. GraphQL is that its queries mirror the response. This allows you to better predict the shape of the data that will be returned from a query. It also makes it easier for you to write a query if you already know what data your app needs. #2: Don’t have to worry about API versioning It’s important to note that the shape of the data that’s returned is determined by the client’s query. Due to this, servers become simpler and easier to generalize. As a result, when you add new product features, you’ll be able to add additional fields to the server without affecting existing clients.

GraphQL – Disadvantages

#1 File uploading : Since GraphQL doesn’t understand files, a file uploading feature is not included in its specification. #2 Performance issues with complex queries: GraphQL query can encounter performance issues if a client asks for too many nested fields at once. #3 Overkill for small applications: While GraphQL is the right solution for multiple Microservices, REST architecture is a better option for a simple application.