GraphQL is an opensource Query Language for APIs. It was Created by Facebook in 2012 for use in their Mobile applications. After, it was open sourced in 2015.
โGraphQL is a Query language for your API that shifts the contract between Clients and servers that allows the server to say these are the capabilities that I exposed and allows the clients to describe their requirements in a way that ultimately empower product developers to build the products they want create .โ โDan Schafer, GraphQL Co-Creator.
Query looks like:
{
students {
Name
Grade
}
}
The response for the query is given below โ
{
โdataโ: {
โstudentsโ: [
{
โNameโ: โJackโ,
โGradeโ: โAโ
},
{
โNameโ: โJohnโ,
โGradeโ: โA+โ
},
{
โNameโ: โNickโ,
โGradeโ: โBโ
}
]
}
}
GraphQL schema is core part of GraphQL server.All the queries are validated and executed against the schema. the schema describes the possible data that a client can access.
It is used for the write operations. It is used for operations like add, delete and edit data.
Enums are basically a special type we can use to enumerate all possible values in th field.By using enums we are adding another kind of validation to existing GraphQl schema.
SDL stands for Schema Definition Language. It is used for writing schemas.
A resolver is a function that returns data for a particular field. Resolver functions return data in the type and shape specified by the schema.
It is a required argument. It represents a GraphQL query as a UTF-8 string.
Facebook, GitHub, Pinterest, twitter, PayPal, Atlassian, Audi, sky, the New York times, shopify, yelp, and more.
Overfetching is extra data for an API request It increase payload size. Underfetching is the opposite of overfetching.It means that not enough data is included in an API response. it requires multiple API calls to fetch the complete data.
A client can request only the required data in GraphQL. It makes the payload size smaller than any other network request. In low network areas, small payload network requests can be executed faster.
Interospection allow you to ask GraphQl Schema for information about what queries it supports.
REST API:
- REST is an architectural pattern Concept for Network based Software.
- REST API easy to understand.
- Data fetch: A typical usage of REST API will fetch data by calling multiple API endpoints, and the server will return all data in those endpoints.
- The shape and size of the resource is determined by the server in REST
- In REST, you specify a write by changing the HTTP verb from GET to something else like POST.
- It uses different routes for requests.
- It is server-driven application state.
- REST only communicates on HTTP or HTTPS.
GraphQL:
GraphQL is a Query Language for APIs and set of tools that operate over a single endpoint using HTTP.
- GraphQL is a flexible and easy to backup.
- Data fetch: It uses queries, schema, and resolvers. Developers can specify the exact data thy need, moreover, they can create and execute nested queries.
- In GraphQL, the server declares what resources are available, and client asks for what it needs at the time.
- In GraphQL, you change a keyword in the query.
- In GraphQL, doesnโt have any Routes.
- It is Contract-driven by nature
- GraphQL server communicate over HTTP, HTTPs, TCP, WebSocket, UDP, FTP.