TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Use GraphQL instead of SQL to fetch complex related data from your DB in GO

1 点作者 gsvclass大约 5 年前
The idea behind this library is to build an alternative way to query databases using GraphQL instead of SQL. Unlike with direct SQL or an ORM you don&#x27;t have to write all the steps required to get the data you want in the structure you need, instead just describe it with GraphQL and you&#x27;ll get exactly the data back.<p>package main<p>import ( &quot;database&#x2F;sql&quot; &quot;fmt&quot; &quot;time&quot; &quot;github.com&#x2F;dosco&#x2F;super-graph&#x2F;core&quot; _ &quot;github.com&#x2F;jackc&#x2F;pgx&#x2F;v4&#x2F;stdlib&quot; )<p>func main() { db, err := sql.Open(&quot;pgx&quot;, &quot;postgres:&#x2F;&#x2F;postgrs:@localhost:5432&#x2F;example_db&quot;) if err != nil { log.Fatal(err) }<p><pre><code> sg, err := core.NewSuperGraph(nil, db) if err != nil { log.Fatal(err) } &#x2F;&#x2F; And here&#x27;s the GraphQL query to fetch posts, comments, author, etc query := ` query { posts { id title body comments(limit: 5) { id user { id name } } user { id name } } }` res, err := sg.GraphQL(context.Background(), query, nil) if err != nil { log.Fatal(err) } fmt.Println(string(res.Data))</code></pre> }<p>https:&#x2F;&#x2F;github.com&#x2F;dosco&#x2F;super-graph

2 条评论

verdverm大约 5 年前
Yes, but how do you work with the data once you get it back? I typically decode it into go structs.<p>What happens when the data model changes? How does type checking and validation happen?<p>You example looks cut off on the right, certain lines seem truncated.
评论 #23012692 未加载
评论 #23012674 未加载
brettkromkamp大约 5 年前
I was thinking of the exact same concept the other day; I need something along these lines for one of my own projects. Anyway, I’ll take a closer look at this during the weekend :)