Why GraphQL? Six problems

GraphQL is an API standard: Specification.

With each new technology, here are six things to know:

  • 1. The background and original intention of the emergence of this technology, and what kind of goals or problems to be solved.
  • 2. What are the advantages and disadvantages of this technology, or what are the trade-offs of this technology?
  • 3. Scenarios in which this technology is used.
  • 4. Components and key points of technology.
  • 5. Underlying principles and key implementation of the technology.
  • 6. Existing implementations and comparisons.

1. The background and original intention of the emergence of this technology, and what kind of goals or problems to be solved.

GraphQL is another standard to REST apis.

In contrast to REST, which returns multiple endpoints of fixed structure data, GraphQL’s server exposes only one endpoint. The client defines the desired fields and structure, and the server returns exactly the data required by the client.

Original Intention and Background

In 2012, Facebook had an iOS app for news. With the increase of mobile users, there are some problems such as device power consumption and weak network environment. This was a very pressing issue at the time, so they developed GraphQL to reduce the number of requests to send and the amount of data transferred, and in 2015 Facebook made GraphQL open source.

Similar attempts were made by Netflix’s Falcor and Coursera, which later canceled its effort to join GraphQL.

Goals and problems to be solved

GraphQL Idea: Enables Declaratative data fetching.

To optimize the process of clients requesting data from the server:

  • While REST apis might request multiple Endpoints for the same business requirement, GraphQL can use a single request.
  • The data format is defined by the client and only the desired data is retrieved, reducing the amount of data transferred.
  • Each front-end can access the data it wants.
  • Rapid development and rapid iteration of feature. When upgrading changes, back-end changes may not be required.

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

The advantage of GraphQL

  • The client can accurately fetch the data it wants, no longer fetching more or less. Multiple fetch: Unwanted data may cause performance or traffic problems. Less fetching: Multiple requests are required to meet requirements.
  • API has strong typed Schema. Clients can know what the API supports through schema, including operations, parameters, possible responses, etc. Introspection. schema is an API capability contract that requires no additional documentation. Once defined, the front and back ends can work independently.
  • Strongly typed, using schema, combined with the build tool, allows some checking of requests at compile time.
  • Facilitate rapid product upgrade and iteration. Front-end changes can be made without back-end changes.
  • Insightful data analysis: Because the client can know precisely what data to read, it can have a deeper understanding of the data usage. Deprecate unused data when API changes are made. It also helps with back-end performance analysis.
  • Schema kevlar: Multiple endpoints for different GraphQL can be combined into one.
  • Community support good. Multilingual: graphql.org/code/#serve… ; Multiple clients: medium.com/open-graphq… ; Various tools: Prisma, GraphQL Faker, GraphQL Playground, GraphQL-config.

The disadvantage of GraphQL

  • It may not be suitable for all types of apis, such as authentication and authorization.
  • Server side performance issues.
  • Caching on the server. A global ID is required. For details, see Caching.
  • With the rapid development of communication, the amount of data transfer saved by implementing GraphQL may not be worth mentioning and the advantages may not be obvious.

3. Scenarios in which this technology is used.

GraphQL is a specification with multiple implementations. It is independent of the transport layer, database, and data source type.

GraphQL application Scenarios:

  • On top of the database.
  • Integrate with existing systems. It can be used to retrofit legacy systems, unify interfaces, and hide implementations.
  • Mix the first two, on top of existing systems and databases.

Ideal development scenario: After the schema is established according to the data, the front and back ends are independently developed, and the front end can get the desired data according to the needs.

4. Components and key points of technology.

Schema

Schema defines the capabilities of the API and is the protocol between the server and client. Specifies the data and types that clients can request.

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

Root Types: Query, Mutation, Subscription. Correspond to query, change, and subscribe.

The Server side

The GraphQL server exposes only one endpoint.

In terms of API design, you need to think of data in terms of Graph rather than endpoints, and focus more on describing data.

For the structure defined by Schema, the Server implements resolver function for each field to query.

Server library: graphql.org/code/#serve…

The Client side

Client-side queries, more autonomous structure, field-level granularity.

The Client library: graphql.org/code/#graph…

Like the Server library, these libraries encapsulate some boilerplate code for us to simplify and facilitate our development.

5. Underlying principles and key implementation of the technology.

Server side implementation

  • -> Structure.
  • Resolver function -> Behaviour

All queries/mutations contain a set of fields. On the server, each field has a resolver function that reads the corresponding field’s data.

The query is executing a traversed field by field, executing a “resolvers” for each field. Breadth first. Proceed by hierarchy.

To improve efficiency, JavaScript has dataloader, which batches calls to resolver to reduce repeated calls.

The Client side implementation

The client actually sends a POST request, and the GraphQL query is the field of the JSON payload.

Using the curl command, you can obtain the same result: graphql.org/graphql-js/…

An Introspection Query may be the only GET request.

6. Existing implementations and comparisons.

REST: Stateless Servers, Structured Access to resources.

The downside of REST: Rapidly changing client requirements can be incompatible with the static nature of REST.

REST:

  • Different service data may require the client to send multiple requests to the server.
  • The request may contain redundant data. The old client will also receive the new data.
  • Weak type.
  • Error returns a different status code.
  • API upgrades require different version numbers.

GraphQL is more flexible and efficient.

  • Single request.
  • Contains no redundant data.
  • Strongly typed.
  • The error return is the “errors” field in the response, which contains a list of errors, each with a “message” field.
  • API upgrades do not require a version number. New data and types do not affect previous queries.

REST and GraphQL can coexist.

reference

  • GraphQL website
  • GraphQL Introduction
  • The Fullstack Tutorial for GraphQL
  • Top 5 Reasons to Use GraphQL
  • graphql concepts