|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + |
| 12 | + "github.com/graphql-go/graphql" |
| 13 | + "github.com/graphql-go/graphql/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + GRAPHQL_PORT := 8080 |
| 18 | + PLAYGROUND_PORT := 8081 |
| 19 | + query := ` |
| 20 | + type Query { |
| 21 | + hello: String |
| 22 | + } |
| 23 | +` |
| 24 | + http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) { |
| 25 | + query := r.URL.Query().Get("query") |
| 26 | + result := graphql.Do(graphql.Params{ |
| 27 | + Schema: testutil.StarWarsSchema, |
| 28 | + RequestString: query, |
| 29 | + }) |
| 30 | + json.NewEncoder(w).Encode(result) |
| 31 | + }) |
| 32 | + |
| 33 | + cmd := exec.Command("node", "index.js", query) |
| 34 | + var out bytes.Buffer |
| 35 | + cmd.Stdout = &out |
| 36 | + cmd.Env = append(os.Environ(), |
| 37 | + fmt.Sprintf("GRAPHQL_PORT=%d", GRAPHQL_PORT), // GRAPHQL_PORT |
| 38 | + fmt.Sprintf("PLAYGROUND_PORT=%d", PLAYGROUND_PORT), // this value is used |
| 39 | + ) |
| 40 | + cmd.Stdout = os.Stdout |
| 41 | + err := cmd.Start() |
| 42 | + if err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | + fmt.Printf("🚀 GraphQL Express playground server is running on: http://localhost:%d/graphql\n", PLAYGROUND_PORT) |
| 46 | + fmt.Println("Now server is running on port 8080") |
| 47 | + fmt.Println("Test with Get : curl -g 'http://localhost:8080/graphql?query={hero{name}}'") |
| 48 | + |
| 49 | + http.ListenAndServe(fmt.Sprintf(":%d", GRAPHQL_PORT), nil) |
| 50 | + |
| 51 | + // if err := cmd.Run(); err != nil { |
| 52 | + // log.Fatal(err) |
| 53 | + // fmt.Printf("🚀 GraphQL Express playground server is running on: http://localhost:%d/graphql\n", PLAYGROUND_PORT) |
| 54 | + // } |
| 55 | + // // fmt.Printf("in all caps: %q\n", out.String()) |
| 56 | +} |
0 commit comments