1 Star 0 Fork 0

麦冬果果 / graphql-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
list_schema.go 2.18 KB
一键复制 编辑 原始数据 按行查看 历史
Romain Menke 提交于 2018-01-06 19:52 . Add benchmarks
package benchutil
import (
"fmt"
"github.com/graphql-go/graphql"
)
type color struct {
Hex string
R int
G int
B int
}
func ListSchemaWithXItems(x int) graphql.Schema {
list := generateXListItems(x)
color := graphql.NewObject(graphql.ObjectConfig{
Name: "Color",
Description: "A color",
Fields: graphql.Fields{
"hex": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "Hex color code.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if c, ok := p.Source.(color); ok {
return c.Hex, nil
}
return nil, nil
},
},
"r": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Red value.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if c, ok := p.Source.(color); ok {
return c.R, nil
}
return nil, nil
},
},
"g": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Green value.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if c, ok := p.Source.(color); ok {
return c.G, nil
}
return nil, nil
},
},
"b": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Blue value.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if c, ok := p.Source.(color); ok {
return c.B, nil
}
return nil, nil
},
},
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"colors": {
Type: graphql.NewList(color),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return list, nil
},
},
},
})
colorSchema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
return colorSchema
}
var colors []color
func init() {
colors = make([]color, 0, 256*16*16)
for r := 0; r < 256; r++ {
for g := 0; g < 16; g++ {
for b := 0; b < 16; b++ {
colors = append(colors, color{
Hex: fmt.Sprintf("#%x%x%x", r, g, b),
R: r,
G: g,
B: b,
})
}
}
}
}
func generateXListItems(x int) []color {
if x > len(colors) {
x = len(colors)
}
return colors[0:x]
}
1
https://gitee.com/mdgg0816/graphql-go.git
git@gitee.com:mdgg0816/graphql-go.git
mdgg0816
graphql-go
graphql-go
v0.7.6

搜索帮助