Why GraphQL is Better Than Rest & Its advantages over rest



Why GraphQl is Better Than Rest

GraphQL is the new standard for API and developed by Facebook as open-sourced. GrapgQL provides a more flexible, efficient, and more powerful API. it reduces network requests for that we need it in a single query. if we want in a single query then GraphQL is the better than REST.

For every client-server request then we need REST because each endpoint in a REST API has a fixed data structure. for example, if we request the employee name then we need to request the employee name by providing id of the employee but the return value is the entire row of the employee. here we are not getting the exact value of employee name. So the main problem in the above scenario is getting unnecessary data along with necessary data.

                              "Why GraphQl is Better Than Rest "

Uses of GraphQL:

  1. Architecture is client-driven not at server-driven like REST.
  2. performs the Operations at GraphQL as Querry mutation subscription.
  3. performs the Operations at REST as Create, Read, Update, and delete.
  4. The development speed is high and fast loading on the webpage.
  5. Only the required data will be present on the web.
  6. Less number of network requests.
  7. Very good for microservices and complex systems
  8. GraphQl has its own type system, these type system used to define the schema of as api

Uses of REST API:

  1. It is a stateless server and structured access to a resource.
  2. REST is the strict specification.
  3. REST has no its own type system like GraphQL.

How Data flow at GraphQL and Rest API


Rest API Request Flow:


Below is the image for REST API Data flow from the client to the server and vice versa. here we have 3 endpoints to gather the data from the server


Below is the JSON script seems like the as above image

{
  "emp": {
      "id":"108108"
      "name":"AllTechGeeks",
       "dept":"dotnet"
  }
}

{
  "empPosts": {
      "id":"109109"
      "post-title":"GraphQL From Beginners to Experience Step by Step Tutorial",
       "Description":"Graph QL is the alternate to rest api"
  }
}

{
  "emp": {
      "id":"108108"
      "deptid":"2",
       "description":"asp.net tutorials"
  }
}



GraphQL Request Flow:


Here 3 endpoints at a time we are sending and we get as per request-response.


Below is the JSON script seems like the as above image



query{
  emp(id"1981098): {
      name
      empPosts(last10): {
        post-title
      }
      deptid

  }
}


Wha is Schema Definition Language (SDL)?

GraphQL has its own type system, these type systems used to define the schema of an API. so we can call the writing the syntax for any type as Schema Definition Language (SDL).

Simple SDL Syntax for employee


type Employee {
      name: String!
       deptid: Int!
}


This type has 2 fields, they’re called name and deptid and are respectively of type String and Int. The "!"  following the type means that this field is required.

So far we have seen request queries. Before going to Learn the in-depth concept of GraphQL, you should know the below 3 main concepts.

      1. type Query {..} syntax
      2. type Mutation{..} syntax
      3. type Subscription{..} syntax
1.0 type Query syntax

{
type Employee {
      name
       deptid
}
}

1.1 type Query syntax with Arguments

{
type Employee (last: 2) {
      name
       deptid
}
}

2. type Subscription syntax

subscription{
newEmployee {
      name
       age
}
}

3. type Mutation syntax

 Mutations create and modify objects. it is used to create, delete and update the data and similar to put, post, and delete the request in REST. Mutation query always start with mutation keyword

mutation{
createEmployee (name: "Murali", age: "27") {
      name
       age
}
}







Previous Post Next Post