
Use GraphQL instead of SQL to fetch complex related data from your DB in GO - gsvclass
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
======
verdverm
Yes, but how do you work with the data once you get it back? I typically
decode it into go structs.

What happens when the data model changes? How does type checking and
validation happen?

You example looks cut off on the right, certain lines seem truncated.

~~~
verdverm
Generally, text based programming without type checking is dangerous and error
prone

~~~
verdverm
Downvotes without replies... guess maybe the truth hurts?

------
brettkromkamp
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 :)

