gql

package module
v0.0.0-...-d986d38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 8 Imported by: 1

README

该项目不再维护

该包的功能在 goql 中被继承,请跳转到 goql

gql

简化 graphql-go 开发,graphql-ql 提供的原始接口开发非常繁琐,稍有不慎就会出错。该项目就是为了简化 golang 开发 graphql 过程,让开发人员把更多的精力投入到业务逻辑的开发中。

简化内容

  1. 省去定义 graphql 对象的过程,只需定义对应的结构体即可
  2. 省去定义 query 和 mutation 的过程,只需定义 go 函数即可
  3. 省去手动获取请求参数的过程,并提供参数验证功能(完善中)
  4. 提供注入功能,简化登录验证等功能

例子

// goods 商品信息
type goods struct {
	ID    string    `json:"id"`
	Name  string    `json:"name"`
	Price float64   `json:"price"`
	URL   string    `json:"url"`
	Time  time.Time `json:"time"`
}

// goodsList 查询函数定义
func goodsList() ([]goods, error) {
	return []goods{
		goods{
			ID: "A1", Name: "A-test1",
		},
		goods{
			ID: "A2", Name: "A-test2",
		},
	}, nil
}

func init() {
	// 注册查询
	gql.Get().RegisterQuery(goodsList)
}

func main() {
	// 在 8080 端口启动服务
	g := gql.Get()
	handler := g.NewHandler(&handler.Config{
		Pretty:   true,
		GraphiQL: true,
	})

	fmt.Print(g.Summary())

	http.Handle("/graphql", handler)
	fmt.Println("The api server will run on port : ", port)
	http.ListenAndServe(fmt.Sprintf(":%d", port), nil) 
}

启动后在浏览器输入 http://localhost:8080/graphql 即可看到接口信息

约定

  1. 定义的对象名称与结构名称完全一致
  2. 输入参数只允许使用自定义结构,且只能有一个输入参数,输入参的参数名称以结构名称定义,graphql 对象类型将以 “input” 打头加上结构名称定义
  3. 输入结构允许嵌套,但是不允许出现匿名结构
  4. 输入参数如果是自定义结构,要使用指针形式
  5. 输入参数不能出现切片、数组、映射等类型(同时提交多条记录,可以使用别名)
  6. 只有返回值是一个自定义结构(指针)和 error 的函数,且输入参数为一个自定义结构(指针)、任意个数注入结构(指针)、*gqlh.InputValidator 的组合才能作为 Query 和 Mutation 对象
  7. 定义的 Query 和 Mutation 名称与函数名称完全一致
  8. 出现 Query 或 Mutation 的函数名称相同时,将舍弃后面的函数
  9. 注入函数必须是固定形式

更详细的使用方法 examples

  1. hello 简单示例
  2. query Query 示例
  3. mutation Mutation 示例
  4. inject 注入示例
  5. checkparams 参数检测示例

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GQL

type GQL struct {
	// contains filtered or unexported fields
}

GQL GraphSQL 辅助类

func Get

func Get() *GQL

Get 获取 GQL 单例实例

func NewGQL

func NewGQL() *GQL

NewGQL 创建 GQL 实例

func (*GQL) GetSchema

func (g *GQL) GetSchema() (*graphql.Schema, error)

GetSchema 获取 GraphQL 结构,如果哈没有创建,则创建

func (*GQL) NewHandler

func (g *GQL) NewHandler(cfg *handler.Config) http.Handler

NewHandler 设置 json 是否格式化

func (*GQL) RegisterInject

func (g *GQL) RegisterInject(injectFn interface{})

RegisterInject 注册注入函数,只是加入到待注册列表 @param injectFn 必须是一个函数,且函数必须是一下形式

func injectFn(ctx, context.Context, r *http.Request, gp *graphql.ResolveParams) *CustomStruct

func (*GQL) RegisterMutation

func (g *GQL) RegisterMutation(mutation interface{})

RegisterMutation 注册操作,加入到待注册列表 @param mutation 可以是一个函数,也可以是一个有多个函数的结构体

func (*GQL) RegisterMutationWithValidateFn

func (g *GQL) RegisterMutationWithValidateFn(mutation interface{}, validateFn gqlh.ValidatorFn)

RegisterMutationWithValidateFn 注册操作,并提供输入参数验证函数

func (*GQL) RegisterQuery

func (g *GQL) RegisterQuery(query interface{})

RegisterQuery 注册查询 @param query 可以是一个函数,也可以是一个有多个函数的结构体

func (*GQL) RegisterQueryWithValidateFn

func (g *GQL) RegisterQueryWithValidateFn(query interface{}, validateFn gqlh.ValidatorFn)

RegisterQueryWithValidateFn 注册查询,并提供一个输入参数验证函数 @param query 可以是一个函数,也可以是一个有多个函数的结构体

func (*GQL) Summary

func (g *GQL) Summary() string

Summary 注册说明

func (*GQL) SummaryOfFailed

func (g *GQL) SummaryOfFailed() string

SummaryOfFailed 列举注册失败的函数信息

Directories

Path Synopsis
examples
pkg
def

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL