What is GraphQL?
       GraphQL is a query language for APIs. It was developed by Facebook as a solution to fetch data more efficiently.

Why GraphQL:
     The Problems with REST In REST APIs, each endpoint will return a specific payload of JSON data. Even if we only need certain fields, it will return everything on every request. REST APIs have multiple endpoints, but GraphQL provides a single endpoint.

How GraphQL Works
      GraphQL fetches only the data you need using queries. With GraphQL, there is:

  1. No overfetching or underfetching
  2. Less endpoints to manage (i.e., only /graphql)
  3. Flexible integration with databases, REST APIs, cloud services, and JSON files.

Query
      Queries are entry points on a GraphQL server that provide read access to your data sources. GraphQL queries allow clients to dictate the fields they want and the response structure, similar to the GET method in REST APIs. For example:

query Query {

getStudentdetails {

    id

    name

  }

}

This query retrieves student details.

Mutation
      Mutations in GraphQL are used to modify data on the server. Equivalent to POST requests in REST, mutations enable clients to create, update, or delete data. While mutations are similar to queries in syntax, they are specifically designed for altering backend data. For example:

mutation Mutation($Studentname: String!, $phoneNumber: String!, $address: String) {

  Studentdetail(Studentname: “bharath”, phoneNumber: “1234567890”, address: “Tamilnadu”) {

    Id
    name
    phone_no

    address

  }

}

This mutation adds student details to the database.

Leave a Reply