Skip to content

Commit 176252d

Browse files
committed
added nodejs-express-graphql for playground
1 parent 116f19d commit 176252d

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

examples/playground/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*-lock.json

examples/playground/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Express GraphQL playground
2+
3+
This just to run an Express GraphQL client which uses proxy to call the go graphql server.
4+
Just an easy hack to show beautiful user interface.
5+
6+
## Pre-Requisites
7+
8+
Nodejs and NPM
9+
10+
## How it works
11+
12+
```bash
13+
cd examples/playground
14+
npm install
15+
16+
go run main.go
17+
```
18+
19+
## Complex Sample
20+
21+
modify PLAYGROUND_PORT or GRAPHQL_PORT if you want:
22+
23+
```go
24+
// main.go
25+
cmd.Env = append(os.Environ(),
26+
fmt.Sprintf("GRAPHQL_PORT=%d", GRAPHQL_PORT), // GRAPHQL_PORT
27+
fmt.Sprintf("PLAYGROUND_PORT=%d", PLAYGROUND_PORT), // this value is used
28+
)
29+
```
30+
31+
You can pass query schema:
32+
33+
```go
34+
query := `
35+
type Query {
36+
hello: String
37+
}
38+
`
39+
cmd := exec.Command("node", "index.js", query)
40+
```
41+
42+
Once playground server will run you will see output like
43+
44+
🚀 GraphQL Express playground server is running on: <http://localhost:8081/graphql>
45+
46+
Open in browser: [playground](http://localhost:8081/graphql)
47+
48+

examples/playground/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const express = require("express");
2+
const axios = require("axios");
3+
const bodyparser = require("body-parser");
4+
const cors = require("cors");
5+
const { buildSchema } = require("graphql");
6+
const graphqlHTTP = require("express-graphql");
7+
let [query = ""] = process.argv.slice(2);
8+
if (!query.trim()) {
9+
query = `
10+
type Query {
11+
hello: String
12+
}
13+
`;
14+
}
15+
const schema = buildSchema(query);
16+
const PLAYGROUND_PORT = process.env.PLAYGROUND_PORT || 4000;
17+
const GRAPHQL_PORT = process.env.GRAPHQL_PORT || 8080;
18+
19+
const app = express();
20+
21+
app.use(cors());
22+
app.get(
23+
"/graphql",
24+
graphqlHTTP({
25+
schema: schema,
26+
rootValue: {},
27+
graphiql: true,
28+
})
29+
);
30+
app.post("/*", bodyparser.json(), (req, res) => {
31+
const options = {
32+
url: req.path,
33+
baseURL: `http://localhost:${GRAPHQL_PORT}/`,
34+
method: "get",
35+
params: req.body,
36+
};
37+
axios(options).then(({ data }) => res.send(data));
38+
});
39+
app.listen(PLAYGROUND_PORT, () => {
40+
console.log(
41+
`🚀 GraphQL Express playground server is running on: http://localhost:${PLAYGROUND_PORT}/graphql`
42+
);
43+
});

examples/playground/main.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

examples/playground/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "graphql-playground",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node .",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"axios": "^0.19.2",
15+
"body-parser": "^1.19.0",
16+
"cors": "^2.8.5",
17+
"express": "^4.17.1",
18+
"express-graphql": "^0.9.0",
19+
"graphql": "^15.0.0"
20+
}
21+
}

0 commit comments

Comments
 (0)