Skip to content

Commit 3bf4f00

Browse files
committed
Command line tool for execute Graphiql on Standalone way
1 parent 9b43018 commit 3bf4f00

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# build CLI
2+
LOCAL_TAG := $(shell git describe --tags --abbrev=0)
3+
LOCAL_BUILD := $(shell date +"%m-%d-%Y_%H_%M_%S")
4+
LOCAL_LDFLAGS = -s -X main.version=$(LOCAL_TAG) -X main.build=$(LOCAL_BUILD)
5+
6+
build-cli:
7+
go build -o bin/graphiql -ldflags "$(LOCAL_LDFLAGS)" cmd/graphiql/main.go
8+
9+
install-cli: build-cli
10+
mv bin/graphiql $(GOPATH)/bin
11+
rm -rf bin

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ func main() {
6262
}
6363
```
6464

65+
Remember that if you want to execute the GraphiQL standalone you must need allow the origin on
66+
your http server.
67+
Eg:
68+
```
69+
Access-Control-Allow-Origin: http://localhost:4040
70+
Access-Control-Allow-Credentials: true
71+
```
72+
6573
## Contribute
6674
[Contributions](https://github.com/friendsofgo/graphiql/issues?q=is%3Aissue+is%3Aopen) are more than welcome, if you are interested please fork this repo and send your Pull Request.
6775

cmd/graphiql/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/friendsofgo/graphiql"
10+
)
11+
12+
var (
13+
name = "graphiql"
14+
version = "undefined"
15+
build = "undefined"
16+
)
17+
18+
func main() {
19+
flag.Usage = usage
20+
endpoint := flag.String("endpoint", "http://localhost:8080/graphql", "Endpoint where are hosted your API, eg: http://localhost:8080/graphql")
21+
port := flag.String("port", "4040", "Port where will be launched the GraphiQL client")
22+
showVersion := flag.Bool("version", false, "Show the graphiql version information")
23+
showClientGraphiQLVersion := flag.Bool("graphiql", false, "Show the js client of GraphiQL version")
24+
flag.Parse()
25+
26+
if *showVersion {
27+
fmt.Printf("%s%s\n", name, version)
28+
return
29+
}
30+
31+
if *showClientGraphiQLVersion {
32+
fmt.Println(graphiql.GraphiQLVersion)
33+
}
34+
35+
run(*endpoint, *port)
36+
}
37+
38+
func run(endpoint, port string) {
39+
graphiqlHandler, err := graphiql.NewGraphiqlHandler(endpoint)
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
http.Handle("/graphiql", graphiqlHandler)
45+
httpAddr := ":" + port
46+
47+
log.Printf("GraphiQL run on http://localhost%s...\n", httpAddr)
48+
log.Printf("GraphQL endpoint %s\n", endpoint)
49+
log.Printf("Use (ctrl+c) for terminate the execution")
50+
51+
if err := http.ListenAndServe(httpAddr, nil); err != nil {
52+
panic(err)
53+
}
54+
}
55+
56+
func usage() {
57+
msg := fmt.Sprintf(`usage: %s [-version] [OPTIONS]
58+
%s is an standalone server to use Graphiql, based on https://github.com/graphql/graphiql
59+
`, name, name)
60+
fmt.Println(msg)
61+
flag.PrintDefaults()
62+
}

server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type Handler struct {
1313
}
1414

1515
// NewGraphiqlHandler preparing the graphiql client to execute on your own server
16-
1716
// The endpoint is the url where you have your graphql api hosted
1817
func NewGraphiqlHandler(endpoint string) (*Handler, error) {
1918
t, err := preparingTemplate()

template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
es6PromiseVersion = "4.2.5"
1818
fetchVersion = "2.0.1"
1919
reactVersion = "15.6.2"
20-
graphiQLVersion = "0.12.0"
20+
GraphiQLVersion = "0.12.0"
2121
)
2222

2323
func data(endpoint string) *dataTmpl {
@@ -26,7 +26,7 @@ func data(endpoint string) *dataTmpl {
2626
Es6PromiseVersion: es6PromiseVersion,
2727
FetchVersion: fetchVersion,
2828
ReactVersion: reactVersion,
29-
GraphiQLVersion: graphiQLVersion,
29+
GraphiQLVersion: GraphiQLVersion,
3030
}
3131
}
3232

0 commit comments

Comments
 (0)