Looking to make documentation for GraphQL library, need some tips

polaris · · 411 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;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 ( &#34;net/http&#34; &#34;github.com/krypton97/GraphiQL&#34; &#34;github.com/krypton97/HandleGraphQL&#34; &#34;github.com/playlyfe/go-graphql&#34; ) var data = map[string]map[string]map[string]map[string]interface{}{ &#34;CNU&#34;: { &#34;A&#34;: { &#34;13&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;14&#34;: { &#34;name&#34;: &#34;ale&#34;, &#34;age&#34;: 13, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;15&#34;: { &#34;name&#34;: &#34;al&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;15&#34;, &#34;grade&#34;: 76, }, &#34;16&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 0, }, }, &#34;B&#34;: { &#34;1&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;2&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;3&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;4&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, }, &#34;C&#34;: { &#34;412&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;413&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;415&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;416&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, }, }, &#34;ETTI&#34;: { &#34;A&#34;: { &#34;73&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;74&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;75&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;76&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, }, &#34;B&#34;: { &#34;23&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;34&#34;: { &#34;name&#34;: &#34;tibi&#34;, &#34;age&#34;: 20, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;25&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;56&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, }, &#34;C&#34;: { &#34;63&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;54&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;45&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 1, }, &#34;26&#34;: { &#34;name&#34;: &#34;alex&#34;, &#34;age&#34;: 19, &#34;id&#34;: &#34;1&#34;, &#34;grade&#34;: 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[&#34;Query/students&#34;] = func(params *graphql.ResolveParams) (interface{}, error) { return data[params.Args[&#34;school&#34;].(string)], nil } resolvers[&#34;Class/class&#34;] = func(params *graphql.ResolveParams) (interface{}, error) { //fmt.Println(params.Source.(map[string]map[string]map[string]interface{})[params.Args[&#34;class&#34;].(string)]) return params.Source.(map[string]map[string]map[string]interface{})[params.Args[&#34;class&#34;].(string)], nil } resolvers[&#34;Student/student&#34;] = func(params *graphql.ResolveParams) (interface{}, error) { //fmt.Println(params.Source.(map[string]map[string]interface{})[params.Args[&#34;id&#34;].(string)]) return params.Source.(map[string]map[string]interface{})[params.Args[&#34;id&#34;].(string)], nil } executor, err := graphql.NewExecutor(schema, &#34;Query&#34;, &#34;&#34;, resolvers) if err != nil { panic(err) } api := handler.New(&amp;handler.Config{ Executor: executor, Context: &#34;&#34;, Pretty: true, }) graphiql := graphiql.New(&#34;/graphql&#34;) http.Handle(&#34;/graphql&#34;, api) http.Handle(&#34;/&#34;, graphiql) http.ListenAndServe(&#34;:3000&#34;, 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&#39;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&#39;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&#39;re looking to help with the docs, please message me or write below ;)</p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

411 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传