What is GraphQL

GraphQL is an open source, API-oriented data query operation language and the corresponding runtime environment. It was still in internal development at Facebook in 2012 and wasn’t released publicly until 2015. On November 7, 2018, Facebook moved the GraphQL project to the newly formed GraphQL Foundation, which is part of the non-profit Linux Foundation.

GraphQL offers a more efficient, powerful and flexible way to develop Web APIs than REST and other Web service architectures. It avoids the return of a lot of redundant data on the server side by defining the data structure as required by the client, while the server is responsible for returning the corresponding data of the same data structure, but at the same time, it also means that the web cache of query results cannot be effectively utilized in this way. The flexibility and richness of a query language like GraphQL also adds complexity, making it possible that simple APIs may not be suitable for this approach.

2. Why do I want to use GraphQL for refactoring

The YEE payroll system started out with REST, and REST has both advantages and disadvantages, and for me, the disadvantages are that it’s hard to describe good relationships, and often urls get complicated and long and smelly. For example, a payroll system that wants to get all of an employee’s data may require many HTTP requests before it can be combined into the desired data. And you can’t customize the data, so you pull it all out every time.

For example REST:

/profile/:id Obtain the basic information of the employee. /profile/:id/ Department Obtain the department of the employee. /profile/: ID /salary Obtain the salary information of the employeeCopy the code

Similar to the above information, first get the basic information of the employee, use ID to get the employee’s payroll, but also display the employee’s department information. Of course it’s not that simple in a real system, but here’s just an example.

When converted to GraphSQL, it looks something like this:

Query profile{profile(filter:{id:10086}){total skip take rows{id name picture # Department {id name # unnecessary information can be omitted} salary {total # want data at any time to add}}}}Copy the code

For payroll data management applications, I think GraphQL is a good fit.

3. How should Golang integrate with GraphQL?

I spent a lot of time selecting libraries until I found github.com/99designs/g… This library, it feels, has completely changed the way I organize code.

How can GQLGen be used

(1) Installation

 go get github.com/99designs/gqlgen

(2) Initialization

go run github.com/99designs/gqlgen init.

(3) Edit GQLgen. Yml

Then, in the root directory of your Golang project, a gqlgen.yml file appears, which looks something like this:

Schema: -graph /schema/*.graphqls # graph/schema/ exec: filename: graph/generated/generated.go package: generated model: filename: graph/model/models_gen.go package: model resolver: layout: follow-schema dir: graph package: graph autobind: - "gohr/models" omit_slice_element_pointers: true models: Glossary: # this is a model in our system, fields: user: user: # is defined here because we want User \ Chapter \ Word to force a resolver. A chapter of resolver: true Word: resolver: trueCopy the code

(4) Write graphQL file

Create a new directory, graph/schema/

Create a new file, app.graphqls

directive @hasRole(resolver: String!) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION directive @needLogin(resolver: String!) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION input Pagination { skip: Int! take: Int! } type LoginResponse { token:String! permissions:[String!] ! } input LoginInput { username:String email:String! password:String! } input LogoutInput { username:String email:String! } input ResetPasword { ids:[Int!] ! } type Mutation { login(input:LoginInput!) :LoginResponse! logout(input:LogoutInput!) :Boolean! }Copy the code

The graphSQL file above is just a very basic one, defining login and logout.

I have defined two directives for permission control. Don’t worry about them now. You will see how to use them later.

(5) Generate code

PS E:\project\gohr>  go run github.com/99designs/gqlgen generate .
Copy the code

The following files are generated:

The code is automatically generated by GQLGen, and we just have to find the places panic(FMt.errorf (“not Implemented “)) places and fill them in with our own code.

And that’s why I said, I completely changed the way I write code. After using GraphQL, I will first conceive the graphQL definition file, figure out the composition of a model, what data is needed, and what operations are required. After defining the GraphQL file and generating it by GQLGen, we just need to fill in the blanks.

(6) Verify the data API

http://localhost:20191/playground
Copy the code

It’s really handy to validate data.

Good night, ready to sleep, and then prepare to write system permission control ideas.

                                    

Personal home page: YEE Domain

Recite the words Flutter APP: domain English APP