美文网首页
gqlgen初体验(四)

gqlgen初体验(四)

作者: 戈壁堂 | 来源:发表于2021-05-25 20:07 被阅读0次

根据How to configure gqlgen using gqlgen.yml中的描述,models字段的含义如下——

This section declares type mapping between the GraphQL and go type systems

The first line in each type will be used as defaults for resolver arguments and
modelgen, the others will be allowed when binding to fields. Configure them to
your liking

参考Create Your First Simple GraphQL Golang Application with GQLGen中的实践——

schema:
- schema.graphql
exec:
  filename: generated.go
models:
  Person:
    model: github.com/antoooks/go-graphql-tutorial/models.Person
  Pet:
    model: github.com/antoooks/go-graphql-tutorial/models.Pet
resolver:
  filename: resolver.go
  type: Resolver
autobind: []

会优先使用models中定义的映射关系;如果没有找到,则会根据自动匹配规则,创建新的resolver进行实现。

例如,以下查询表示的查询方法为app,返回的对象再做“次级选择”(sub-selection),返回users对象的username, role两个字段。这意味着查询方法app(这个方法后台定义返回一个App对象)返回的对象里一定包含一个次级对象查询users的实现。

query($id: String!){
    app (id: $id) {
      users {
        username
        role
      }
    }
  }

验证一下:采用默认的配置文件,在App.graphqls文件中添加一个新的查询方法——

type App {
    # The app id.
    id: Int
    # The namespace of the app
    namespace: String!
    # The name of the app
    name: String!
    # The users belong to the app
    users: [AppUser]
    # 新增一个方法
    geb: [Gebitang]
}

type Gebitang {
    id: Int!
    name: String!
    grade: String
}

如果在配置文件中指定了App对应的model——github.com/antoooks/go-graphql-tutorial/models.App,需要在github.com/antoooks/go-graphql-tutorial/models目录下的App.go文件中定义对应的结构体和方法

func (a *App) Geb(ctx context.Context) ([]*Gebitang, error) {
 g := make([]*Gebitang, 0)
 ...
 return g, nil
}

Gebitang struct {
  Id    int
  Name  string
  Grade string
}

这种情况下执行gqlgen generate时,只会更新最终的generated.go文件——用于生产运行时;如果没有更新App.go的情况执行gqlgen generate,则会根据配置文件自动更新——

  • app.resolver.go 自动生成查询方法GebGetInfo方法,需要手动做进一步的实现,如下。
  • models_gen.go 中自动生成对Gebitang结构体的声明,如下。
# in app.resolver.go
func (r *appResolver) Geb(ctx context.Context, obj *App) ([]*Gebitang, error) {
 panic(fmt.Errorf("not implemented"))
}

func (r *appResolver) GetInfo(ctx context.Context, obj *App) ([]*GebInfo, error) {
 panic(fmt.Errorf("not implemented"))
}

# in models_gen.go 
type Gebitang struct {
 ID    int     `json:"id"`
 Name  string  `json:"name"`
 Grade *string `json:"grade"`
}

相关文章

网友评论

      本文标题:gqlgen初体验(四)

      本文链接:https://www.haomeiwen.com/subject/nahcsltx.html