-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (82 loc) · 2.82 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"net/http"
"time"
"github.com/samsarahq/thunder/graphql"
"github.com/samsarahq/thunder/graphql/graphiql"
"github.com/samsarahq/thunder/graphql/introspection"
"github.com/samsarahq/thunder/graphql/schemabuilder"
"github.com/samsarahq/thunder/reactive"
)
type post struct {
Title string
Body string
CreatedAt time.Time
}
// server is our graphql server.
type server struct {
posts []post
}
// registerQuery registers the root query type.
func (s *server) registerQuery(schema *schemabuilder.Schema) {
obj := schema.Query()
obj.FieldFunc("posts", func() []post {
return s.posts
})
}
// registerMutation registers the root mutation type.
func (s *server) registerMutation(schema *schemabuilder.Schema) {
obj := schema.Mutation()
obj.FieldFunc("echo", func(args struct{ Message string }) string {
return args.Message
})
}
// registerPost registers the post type.
func (s *server) registerPost(schema *schemabuilder.Schema) {
obj := schema.Object("Post", post{})
obj.FieldFunc("age", func(ctx context.Context, p *post) string {
reactive.InvalidateAfter(ctx, 5*time.Second)
return time.Since(p.CreatedAt).String()
})
}
// schema builds the graphql schema.
func (s *server) schema() *graphql.Schema {
builder := schemabuilder.NewSchema()
s.registerQuery(builder)
s.registerMutation(builder)
s.registerPost(builder)
return builder.MustBuild()
}
func main() {
// Instantiate a server, build a server, and serve the schema on port 3030.
server := &server{
posts: []post{
{Title: "first post!", Body: "I was here first!", CreatedAt: time.Now()},
{Title: "graphql", Body: "did you hear about Thunder?", CreatedAt: time.Now()},
},
}
schema := server.schema()
introspection.AddIntrospectionToSchema(schema)
// Expose schema and graphiql.
http.Handle("/graphql", graphql.Handler(schema))
http.Handle("/graphiql/", http.StripPrefix("/graphiql/", graphiql.Handler()))
http.ListenAndServe(":3030", nil)
}
// Using Thunder without Websockets (POST requests)
// For use with non-live clients (e.g. Relay, Apollo) thunder provides an HTTP handler that can serve POST requests, instead of having the client connect over a websocket. In this mode, thunder does not provide live query updates.
// In the above example, the main function would be changed to look like:
// func main() {
// // Instantiate a server, build a server, and serve the schema on port 3030.
// server := &server{
// posts: []post{
// {Title: "first post!", Body: "I was here first!", CreatedAt: time.Now()},
// {Title: "graphql", Body: "did you hear about Thunder?", CreatedAt: time.Now()},
// },
// }
// schema := server.schema()
// introspection.AddIntrospectionToSchema(schema)
// // Expose GraphQL POST endpoint.
// http.Handle("/graphql", graphql.HTTPHandler(schema))
// http.ListenAndServe(":3030", nil)
// }