Skip to content

Commit fb3865b

Browse files
authored
Subscription client and named operation (hasura#2)
Extend https://github.com/shurcooL/graphql library with new features: - Subscription client - Named operation wrappers
1 parent d48a9a7 commit fb3865b

15 files changed

+1197
-28
lines changed

LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
MIT License
22

33
Copyright (c) 2017 Dmitri Shuralyov
4+
Copyright (c) 2020 Hasura
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

README.md

+130-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
graphql
1+
go-graphql-client
22
=======
33

4-
[![Build Status](https://travis-ci.org/shurcooL/graphql.svg?branch=master)](https://travis-ci.org/shurcooL/graphql) [![GoDoc](https://godoc.org/github.com/shurcooL/graphql?status.svg)](https://godoc.org/github.com/shurcooL/graphql)
4+
[![Build Status](https://travis-ci.org/hasura/go-graphql-client.svg?branch=master)](https://travis-ci.org/hasura/go-graphql-client.svg?branch=master) [![GoDoc](https://godoc.org/github.com/hasura/go-graphql-client?status.svg)](https://pkg.go.dev/github.com/hasura/go-graphql-client)
5+
6+
**Preface:** This is a fork of `https://github.com/shurcooL/graphql` with extended features (subscription client, named operation)
57

68
Package `graphql` provides a GraphQL client implementation.
79

@@ -12,10 +14,10 @@ For more information, see package [`github.com/shurcooL/githubv4`](https://githu
1214
Installation
1315
------------
1416

15-
`graphql` requires Go version 1.8 or later.
17+
`go-graphql-client` requires Go version 1.13 or later.
1618

1719
```bash
18-
go get -u github.com/shurcooL/graphql
20+
go get -u github.com/hasura/go-graphql-client
1921
```
2022

2123
Usage
@@ -278,6 +280,130 @@ fmt.Printf("Created a %v star review: %v\n", m.CreateReview.Stars, m.CreateRevie
278280
// Created a 5 star review: This is a great movie!
279281
```
280282
283+
### Subcriptions
284+
285+
Usage
286+
-----
287+
288+
Construct a Subscription client, specifying the GraphQL server URL.
289+
290+
```Go
291+
client := graphql.NewSubscriptionClient("wss://example.com/graphql")
292+
defer client.Close()
293+
294+
// Subscribe subscriptions
295+
// ...
296+
// finally run the client
297+
client.Run()
298+
```
299+
300+
#### Subscribe
301+
302+
To make a GraphQL subscription, you need to define a corresponding Go type.
303+
304+
For example, to make the following GraphQL query:
305+
306+
```GraphQL
307+
subscription {
308+
me {
309+
name
310+
}
311+
}
312+
```
313+
314+
You can define this variable:
315+
316+
```Go
317+
var subscription struct {
318+
Me struct {
319+
Name graphql.String
320+
}
321+
}
322+
```
323+
324+
Then call `client.Subscribe`, passing a pointer to it:
325+
326+
```Go
327+
subscriptionId, err := client.Subscribe(&query, nil, func(dataValue *json.RawMessage, errValue error) error {
328+
if errValue != nil {
329+
// handle error
330+
// if returns error, it will failback to `onError` event
331+
return nil
332+
}
333+
data := query{}
334+
err := json.Unmarshal(dataValue, &data)
335+
336+
fmt.Println(query.Me.Name)
337+
338+
// Output: Luke Skywalker
339+
})
340+
341+
if err != nil {
342+
// Handle error.
343+
}
344+
345+
// you can unsubscribe the subscription while the client is running
346+
client.Unsubscribe(subscriptionId)
347+
```
348+
349+
#### Authentication
350+
351+
The subscription client is authenticated with GraphQL server through connection params:
352+
353+
```Go
354+
client := graphql.NewSubscriptionClient("wss://example.com/graphql").
355+
WithConnectionParams(map[string]interface{}{
356+
"headers": map[string]string{
357+
"authentication": "...",
358+
},
359+
})
360+
361+
```
362+
363+
#### Options
364+
365+
```Go
366+
client.
367+
// write timeout of websocket client
368+
WithTimeout(time.Minute).
369+
// When the websocket server was stopped, the client will retry connecting every second until timeout
370+
WithRetryTimeout(time.Minute).
371+
// sets loging function to print out received messages. By default, nothing is printed
372+
WithLog(log.Println).
373+
// max size of response message
374+
WithReadLimit(10*1024*1024).
375+
// these operation event logs won't be printed
376+
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE)
377+
378+
```
379+
380+
#### Events
381+
382+
```Go
383+
// OnConnected event is triggered when the websocket connected to GraphQL server sucessfully
384+
client.OnConnected(fn func())
385+
386+
// OnDisconnected event is triggered when the websocket server was stil down after retry timeout
387+
client.OnDisconnected(fn func())
388+
389+
// OnConnected event is triggered when there is any connection error. This is bottom exception handler level
390+
// If this function is empty, or returns nil, the error is ignored
391+
// If returns error, the websocket connection will be terminated
392+
client.OnError(onError func(sc *SubscriptionClient, err error) error)
393+
```
394+
395+
### With operation name
396+
397+
Operatiion name is still on API decision plan https://github.com/shurcooL/graphql/issues/12. However, in my opinion separate methods are easier choice to avoid breaking changes
398+
399+
```Go
400+
func (c *Client) NamedQuery(ctx context.Context, name string, q interface{}, variables map[string]interface{})
401+
402+
func (c *Client) NamedMutate(ctx context.Context, name string, q interface{}, variables map[string]interface{})
403+
404+
func (sc *SubscriptionClient) NamedSubscribe(name string, v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error) (string, error)
405+
```
406+
281407
Directories
282408
-----------
283409

example/graphqldev/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
graphqlserver "github.com/graph-gophers/graphql-go"
1818
"github.com/graph-gophers/graphql-go/example/starwars"
1919
"github.com/graph-gophers/graphql-go/relay"
20-
"github.com/shurcooL/graphql"
20+
graphql "github.com/hasura/go-graphql-client"
2121
)
2222

2323
func main() {

example/subscription/main.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// subscription is a test program currently being used for developing graphql package.
2+
// It performs queries against a local test GraphQL server instance.
3+
//
4+
// It's not meant to be a clean or readable example. But it's functional.
5+
// Better, actual examples will be created in the future.
6+
package main
7+
8+
import (
9+
"encoding/json"
10+
"flag"
11+
"log"
12+
"os"
13+
"time"
14+
15+
graphql "github.com/hasura/go-graphql-client"
16+
)
17+
18+
func main() {
19+
flag.Parse()
20+
21+
err := run()
22+
if err != nil {
23+
panic(err)
24+
}
25+
}
26+
27+
func run() error {
28+
url := flag.Arg(0)
29+
client := graphql.NewSubscriptionClient(url).
30+
WithConnectionParams(map[string]interface{}{
31+
"headers": map[string]string{
32+
"x-hasura-admin-secret": "hasura",
33+
},
34+
}).WithLog(log.Println).
35+
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE).
36+
OnError(func(sc *graphql.SubscriptionClient, err error) error {
37+
log.Print("err", err)
38+
return err
39+
})
40+
41+
defer client.Close()
42+
43+
/*
44+
subscription($limit: Int!) {
45+
users(limit: $limit) {
46+
id
47+
name
48+
}
49+
}
50+
*/
51+
var sub struct {
52+
User struct {
53+
ID graphql.ID
54+
Name graphql.String
55+
} `graphql:"users(limit: $limit, order_by: { id: desc })"`
56+
}
57+
type Int int
58+
variables := map[string]interface{}{
59+
"limit": Int(10),
60+
}
61+
_, err := client.Subscribe(sub, variables, func(data *json.RawMessage, err error) error {
62+
63+
if err != nil {
64+
return nil
65+
}
66+
67+
time.Sleep(10 * time.Second)
68+
return nil
69+
})
70+
71+
if err != nil {
72+
panic(err)
73+
}
74+
75+
go func() {
76+
for {
77+
time.Sleep(5 * time.Second)
78+
log.Println("reseting...")
79+
go client.Reset()
80+
}
81+
}()
82+
83+
go client.Run()
84+
85+
time.Sleep(time.Minute)
86+
return nil
87+
}
88+
89+
// print pretty prints v to stdout. It panics on any error.
90+
func print(v interface{}) {
91+
w := json.NewEncoder(os.Stdout)
92+
w.SetIndent("", "\t")
93+
err := w.Encode(v)
94+
if err != nil {
95+
panic(err)
96+
}
97+
}

go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/hasura/go-graphql-client
2+
3+
go 1.14
4+
5+
require (
6+
github.com/google/uuid v1.1.2
7+
github.com/graph-gophers/graphql-go v0.0.0-20200819123640-3b5ddcd884ae
8+
golang.org/x/net v0.0.0-20200822124328-c89045814202
9+
nhooyr.io/websocket v1.8.6
10+
)

go.sum

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
5+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
6+
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
7+
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
8+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
9+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
10+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
11+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
12+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
13+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
14+
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
15+
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
16+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
17+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
18+
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
19+
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
20+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
21+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
22+
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
23+
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
24+
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
25+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
26+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
27+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
29+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
30+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
31+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
32+
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
33+
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
34+
github.com/graph-gophers/graphql-go v0.0.0-20200819123640-3b5ddcd884ae h1:TQuRfD07N7uHp+CW7rCfR579o6PDnwJacRBJH74RMq0=
35+
github.com/graph-gophers/graphql-go v0.0.0-20200819123640-3b5ddcd884ae/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
36+
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
37+
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
38+
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
39+
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
40+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
41+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
42+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
43+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
44+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
45+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
46+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
47+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
48+
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
49+
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
50+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
51+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
52+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
53+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
54+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
55+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
56+
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
57+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
58+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
59+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
60+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
61+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
62+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
63+
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
64+
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
65+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
66+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
67+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
68+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
69+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
70+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
71+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
72+
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
73+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
74+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
75+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
76+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
77+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
78+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
79+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
80+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
81+
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
82+
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=

0 commit comments

Comments
 (0)