TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

1 pointsby gsvclassabout 5 years ago
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 comments

verdvermabout 5 years ago
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 未加载
brettkromkampabout 5 years ago
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 :)