-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (52 loc) · 1.97 KB
/
server.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
54
55
56
57
58
59
60
61
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/fasibio/autogql_example/graph"
"github.com/fasibio/autogql_example/graph/db"
"github.com/fasibio/autogql_example/graph/generated"
"github.com/fasibio/autogql_example/graph/model"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const defaultPort = "8080"
func main() {
dbCon, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
dbCon = dbCon.Debug()
dborm := db.NewAutoGqlDB(dbCon)
// db.AddGetHook[model.User](&dborm, "GetUser", GetUserHook{})
db.AddQueryHook[model.Company, model.CompanyFiltersInput, model.CompanyOrder](&dborm, db.QueryCompany, QueryCompanyHook{})
db.AddAddHook[model.Company, model.CompanyInput, model.AddCompanyPayload](&dborm, db.AddCompany, AddCompanyHook{})
db.AddUpdateHook[model.Company, model.UpdateCompanyInput, model.UpdateCompanyPayload](&dborm, db.UpdateCompany, UpdateCompanyHook{})
db.AddDeleteHook[model.CompanyFiltersInput, model.DeleteCompanyPayload](&dborm, db.DeleteCompany, DeleteCompanyHook{})
dborm.Init()
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{Sql: &dborm}, Directives: generated.DirectiveRoot{
Validated: DirectiveImpl,
A: DirectiveImpl,
B: DirectiveImpl,
C: DirectiveImpl,
D: DirectiveImpl,
E: DirectiveImpl,
F: DirectiveImpl,
G: DirectiveImpl,
}}))
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))
}
func DirectiveImpl(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
}