-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
54 lines (46 loc) · 1.14 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
54
package main
import (
"flag"
"net/http"
"github.com/eientei/wsgraphql/v1"
"github.com/eientei/wsgraphql/v1/compat/gorillaws"
"github.com/gorilla/websocket"
"github.com/graphql-go/graphql"
)
func main() {
var addr string
flag.StringVar(&addr, "addr", ":8080", "Address to listen on")
flag.Parse()
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "QueryRoot",
Fields: graphql.Fields{
"getFoo": &graphql.Field{
Description: "Returns most recent foo value",
Type: graphql.NewNonNull(graphql.Int),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return 123, nil
},
},
},
}),
})
if err != nil {
panic(err)
}
srv, err := wsgraphql.NewServer(
schema,
wsgraphql.WithProtocol(wsgraphql.WebsocketSubprotocolGraphqlTransportWS),
wsgraphql.WithUpgrader(gorillaws.Wrap(&websocket.Upgrader{
Subprotocols: []string{wsgraphql.WebsocketSubprotocolGraphqlTransportWS.String()},
})),
)
if err != nil {
panic(err)
}
http.Handle("/query", srv)
err = http.ListenAndServe(addr, nil)
if err != nil {
panic(err)
}
}