An interface is a definition of behavior that defines the interface through which the system interacts. The entity of the behavior is the function, which includes the definition and implementation, whereas the interface only focuses on the definition of the function. The function definition contains the descendant argument and the return value, contains the parameter name and type, and contains the type of the return value.

GraphQL is the interface

GraphQL is essentially an interface specification that includes type definitions and function definitions (names, parameters, return values) that precisely define the interface between the front and back end interactions.

GraphQL is not just any interface

GraphQL defines only the interface, not the implementation, and is itself language independent.

This means that GraphQL can serve as a unified specification for connecting different systems.

GraphQL contains strict type definitions that can be used for code generation, as well as a number of development AIDS that can help solve interface definition, implementation, documentation, and debugging problems.

Many RPC platforms, such as GRPC and THRIFT, can do this. In this sense, GraphQL is an RPC protocol.

If anything, GraphQL was designed to solve the problem of front – and back-end interaction. Back – to – back interactive network transport is a problem.

Traditional RPC returns complete data when the result is returned, but most of the time, we only need part of the data in the result. In this way, useless data transmission will cause bandwidth waste and affect the processing speed, which is unfavorable for applications with high response requirements or insufficient network bandwidth, such as mobile applications.

When you use the GraphQL interface, you can define what you want based on the convention, and then GraphQL returns only the data you want based on the request, similar to SQL. At the same time, multiple function call requests can be sent in a single GraphQL request, which reduces the number of requests sent and improves the response speed.

For example

Below we define a simple type and interface using the Go language.

// The User type defined
type User struct {
    ID int
    Name string
    Age int
    Posts []Post
}

type Post struct {
    ID int 
    Title string
    Owner User
}

// Define the query interface
type Query interface {
    FetchUsers() []User
    FetchPosts(userID int) []Post
    GetUserById(id int) *User // Defines the parameters and return values of the function
}

// Define the modify interface
type Mutation interface {
    CreateUser(name string, age int) User
    DeleteUser(id int) *User
}
Copy the code

We know that the interface, if successfully invoked, returns a complete set of data. For example, a successful call to GetUserById will return a full User to us. This includes all attributes of a large number of users, such as User {ID: 0, Name: “Alex”, Age: 28}. But what if our bandwidth is too narrow and we only need the User’s Name information? This is where GraphQL differs from interfaces. Like SQL, with GraphQL you can declare what attributes you need.

{
    GetUserById(id: 1) {
        Name
    }
}
Copy the code

Interface, you call a function, return a result. You return multiple results, call multiple functions. With GraphQL, you can put multiple requests for data into a single request.

{
    GetUserById(id: 1) {
        Name
    }
    
    FetchPosts(userId: 1) {
        title
    }
}
Copy the code

Furthermore, GraphQL supports nesting of fetching information along type definitions. The above query could also be written as follows.

{
    GetUserById(id: 1) {
        Posts {
            title
        }
    }
}
Copy the code

Of course, you can also go crazy and continue to nest data.

{
    GetUserById(id: 1) {
        Posts {
            title
            Owner {
                Name {
                    Posts {
                        title
                        ...
                    }
                }
            }
        }
    }
}
Copy the code

According to the definition of interface type, the continuous nesting of grabbing data is the origin of Graph in GraphQL, which expresses the connotation of Graph + QL = according to Graph + search. Of course, this kind of abnormal grab is very scary, if there are bad people deliberately send this request to kill us, we are finished, so there must be a way to limit this behavior. Fortunately, we can perform AST parsing on user requests and limit the depth of nesting, for example, we can let you nest up to 3 layers.

conclusion

GraphQL is essentially an interface protocol designed to solve the problem of front – and back-end interaction. A good solution to the interface definition, documentation, debugging, use and other problems, visual inspection will be more and more popular.