What is Graphql


What is GraphQL?

Think of GraphQL as a language for your APIs. 

Normally, when you build an application that talks to a server, you request data from that server using an API (Application Programming Interface).

With traditional APIs/ REST APIs, the server decides what data to send you.

It's like going to a restaurant and ordering a fixed meal from the menu. You get what's predefined.

But with GraphQL, it's more like ordering a customizable meal.

You can specify exactly what data you need, down to the finest detail, and the server will send you just that.

So, instead of getting a fixed meal, you get exactly the ingredients you asked for.

This flexibility can make your app more efficient because you're only getting the data you need, nothing more, nothing less.


Structure of GraphQL :-

GraphQL has a simple and intuitive structure.

  1. Schema Definition Language (SDL) :- GraphQL schemas are defined using a special syntax called the Schema Definition Language (SDL). This is where you define the types of data that your API can return and specify how clients can interact with those types.
    1. Defines a query type with a single field hello
    2. type Query {
          hello: String!
      }
      
      # JSON response
      {
        "data": {
          "hello": "Hello, world!"
        }
      }
      
  2. Types :- In GraphQL, you define types to represent the different kinds of data that your API can return. These types can be objects, scalars, enums, interfaces, or unions.
    1. Defines a User type with id and name fields. ! means can't be empty, required field
    2. type User {
          id: ID!
          name: String!
      }
      
      # JSON response
      {
        "id": "123",
        "name": "John Doe"
      }
      
  3. Queries :- Queries are used by clients/frontend to request specific data from the GraphQL/Backend server. Queries defines the shape of the data they expect to receive and can be deeply nested to retrieve related data in a single request.
    1. Queries the user field for id and name
    2. query {
          user {
              id
              name
          }
      }
      # JSON response
      {
        "data": {
          "user": {
            "id": "123",
            "name": "John Doe"
          }
        }
      }
      
  4. Mutations :- Mutations are operations used to modify data on the server. like create, update, delete. They are similar to queries in structure but are used for actions that cause side-effects, such as creating, updating, or deleting data.
    1. Mutation to create a user with name.  after creation returns, name and id
    2. mutation {
          createUser(name: "Alice") {
              id
              name
          }
      }
      # JSON response
      
      {
        "data": {
          "createUser": {
            "id": "456",
            "name": "Alice"
          }
        }
      }
      
  5. Subscriptions :- Subscriptions allow clients to subscribe to real-time data updates from the server. This is useful for applications that require live updates, such as chat applications or real-time dashboards.
    1.  Subscribes to new messages for id and content
    2. subscription {
          newMessage {
              id
              content
          }
      }
      # JSON response (real-time)
      {
        "data": {
          "newMessage": {
            "id": "789",
            "content": "Hello, world!"
          }
        }
      }
      
  6. Resolvers :- Resolvers are functions responsible for fetching the data for a particular field in a GraphQL query or mutation. Each field in a schema must have a corresponding resolver function that returns the data for that field.