title: Golang, how to use a struct to write JSON response over HTTP?
body: <p><strong>The Goal:</strong></p>
  
  <p>Using github.com/neelance/graphql-go starwars example, I'm trying to write a JSON response to my ReactJS client. That struct stuff is completely new for me, Golang as well btw.</p>
  
  <p><strong>The question:</strong></p>
  
  <p>What should <code>data</code> variable be in order to get the appropriate response to the following example GraphQL query? </p>
  
  <pre class="lang-graphql prettyprint-override"><code>query clientQuery {
    character(id: 1000) {
      name
      appearsIn
    }
  }
  </code></pre>
  
  <p><strong>Additional info:</strong></p>
  
  <p>From what I've read here and there, <code>data</code> must be some kind of struct. I've got plenty of structs available in the example (see starwars.go below).</p>
  
  <p><strong>The Code to be modified (main.go):</strong></p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "encoding/json"
      "log"
      "net/http"
  
      "github.com/neelance/graphql-go"
      "github.com/neelance/graphql-go/example/starwars"
      "github.com/neelance/graphql-go/relay"
  )
  
  var schema *graphql.Schema
  
  func init() {
      schema = graphql.MustParseSchema(starwars.Schema, &amp;starwars.Resolver{})
  }
  
  func main() {
  
      port := ":8080"
      log.Printf(`GraphQL server starting up on http://localhost%v`, port)
  
      http.Handle("/query", &amp;relay.Handler{Schema: schema})
  
      http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
  
          // THIS IS SUPER WRONG, data should be something
          // like data := starwars.Resolver{} or so?
          data := `{"data":{"character":{"name":"Luke Skywalker","appearsIn":["NEWHOPE","EMPIRE","JEDI"]}}}`
  
          w.Header().Set("Content-Type", "application/json")
          w.WriteHeader(http.StatusCreated)
          json.NewEncoder(w).Encode(data)
      })
  
      log.Fatal(http.ListenAndServe(port, nil))
  }
  </code></pre>
  
  <p><strong>REFERENCE 1 - <a href="https://github.com/neelance/graphql-go/blob/master/example/starwars/starwars.go" rel="nofollow noreferrer">starwars.go</a></strong></p>
  
  <p><strong>REFERENCE 2 - <a href="https://github.com/neelance/graphql-go/blob/master/relay/relay.go" rel="nofollow noreferrer">relay.go</a></strong></p>