当前位置:   article > 正文

gqlgen学习总结

gqlgen

1.要有go.mod和go.sum这两个文件才能去执行go run github.com/99designs/gqlgen去更新generated.go和models_gen.go,在没有这两个文件的时候去执行该命令,会把generated.go文件删除

2.要执行了go mod init github.com/[username]/[project]才能生成go.mod

3.要执行了go get github.com/99designs/gqlgen才能生成go.sum

4.在generated.go里面存放了很多信息,但我们常用到的是interface接口信息,如:

  1. type MutationResolver interface {
  2. CreateTodo(ctx context.Context, input NewTodo) (*Todo, error)
  3. }

在resolver.go里面去编写这些接口的实现,也就是增删改查的业务逻辑,如:

  1. func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (*Todo, error) {
  2. newID := r.nextID
  3. r.nextID++
  4. newTodo := &Todo{
  5. DatabaseID: newID,
  6. Description: input.Text,
  7. }
  8. r.todos = append(r.todos, newTodo)
  9. return newTodo, nil
  10. }

5.gqlgen.yml的schema属性是指明schema.graphql存放的位置

schema: "*.graphql"

6.gqlgen.yml的exec.filename属性是指明generated.go存放的位置

  1. exec:
  2. filename: generated.go

7.gqlgen.yml的model.filename属性是指明model_gen.go存放的位置

  1. model:
  2. filename: models_gen.go

8.schema和struct有的字段名称可能是不匹配的,但传的值还是一样的,只是传给struct的时候改了个名称,不是直接用schema的名称,这个问题可以在gqlgen.yml里面配置解决

  1. models:
  2. #用于schema和struct的字段的不同名称的匹配
  3. Todo: # Object
  4. fields:
  5. text:
  6. fieldName: Description # Field

schema里面的text字段,传给struct时变成了Description

除了以上在gqlgen.xml中配置,还有一种方法也可以达到同样的效果,就是使用指令directive。在schema.graphql中编写以下代码:

  1. directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
  2. type User {
  3. id: ID!
  4. goo:String!
  5. name: String! @goField(name:"FullName")
  6. }

schema里面的name字段,传给struct时变成了FullName

除了以上使用指令directive,还有第三种方法:json序列化

  1. type Car struct {
  2. Make string
  3. ShortState string
  4. LongState string `gqlgen:"state"`
  5. Model string
  6. Color string
  7. OdometerReading int
  8. }
  1. type Car {
  2. make: String!
  3. model: String!
  4. state: String!
  5. color: String!
  6. odometerReading: Int!
  7. }

通过json序列化, 把schema.graphql中的LongState字段匹配到struct中的state字段

9.models_gen.go和generated.go是用go run github.com/99designs/gqlgen生成的,不用编辑

10.Resolver结构体一般包含的属性是要操作的其他结构体

11.在server.go使用NewExecutableSchema(cfg Config),可以做数据的初始化,如:

  1. func New() Config {
  2. c := Config{
  3. Resolvers: &Resolver{
  4. todos: []*Todo{
  5. {DatabaseID: 1, Description: "A todo not to forget", Done: false},
  6. {DatabaseID: 2, Description: "This is the most important", Done: false},
  7. {DatabaseID: 3, Description: "Please do this or else", Done: false},
  8. },
  9. nextID: 3,
  10. },
  11. }
  12. return c
  13. }
  14. type Resolver struct {
  15. todos []*Todo
  16. nextID int
  17. }

12.使用了@goModel指令后,该schema无法通过命令更新到gen.go中,所以它的struct只能自定义编写,如:

  1. type User
  2. @goModel(model:"github.com/99designs/gqlgen/example/config.User") {
  3. id: ID!
  4. goo:String!
  5. name: String! @goField(name:"FullName")
  6. }

其实这个功能也能够在gqlgen.yml里面实现,如

  1. models:
  2. User:
  3. model: github.com/99designs/gqlgen/example/config.User

13.input是输入类型,一般作为Mutation函数的参数传递,如:

  1. input NewTodo {
  2. text: String!
  3. userId: String!
  4. foo:String!
  5. }
  6. type Mutation {
  7. createTodo(input: NewTodo!): Todo!
  8. }

14.gqlgen的自定义指令一般配合着Config来使用,用来做一些数据的初始化或配置,例如,在schema.graphql中编写代码:

directive @us(id: ID!) on MUTATION | QUERY | FIELD

通过go run github.com/99designs/gqlgen命令去生成接口,在generated.go中可以看到:

  1. type DirectiveRoot struct {
  2. Us func(ctx context.Context, obj interface{}, next graphql.Resolver, id string) (res interface{}, err error)
  3. }

然后就可以编写业务逻辑代码去实现这个接口

  1. func New() Config {
  2. //可以参照generated.go中的Config结构体来写
  3. c := Config{
  4. Resolvers: &resolvers{
  5. todos: []*Todo{
  6. {ID: 1, Text: "A todo not to forget", Done: false, owner: you},
  7. {ID: 2, Text: "This is the most important", Done: false, owner: you},
  8. {ID: 3, Text: "Somebody else's todo", Done: true, owner: them},
  9. {ID: 4, Text: "Please do this or else", Done: false, owner: you},
  10. },
  11. lastID: 4,
  12. },
  13. }
  14. c.Directives.Us = func(ctx context.Context, obj interface{}, next graphql.Resolver, id int) (interface{}, error) {
  15. ...
  16. }
  17. return c
  18. }

15.绑定的字段是一个方法

  1. type Person {
  2. name string
  3. }
  4. type Car {
  5. make: String!
  6. model: String!
  7. color: String!
  8. odometerReading: Int!
  9. owner: Person
  10. }
  1. type Car struct {
  2. Make string
  3. Model string
  4. Color string
  5. OwnerID *string
  6. OdometerReading int
  7. }
  8. func (c *Car) Owner() (*Person) {
  9. // get the car owner
  10. //....
  11. return owner
  12. }
  1. models:
  2. Car:
  3. model: github.com/my/app/models.Car
  4. Person:
  5. model: github.com/my/app/models.Person

Owner作为Car结构体的一个方法,返回的是*Person

16.schema.graphql绑定到匿名或嵌入式结构

  1. type Truck {
  2. make: String!
  3. model: String!
  4. state: String!
  5. color: String!
  6. odometerReading: Int!
  7. is4x4: Bool!
  8. }
  9. type Car {
  10. make: String!
  11. model: String!
  12. state: String!
  13. color: String!
  14. odometerReading: Int!
  15. }
  1. type Truck struct {
  2. Car
  3. Is4x4 bool
  4. }
  5. type Car struct {
  6. Make string
  7. State string
  8. Model string
  9. Color string
  10. OdometerReading int
  11. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/308554
推荐阅读
相关标签
  

闽ICP备14008679号