Translation: Crazy geek

Original text: opensource.com/article/19/…

Reproduced without permission

GraphQL is a query language, an execution engine, and a specification that leads developers to rethink how they should build clients and apis.

GraphQL is one of the biggest buzzwords in software technology today. But what is it? Is it a query language like SQL? An execution engine like the JVM? A specification like XML?

Only if you answer all of the above questions is the correct answer! GraphQL is a query language syntax, programming language-independent execution engine, and an evolving specification.

Let’s take a closer look at how GraphQL came to be and why people are excited about it.

Query language (SQL)

GraphQL as a query language seems reasonable — it contains the word “QL”, after all. But what are we looking for? The following examples of query requests and corresponding responses may help you understand.

The following user queries:

{
 user(id: 4) {
   name
   email
   phoneNumber
 }
}
Copy the code

The following JSON response might be returned:

{
 "user": {
   "name": "Zach Lendon""Email" : "[email protected]" phoneNumber ":" "867- 5309."}}Copy the code

Suppose the client program queries the user’s details, then retrieves the results and displays them on the screen. One of the core strengths of GraphQL as a query language is that client programs can only request the data they need and return it in a consistent form.

But what is the return GraphQL response? This is where the execution engine (usually in the form of a GraphQL server) comes in.

Execution engine

The GraphQL execution engine is responsible for processing the GraphQL query and returning a JSON response. All GraphQL servers are made up of two core components: the schema and the parser, which define the structure and behavior of the execution engine, respectively.

GraphQL Schema is a custom typing language that exposes what queries are allowed (valid) and processed by the GraphQL server implementation. The user query case above can be structured as follows:

type User {
        name: String
        email: String
        phoneNumber: String
}

type Query {
        user: User
}
Copy the code

These schemas define queries that return user data. The client can request all the fields of the User through the User query, and the GraphQL server returns only those fields in the response. By using a strongly typed schema, the GraphQL server can validate incoming queries to ensure that they are based on the defined schema.

Once the query is determined to be valid, it is handed over to the GraphQL server’s parser for processing. The parser function supports all fields of each GraphQL type. The parser for the user query looks like this:

Query: {
  user(obj, args, context, info) {
    return context.db.loadUserById(args.id).then(
      userData => new User(userData)
    )
  }
}
Copy the code

While the above example is written in JavaScript, the GraphQL server can be written in any language. This is because GraphQL itself is a specification!

specification

The GraphQL specification defines the functions and features that you must follow to implement GraphQL. As an Open specification under the Open Web Foundation’s final specification protocol (currently OWFa 1.0 only), the technical community has the opportunity to review what GraphQL implementations must do to comply with the specification and help shape the language’s future.

While the specification is very specific about the syntax of GraphQL, such as what a valid query is and how the Schema works, it does not specify how the data is stored or what programming language the GraphQL server should be implemented in. This is very powerful and unique in the software world — it allows GraphQL servers to be implemented in any programming language, and clients to know exactly what they do because they are spec compliant. And there are already GraphQL server implementations based on a variety of programming languages, not just JavaScript, Java, and C# as you might expect, but also Go, Elixir, and Haskell. The server’s implementation language is not an obstacle — not only are there many implementations, but they are all open source. If your chosen language doesn’t already have an implementation, you can create your own.

conclusion

GraphQL is an exciting and relatively new open source API. It combines a query language, an execution engine, and an open source specification that defines the look and function of the GraphQL implementation.

GraphQL is changing the way it builds client and API applications. With GraphQL as part of their own technology stack, front-end developers have the freedom to query as much data as they want, and back-end developers can separate the needs of the client program from the back-end system architecture. Some companies typically start using GraphQL by building a GraphQL API “layer” on top of their existing back-end services. This allows client programs to get the performance and operational efficiencies they seek, while allowing the back-end team to “improve” behind their system’s GraphQL layer if needed. Often these improvements are optimized for performance, which will help ensure that programs using GraphQL run efficiently. Because of the abstraction provided by GraphQL, the system team can make these improvements while continuing to adhere to GraphQL API-level specifications.

Because GraphQL is relatively new, developers are still looking for better ways to use it. How will GraphQL change the way you develop and make you successful? There’s only one way — implement something with GraphQL!

Welcome to pay attention to the front-end public number: front-end pioneer, access to front-end engineering practical toolkit.