Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Commit ea3e2ec

Browse files
committed
Using neelance/graphql-go
1 parent 0c58dc2 commit ea3e2ec

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [0.1.0] 2017-09-13
11+
12+
- [SystemVersion type](resources/schema/system.graphql#L6) is the only query-ready field for now.
13+
14+
### Changed
15+
16+
- Using [neelance/graphql-go](https://github.com/neelance/graphql-go).
17+
- `API_ENDPOINT` is now `UI_ENDPOINT`.
18+
- `GQL_PORT` is available again.
19+
20+
### Removed
21+
22+
- No longer using [graphql-go/graphql](https://github.com/graphql-go/graphql)
23+
24+
## [0.1.0-rc] 2017-09-11
25+
26+
### Added
27+
28+
- Start using a changelog file in order to keep a neat track of every change made to the project.
29+
- [Version type](resources/schema/system.graphql#L6) is the only query-ready field for now.
30+
31+
### Changed
32+
33+
- Moving from [graphql-go/graphql](https://github.com/graphql-go/graphql) to [neelance/graphql-go](https://github.com/neelance/graphql-go).
34+
- `API_ENDPOINT`, `GQL_PORT` & `GRAPHIQL` are temporarily unavailable to be set in the Docker image.
35+
36+
### Removed
37+
38+
- No longer using [graphql-go/graphql](https://github.com/graphql-go/graphql)

cmd/gdapi/main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"os"
7+
8+
"github.com/gorilla/handlers"
9+
graphql "github.com/neelance/graphql-go"
10+
"github.com/neelance/graphql-go/relay"
11+
"gitlab.com/klud/graphql-docker-api/resolver"
12+
"gitlab.com/klud/graphql-docker-api/schema"
13+
)
14+
15+
var scheme *graphql.Schema
16+
17+
// Gets GraphiQL route from UI_ENDPOINT env var, else assigns one by default
18+
func uiEndpoint() string {
19+
end := os.Getenv("UI_ENDPOINT")
20+
if end == "" {
21+
end = "/graphql"
22+
}
23+
return end
24+
}
25+
26+
// Gets the port to serve on from GQL_PORT env var, else assigns one by default
27+
func getPort() string {
28+
port := os.Getenv("GQL_PORT")
29+
if port == "" {
30+
port = ":8080"
31+
}
32+
return port
33+
}
34+
35+
// Enables GraphiQL by default, if 0 is set GraphiQL is disabled.
36+
func enableUI() bool {
37+
env := os.Getenv("GRAPHIQL")
38+
if env == "0" {
39+
return false
40+
}
41+
return true
42+
}
43+
44+
func init() {
45+
scheme = graphql.MustParseSchema(schema.Schema, &resolver.Resolver{})
46+
}
47+
48+
func main() {
49+
var uiStatus string
50+
51+
http.Handle("/query", &relay.Handler{Schema: scheme})
52+
53+
if enableUI() {
54+
uiStatus = "enabled"
55+
http.Handle(uiEndpoint(), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56+
w.Write(page)
57+
}))
58+
log.Printf("Endpoint: /query | GraphiQL: %v | UI route: %v | Serving on %v\n", uiStatus, uiEndpoint(), getPort())
59+
} else {
60+
uiStatus = "disabled"
61+
log.Printf("Endpoint: /query | GraphiQL: %v | Serving on %v\n", uiStatus, getPort())
62+
}
63+
64+
// log.Printf("Endpoint: %v | Serving on %v\n", uiEndpoint(), getPort())
65+
log.Fatalln(http.ListenAndServe(getPort(), handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)))
66+
}
67+
68+
var page = []byte(`
69+
<!DOCTYPE html>
70+
<html>
71+
<head>
72+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.css" />
73+
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.1.0/fetch.min.js"></script>
74+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.min.js"></script>
75+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js"></script>
76+
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.js"></script>
77+
</head>
78+
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
79+
<div id="graphiql" style="height: 100vh;">Loading...</div>
80+
<script>
81+
function graphQLFetcher(graphQLParams) {
82+
return fetch("/query", {
83+
method: "post",
84+
body: JSON.stringify(graphQLParams),
85+
credentials: "include",
86+
}).then(function (response) {
87+
return response.text();
88+
}).then(function (responseBody) {
89+
try {
90+
return JSON.parse(responseBody);
91+
} catch (error) {
92+
return responseBody;
93+
}
94+
});
95+
}
96+
97+
ReactDOM.render(
98+
React.createElement(GraphiQL, {fetcher: graphQLFetcher}),
99+
document.getElementById("graphiql")
100+
);
101+
</script>
102+
</body>
103+
</html>
104+
`)

resolver/resolver.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package resolver
2+
3+
import (
4+
"github.com/docker/docker/api/types"
5+
"github.com/docker/docker/client"
6+
"golang.org/x/net/context"
7+
)
8+
9+
var (
10+
ctx = context.Background()
11+
)
12+
13+
type (
14+
Resolver struct{}
15+
systemResolver struct {
16+
system client.Client
17+
}
18+
systemVersionResolver struct {
19+
version types.Version
20+
}
21+
)
22+
23+
func (r *Resolver) System() *systemResolver {
24+
dkr, _ := client.NewEnvClient()
25+
return &systemResolver{system: *dkr}
26+
}
27+
28+
func (r *systemResolver) Version() *systemVersionResolver {
29+
v, _ := r.system.ServerVersion(ctx)
30+
return &systemVersionResolver{version: v}
31+
}

resolver/system.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package resolver
2+
3+
func (r *systemVersionResolver) ApiVersion() string {
4+
return r.version.APIVersion
5+
}
6+
7+
func (r *systemVersionResolver) Arch() string {
8+
return r.version.Arch
9+
}
10+
11+
func (r *systemVersionResolver) BuildTime() string {
12+
return r.version.BuildTime
13+
}
14+
15+
func (r *systemVersionResolver) Experimental() bool {
16+
return r.version.Experimental
17+
}
18+
19+
func (r *systemVersionResolver) GitCommit() string {
20+
return r.version.GitCommit
21+
}
22+
23+
func (r *systemVersionResolver) GoVersion() string {
24+
return r.version.GoVersion
25+
}
26+
27+
func (r *systemVersionResolver) KernelVersion() string {
28+
return r.version.KernelVersion
29+
}
30+
31+
func (r *systemVersionResolver) MinApiVersion() string {
32+
return r.version.MinAPIVersion
33+
}
34+
35+
func (r *systemVersionResolver) Os() string {
36+
return r.version.Os
37+
}
38+
39+
func (r *systemVersionResolver) Version() string {
40+
return r.version.Version
41+
}

resources/docker-go-graphql_400.png

60.7 KB
Loading

0 commit comments

Comments
 (0)