-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
53 lines (40 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"log"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/shellbear/go-graphql-example/ent"
"github.com/shellbear/go-graphql-example/graph"
"github.com/shellbear/go-graphql-example/graph/generated"
)
const defaultPort = "8080"
func newClient() *ent.Client {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
return client
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
client := newClient()
defer client.Close()
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{
Client: client,
}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}