跳至正文
Wener Notes

gqlgen

约 2 分钟阅读
  • 是什么?
    • 基于生成的 GraphQL 引擎
    • Schema first - 需要 DSL 定义 GraphQL Schema
    • 类型安全
    • 支持 Plugin - 修改配置, 生成代码
  • 缺点
    • 库代码质量一般
    • 文档少
    • 插件能力弱,且没什么 Demo
    • gqlgen 最近开发不活跃 - 2021-08
    • graphql-go/graphql 开发不活跃
bash
go get -u github.com/99designs/gqlgen
go run github.com/99designs/gqlgen init

go run github.com/99designs/gqlgen generate

Note

go
type HandlerExtension interface {
  // ExtensionName should be a CamelCase string version of the extension which may be shown in stats and logging.
  ExtensionName() string
  // Validate is called when adding an extension to the server, it allows validation against the servers schema.
  Validate(schema ExecutableSchema) error
}
text
+--- REQUEST   POST /graphql --------------------------------------------+
| +- OPERATION query OpName { viewer { name } } -----------------------+ |
| |  RESPONSE  { "data": { "viewer": { "name": "bob" } } }             | |
| +- OPERATION subscription OpName2 { chat { message } } --------------+ |
| |  RESPONSE  { "data": { "chat": { "message": "hello" } } }          | |
| |  RESPONSE  { "data": { "chat": { "message": "byee" } } }           | |
| +--------------------------------------------------------------------+ |
+------------------------------------------------------------------------+
graphql
directive @goModel(model: String, models: [String!]) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

scalar Time
scalar Any
scalar Map
scalar Upload

禁用 Introspection

go
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
  if !isUserAdmin(ctx) {
    graphql.GetOperationContext(ctx).DisableIntrospection = true
  }
  return next(ctx)
})

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub7 条
其他外链1 条
最近更新commit ccdb50f编辑