-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathexample_test.go
More file actions
134 lines (108 loc) · 2.61 KB
/
example_test.go
File metadata and controls
134 lines (108 loc) · 2.61 KB
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
package graphqlws_test
import (
"context"
"encoding/json"
"fmt"
"net/http/httptest"
"strings"
"github.com/gorilla/websocket"
graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
graphqlws "github.com/graph-gophers/graphql-transport-ws"
)
const schemaSDL = `
schema {
query: Query
subscription: Subscription
}
type Query {
hello: String!
}
type Subscription {
ticks: TickEvent!
}
type TickEvent {
at: String!
count: Int!
}
`
type rootResolver struct{}
type queryResolver struct{}
type subscriptionResolver struct{}
type tickEventResolver struct {
at string
count int32
}
func (*rootResolver) Query() *queryResolver {
return &queryResolver{}
}
func (*rootResolver) Subscription() *subscriptionResolver {
return &subscriptionResolver{}
}
func (*queryResolver) Hello() string {
return "hello from graphql-transport-ws"
}
func (*subscriptionResolver) Ticks(ctx context.Context) (chan *tickEventResolver, error) {
updates := make(chan *tickEventResolver, 1)
updates <- &tickEventResolver{at: "2026-03-28T00:00:00Z", count: 1}
close(updates)
return updates, nil
}
func (r *tickEventResolver) At() string {
return r.at
}
func (r *tickEventResolver) Count() int32 {
return r.count
}
func Example() {
schema := graphql.MustParseSchema(schemaSDL, &rootResolver{})
rh := &relay.Handler{Schema: schema}
h := graphqlws.NewHandlerFunc(schema, rh)
srv := httptest.NewServer(h)
defer srv.Close()
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http")
dialer := websocket.Dialer{Subprotocols: []string{graphqlws.ProtocolGraphQLTransportWS}}
conn, _, err := dialer.Dial(wsURL, nil)
if err != nil {
panic(err)
}
defer conn.Close()
type message struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Payload json.RawMessage `json:"payload,omitempty"`
}
if err := conn.WriteJSON(message{Type: "connection_init"}); err != nil {
panic(err)
}
if err := conn.WriteJSON(message{
ID: "1",
Type: "subscribe",
Payload: json.RawMessage(`{
"query": "subscription { ticks { at count } }"
}`),
}); err != nil {
panic(err)
}
var ack message
if err := conn.ReadJSON(&ack); err != nil {
panic(err)
}
var next message
if err := conn.ReadJSON(&next); err != nil {
panic(err)
}
var complete message
if err := conn.ReadJSON(&complete); err != nil {
panic(err)
}
fmt.Println(conn.Subprotocol())
fmt.Println(ack.Type)
fmt.Println(string(next.Payload))
fmt.Println(complete.Type)
// Output:
// graphql-transport-ws
// connection_ack
// {"data":{"ticks":{"at":"2026-03-28T00:00:00Z","count":1}}}
// complete
}