Why we would need to use GraphQL for our API s …

Wathsala Rasanjalee Ganewatta
5 min readMay 31, 2021
GraphQL

Before coming to GraphQL most of the developers use REST for building backend Services. With REST, if you want to make a request with different functionality like POST PUT GET or DELETE you can use one API for different purposes but for the same method cannot use.

GraphQL as API gateway
REST and GraphQL

Also, REST has some limitations.

  • Client-side need to know about the all the location of each service → Example: There must have two endpoints with two different URLs http://localhost:3000/customers and http://localhost:3001/vehicle if client-side doesn't know that locations to get customer details and vehicle details, then client-side cannot access that.
  • Over fetching → Fetch too much data. In the response, there are so much unnecessary data for you. If that often changes that go with more cost when maintaining that like endpoints.
  • For multiple resources must use multiple requests → If there are multiple resources to handle then we have to use multiple requests for all resources. that also gets more cost when maintaining that like systems.

GraphQL is a query language for APIs. It stands for Graph Query Language. It has free plans.GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data.

The general movement around GraphQL is very encouraging for anyone looking to adopt it. It has been increased its popularity in the last few years.

GraphQL requires minimal effort to have a well-documented API when using GraphQL. This provides great transparency to developers who are working with an API for the first time and makes development smoother and more efficient.

Using GraphQL, from a single query call can handle multiple resource requests. That saves time and bandwidth by reducing the number of network round trips to the server. It also helps to prevent waterfall network requests, where you need to resolve dependent resources on previous requests.

GraphQL solves the problem of over-fetching in REST by fetching only the exact and specific data in a single request. It also enables clients to request their own unique data specifications.

GraphQL can speed up development and automation in comparison to REST.

What is GraphQL playground?

GraphQL Playground is a graphical, interactive, in-browser GraphQL IDE, created by Prisma and based on GraphiQL. In development, Apollo Server enables GraphQL Playground on the same URL as the GraphQL server itself (e.g. http://localhost:4000 ) and automatically serves the GUI to web browsers.

query in playground
mutation in playground

Query

GraphQL queries themselves are not faster than REST queries, but because you can pick the fields you want to query, GraphQL requests will always be smaller and more efficient.

GraphQL is neither the frontend nor backend but rather the language spoken between the two to exchange information.

With GraphQL, as each client specifies exactly the information that they are interested in, we can understand what data is being used and what is not being used. Also, we can measure performance for each attribute requested and this provides crucial insights about the performance of your API.

If anyone has JavaScript knowledge and has built REST API then should be easy to work with GraphQL. And should have the knowledge to work with node to work with GraphQL.

GraphQL services are typically responding using JSON format. JSON is more familiar to frontend and API developers. Because that can easy to read and debug.

A GraphQL service is created by defining types and fields. For example, a GraphQL service that tells you who are the employees in the system(employees), as well as that employee’s names, employee ids, designations, might look like this:

type Query{
employees:[Employee]
}
type Employee{
id:ID
name:String
designation:String
}

Getting the result of this query is very easy.

{
employees{
name
designation
}
}

The result looks like this,

}
"data":{
"employees":[{
"name":"John Abran",
"designation":"Software Engineer"
},
"name":"sali Khan",
"designation":"Associate Software Engineer"
},
"name":"sara Ali",
"designation":"Associate Software Engineer"
},
"name":"karina Abran",
"designation":"Software Engineer"
},
]
}

For fetching, data queries can have arguments also

For Example:

//Querytype Query{
employeeBYId(id:ID):Employee
}
// type of Employee
type Employee{
id:ID
name:String
designation:String
tier:Number
}
// fetching Query example
{
employeeById(id:"1000")
{
id
name
}
}
// result of this query look like this{
"data":{
"employeeById:{
"id":"1000",
"name":"Jone Kane"
}
}
}

GraphQL Aliases are written on the client making the request. This is a workaround for use cases where we want the selection set to be named more readable. It is not wise to write aliases for every query from every client making this request for modifying trivial fields.

Fragments in GraphQL is a logic. fragments can share between multiple mutations and queries. Every fragment has a subset of the field that belongs to its associate type. Fragment access with three dots( ... )

Example:

fragment name on Employee {
firstName
lastName
}

//can query it like this
{
employeeById(id:"1000"){
...name
}

Mutation

GraphQL uses mutations if any change happens to the server-side. The mutation also looks like the query. Mutations represent the state-changing methods in REST (like DELETE, PUT, PATCH, etc).

Example: For create requests.

type Mutation {setMessage(message: String): String}

TypeScript will become the dominant language for writing GraphQL APIs with Node. JS. With its strongly-typed schema, GraphQL is best used in combination with typed languages.

Who uses GraphQL?

  • Facebook
  • Instagram
  • Twitter
  • etc.

GraphQL has more features. If you knowledge of typeScript or javaScript try to learn this. This would be a great experience in your career.

--

--