Comprehensive and in-depth explanation of GraphQL

Table of contents

1. What is GraphQL?

2. GraphQL specification

data model

Field

parameter

3. Running example

4. Advantages and Disadvantages

Advantage

Disadvantages


1. What is GraphQL?

GraphQL  is a query language for APIs and a server-based execution engine. GraphQL provides a complete set of specifications and descriptions for querying APIs. The server can accurately return the data required by the client without any redundant data. GraphQL is essentially an API query language, used for querying front-end and back-end data; developers can customize data models, query specifications and query parameters, and obtain all desired data in one request. This is unlike a RESTful request, which may require multiple requests to obtain the required data. Therefore, in GraphQL requests, developers are certain of the returned results.

2. GraphQL specification

data model

The data model is used to define the data structure of object entities and the relationships between objects, such as what attributes the objects have. The following figure represents a complete data model, which defines a query Query and two return objects Header and Query. These two objects The included attribute fields and data types are also defined separately, and these will be used in actual GraphQL requests.

Field

In GraphQL queries, the request results returned by the server to the client contain the expected structure, which is to accurately return the field result data of each request. The fields responded by the server are consistent with the fields requested. This is the most significant feature of GraphQL, and developers have expected it. As shown in the figure below, according to the example defined above, two fields are requested and the results of the two fields are returned. The reference tool used is: APOLLO

parameter

When querying data, you can pass parameters for query. For example, in the figure below, person uses the id parameter to query related attribute data. You can enter the actual id in the variable below for query.

3. Running example

Example 1: Output complete data results based on the data model

4. Advantages and Disadvantages

Understand some basic concepts and usage of GraphQL, and briefly analyze the advantages and disadvantages of using GraphQL as an API query language.

Advantage

  • GraphQL can query all data using only one data source, and all requests can access a service endpoint.
  • The result responses returned by GraphQL are accurately based on the client's request fields, reducing interference between the client and server.
  • GraphQL supports the definition and transfer of all data types, including parameter transfer in requests.
  • GraphQL now has many open source tool platforms as well as plugins and extensions.

Disadvantages

Guess you like

Origin blog.csdn.net/FENGQIYUNRAN/article/details/133499450