-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
149 lines (125 loc) · 2.88 KB
/
new.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gqlwss
import (
"net/http"
"net/http/httptest"
"strings"
"github.com/gorilla/websocket"
"github.com/graphql-go/graphql"
)
func newTestClientServer(h http.Handler) (*websocket.Conn, *httptest.Server, error) {
s := httptest.NewServer(h)
url := "ws" + strings.TrimPrefix(s.URL, "http")
c, _, err := websocket.DefaultDialer.Dial(url, nil)
return c, s, err
}
func newTestSchema() (*graphql.Schema, error) {
userResolver := func(p graphql.ResolveParams) (interface{}, error) {
return testUser{1, "test user", "test@user.com"}, nil
}
tickResolver := func(p graphql.ResolveParams) (interface{}, error) {
return p.Source, nil
}
tickSubscriber := func(p graphql.ResolveParams) (interface{}, error) {
ticks := make(chan interface{})
go func() {
i := int64(1)
stop := false
for !stop {
select {
case <-p.Context.Done():
stop = true
break
default:
if i > 100 {
stop = true
} else {
ticks <- testTick{i}
i++
}
}
}
close(ticks)
}()
return ticks, nil
}
userType := graphql.NewObject(
graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int},
"name": &graphql.Field{Type: graphql.String},
"email": &graphql.Field{Type: graphql.String},
},
},
)
tickType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Tick",
Fields: graphql.Fields{
"value": &graphql.Field{Type: graphql.Int},
},
},
)
queryType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"getUser": &graphql.Field{
Type: userType,
Resolve: userResolver,
Description: "Get test user",
},
},
},
)
subscriptionType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Subscription",
Fields: graphql.Fields{
"tick": &graphql.Field{
Type: tickType,
Resolve: tickResolver,
Subscribe: tickSubscriber,
Description: "Tick tock tick tock...",
},
},
},
)
if schema, err := graphql.NewSchema(
graphql.SchemaConfig{
Query: queryType,
Subscription: subscriptionType,
},
); err != nil {
return nil, err
} else {
return &schema, nil
}
}
func NewAuth() Auth {
return Auth{}
}
func NewGraphQLSocket(schema graphql.Schema) GraphQLSocket {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
return GraphQLSocket{schema, upgrader}
}
func NewServerWithAuth(opts Options) Server {
auth := NewAuth()
graphql := NewGraphQLSocket(opts.Schema)
mux := http.NewServeMux()
mux.Handle(opts.AuthPath, auth)
mux.Handle(opts.GraphQLPath, graphql)
return Server{mux}
}
func NewServerWithoutAuth(opts Options) Server {
graphql := NewGraphQLSocket(opts.Schema)
mux := http.NewServeMux()
mux.Handle(opts.GraphQLPath, graphql)
return Server{mux}
}