GraphQL is a query language and runtime for your API created by Facebook. GraphQL is a query string interpreted by a server that returns the result in a specified format. (GraphQL provides a common interface between client and server for fetching data from server or data manipulation). The client can ask for the required data from GraphQL server via queries. The response format is defined in query format and designed by the client instead of the server.

GraphQL is not language specific. Any client should be able to communicate with any server.

SAMPLE CODE:

Request:

{

     user(id: 120000) {

          id

          name

          profilePicture(size: 100){

                    uri

                    width

                    height

         }

         friends {

              id

              name

          }

     }

}

JSON Response:

{

     “data”: {

          “user”: {

               “id”: 120000,

               “name”: “Satish”,

               “profilePicture”: {

                    “uri”: “http://….jpg”,

                    “width”: 100,

                    “height”: 100

               },

              “friends”: [

                    {

                         “id”: 120001,

                         “name”: “Neha”

                    },

                    {

                         “id”: 120002,

                        “name”: “Madhura”

                    }

               ]

          }

     }

}

Concept of GraphQL:

  • Defines data shape: It is easy to predict the shape of the data returned from a query as well as write as a query if you know the data your app needs.
  • Strong typing: Every GraphQL server defines an application-specific type system. Queries are executed within the context of the type system. Given a query, tools can ensure that the query is both syntactically correct and valid within the GraphQL type system before execution.
  • Hierarchical: It is a natural way for the client to describe data requirements. GraphQL query itself is structured hierarchically. The query is shaped just like the data it returns.
  • Product centric: GraphQL is unapologetically driven by the requirements of views and front-end engineers that write them. GraphQL starts with their way of thinking and requirements and build language and runtime necessary to enable that.
  • Introspective: GraphQL server can be queried for the types it supports. This creates a powerful platform for tools and client software libraries. GraphQL helps developers learn and explore API’s quickly.