Why GraphQL? Six questions


Why GraphQL? Six questions

Graphql is an API standardspecification.

For each new technology, there are six issues to be clarified:

  • 1. The background of this technology, the original intention, what kind of goal to achieve or what kind of problem to solve
  • 2. What are the advantages and disadvantages of this technology, or what is the trade-off of this technology
  • 3. The scene of using this technology
  • 4. Components and key points of technology
  • 5. The underlying principle and key implementation of the technology
  • 6. The existing implementation and the comparison between them

1. The background of this technology, the original intention, what kind of goal to achieve or what kind of problem to solve

Graphql is another standard relative to rest API

Compared with multiple endpoints that rest returns fixed structure data, the server of graphql only exposes one endpoint. The client specifies the desired field and structure, and the server precisely returns the data required by the client

Original intention and background

In 2012, Facebook had a news IOS app. With the increase of mobile users, network requests have problems such as power consumption of devices, weak network environment and so on
This was a very urgent issue at that time, so they developed graphql to reduce the number of requests and data transmission
In 2015, Facebook will open up graphql

There’s also NetflixFalcorAnd coursera, who later canceled their own effort and joined graphql

Objectives and problems to be solved

Graphql idea: enables declarative data fetching

The problem to be solved by graphql is to optimize the process of data request from client to server

  • For the same business requirements, rest API may request multiple endpoints, and graphql can use one request
  • The data form is defined by the client. It only takes the desired data, not the redundant data, which reduces the amount of data transmission
  • Each front end can access the data they want
  • Fast development, feature fast iteration. Upgrade changes, may not need back-end changes

2. What are the advantages and disadvantages of this technology, or what is the trade-off of this technology

Advantages of graphql

  • The client can accurately get the data they want, no more or less; Take less: multiple requests are required to meet the demand
  • The API has a strong typed schema. Through the schema, the client can know what the API supports, including operations, parameters, possible responses, etc. it supports introspection. Schema is an API capable contract, and there is no need to write additional documents. Once defined, the front and back end can work independently
  • Strong type, schema and build tool can be used to check the request during compilation
  • It is helpful to upgrade the product quickly and iteratively
  • Insightful data analysis: because you can know the data to be read by the client in detail, you can have a deeper understanding of the usage of the data. When the API changes, you can decode the unused data. It is also helpful to the back-end performance analysis
  • Schema stitching: multiple different endpoints of graphql can be combined into one
  • Good community supporthttps://graphql.org/code/#server-libraries;Multiple clients:https://medium.com/open-graphql/exploring-different-graphql-clients-d1bc69de305f;Multiple tools: prism, graphql faker, graphql playground, graphql config

Disadvantages of graphql

  • It may not be suitable for all types of API, such as authentication and authorization
  • Server side performance issues
  • The cache on the server side needs a global ID. for discussion, see:Caching.
  • With the rapid development of communication, the amount of data transmission saved by graphql may not be worth mentioning

3. The scene of using this technology

Graphql is a kind of specification, which can be implemented in many ways
And transport layer, database, data source type are not related

Graphql application scenarios:

  • On top of the database
  • It can be used to transform legacy system, unify interface and hide implementation
  • Mix the first two, on top of the existing system and database

Ideal development scenario: according to the data, after the schema is established, the front end and the back end can develop independently, and the front end can get the desired data according to the needs

4. Components and key points of technology


Schema defines the ability of API, which is the protocol between server and client. It specifies the data and type that client can request

The Schema Definition Language (SDL): Schema Definition Language.

Root Types: Query, Mutation, Subscription.
Corresponding query, change and subscription

Server side

The graphql server only exposes one endpoint

In the design of API, we need to think of data according to graph, not according to endpoints

For the structure defined in schema, the server implements resolver function for each field to query

Server Library:https://graphql.org/code/#server-libraries

Client side

Client query, more independent structure, field level granularity

Client Library:https://graphql.org/code/#graphql-clients

Like the server library, these libraries encapsulate some sample code for us, simplify and facilitate our development

5. The underlying principle and key implementation of the technology

Server implementation

  • The definition of schema
  • Concrete implementation of resolver function form. – > behavior

Queries / mutations contain a set of fields
On the server, each field has a resolver function to read the data of the corresponding field

The execution mechanism on the server is as follows:
The query is traversed field by field, executing “resolvers” for each field.
Breadth first. By level

To improve efficiency, JavaScript hasdataloader, batch resolver calls to reduce repeated calls

Client implementation

The client actually sends the post request, and the query of graphql is the field of JSON payload

The same effect can be obtained by using curl command

Introspection query may be the only get request

6. The existing implementation and the comparison between them

A good idea for rest: state servers, structured access to resources

Disadvantages of rest: rapidly changing client requirements may not match the static properties of rest


  • Different business data may require the client to send multiple requests to the server
  • The request may contain redundant data. Old client will also receive new data
  • Weak type
  • Error returns different status codes
  • API upgrade requires different version numbers

Graphql is more flexible and efficient

  • Single request
  • Does not contain redundant data
  • Strong type
  • Error return is the “errors” field in the response, including error list, and each error has a “message” field
  • API upgrade does not need version number. New data and types will not affect previous queries

Rest and graphql can coexist

reference resources

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]