<p>I've been playing lately with github.com/playlyfe/go-graphql/ library and tested *graphql.ResolveParams to see what they offers. Since this library has no documentation, I'm looking to make one with basic and complex examples. For now, to understant it how it works, I made a deep nested field that takes arguments for every sub-field and tried to implemented it. The code I ran looks like this (go ahead and run it, play with the api!):</p>
<pre><code>package main
import (
"net/http"
"github.com/krypton97/GraphiQL"
"github.com/krypton97/HandleGraphQL"
"github.com/playlyfe/go-graphql"
)
var data = map[string]map[string]map[string]map[string]interface{}{
"CNU": {
"A": {
"13": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"14": {
"name": "ale",
"age": 13,
"id": "1",
"grade": 1,
},
"15": {
"name": "al",
"age": 19,
"id": "15",
"grade": 76,
},
"16": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 0,
},
},
"B": {
"1": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"2": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"3": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"4": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
},
"C": {
"412": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"413": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"415": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"416": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
},
},
"ETTI": {
"A": {
"73": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"74": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"75": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"76": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
},
"B": {
"23": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"34": {
"name": "tibi",
"age": 20,
"id": "1",
"grade": 1,
},
"25": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"56": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
},
"C": {
"63": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"54": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"45": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
"26": {
"name": "alex",
"age": 19,
"id": "1",
"grade": 1,
},
},
},
}
func main() {
schema := `
type Query {
students(school: String!): Class
}
type Class {
class(class: String!): Student
}
type Student {
student(id: ID!): Info
}
type Info {
name: String
age: Int
id: String
grade: Int
}
`
resolvers := map[string]interface{}{}
resolvers["Query/students"] = func(params *graphql.ResolveParams) (interface{}, error) {
return data[params.Args["school"].(string)], nil
}
resolvers["Class/class"] = func(params *graphql.ResolveParams) (interface{}, error) {
//fmt.Println(params.Source.(map[string]map[string]map[string]interface{})[params.Args["class"].(string)])
return params.Source.(map[string]map[string]map[string]interface{})[params.Args["class"].(string)], nil
}
resolvers["Student/student"] = func(params *graphql.ResolveParams) (interface{}, error) {
//fmt.Println(params.Source.(map[string]map[string]interface{})[params.Args["id"].(string)])
return params.Source.(map[string]map[string]interface{})[params.Args["id"].(string)], nil
}
executor, err := graphql.NewExecutor(schema, "Query", "", resolvers)
if err != nil {
panic(err)
}
api := handler.New(&handler.Config{
Executor: executor,
Context: "",
Pretty: true,
})
graphiql := graphiql.New("/graphql")
http.Handle("/graphql", api)
http.Handle("/", graphiql)
http.ListenAndServe(":3000", nil)
}
</code></pre>
<p>After some work, this handles all the cases(excepting when a case cannot be found and returns nulll). What I was really looking for was to collect all the arguments from the query. After playing with the params, I found that they provide the source for the upper field so I could have simpled used the source and the current arguments the resolver uses to return what needs. Since I wasn't that sure I made a benchmark where I implemented the resolvers firstly with the params.Source method and secondly with already known arguments that matches the query, here's the benchmark to run <a href="http://pastebin.com/raw/yuFikjk5">http://pastebin.com/raw/yuFikjk5</a>.
The results was extremly different:</p>
<pre><code>Params.Source-8 20000 68259 ns/op
KnowsIDs-8 200000 11634 ns/op
</code></pre>
<p>My final question is: Is there a method to get the query arguments directly or the only way to work with nested fields with arguments is to use the params.Source?</p>
<p>If you're looking to help with the docs, please message me or write below ;)</p>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传