[Note] This section is translated from: GraphQL-Quick Guide (tutorialspoint.com)
GraphQL is an open source server-side technology developed by Facebook to optimize RESTful API calls. It is an execution engine and a data query language. In this chapter, we will discuss the advantages of using GraphQL.
Why use GraphQL
The RESTful API follows a clear and well-structured resource-oriented approach. However, when the data becomes more complex, the route becomes longer. Sometimes data cannot be obtained with a single request. This is where GraphQL comes in handy. GraphQL constructs data in the form of graphs, and its powerful query syntax is used to traverse, retrieve, and modify data.
The following are the advantages of using GraphQL query language:
Ask what you want-and get it
Send GraphQL queries to your API and get exactly what you need. GraphQL queries always return predictable results. Applications that use GraphQL are stable. Unlike Restful services, these applications can limit the data that should be obtained from the server.
The following example will help you understand this better:
Let us consider a property the above mentioned id , firstName , lastName and CollegeName business objects Student . Suppose a mobile application only needs to obtain firstName and id . If we design a /api/v1/students , it will eventually get data for all fields Student This means that data is over-fetched by RESTful services. This problem can be solved by using GraphQL.
Consider the GraphQL query given below:
{
{
id
firstName
}
}
This will only return the values of the id and firstname fields. The query will not get the values of other attributes of the student object. The response to the query described above is as follows:
{
"data": {
"students": [
{
"id": "S1001",
"firstName": "Mohtashim"
},
{
"id": "S1002",
"firstName": "Kannan"
}
]
}
}
Get multiple resources in a single request
GraphQL queries help to retrieve related business objects smoothly, while a typical REST API needs to be loaded from multiple URLs. The GraphQL API gets all the data your application needs in a single request. Even on slow mobile network connections, applications using GraphQL can be fast.
Let us consider a business object College , which has the following attributes: name and location. Student The business object has an association relationship with the College If we use REST API to get the details of students and their universities, we will finally send two requests to the server, such as /api/v1/students and /api/v1/colleges . This will result in insufficient data acquisition for each request. Therefore, mobile applications are forced to call the server multiple times to obtain the required data.
However, mobile applications can use GraphQL to obtain detailed information about Student and College objects in a single request.
The following is the GraphQL query used to get the data:
{
students {
id
firstName
lastName
college {
name
location
}
}
}
The output of the above query contains exactly those fields we require, as shown below:
{
"data": {
"students": [
{
"id": "S1001",
"firstName": "Mohtashim",
"lastName": "Mohammad",
"college": {
"name": "CUSAT",
"location": "Kerala"
}
},
{
"id": "S1002",
"firstName": "Kannan",
"lastName": "Sudhakaran",
"college": {
"name": "AMU",
"location": "Uttar Pradesh"
}
},
{
"id": "S1003",
"firstName": "Kiran",
"lastName": "Panigrahi",
"college": {
"name": "AMU",
"location": "Uttar Pradesh"
}
}
]
}
}
Describe the possibilities of the type system
GraphQL is strongly typed, and queries are based on fields and their associated data types. If there is a type mismatch in the GraphQL query, the server application will return a clear and useful error message. This helps client applications to debug smoothly and easily detect errors. GraphQL also provides client libraries that can help reduce explicit data conversion and parsing.
Student and College data types are given below:
type Query {
students: [Student]
}
type Student {
id: ID!
firstName: String
lastName: String
fullName: String
college: College
}
type College {
id: ID!
name: String
location: String
rating: Float
students: [Student]
}
Respond faster with powerful development tools
GraphQL provides a wealth of development tools for documentation and test queries. GraphiQL is an excellent tool that can generate documentation for queries and their patterns. It also provides a query editor for testing GraphQL API and smart code completion when building queries.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。