Skip to content

Commit

Permalink
add more examples and subscription tests (shurcooL#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Jul 1, 2022
1 parent 391115f commit fd4ee75
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ For more information, see package [`github.com/shurcooL/githubv4`](https://githu

## Installation

`go-graphql-client` requires Go version 1.13 or later.
`go-graphql-client` requires Go version 1.16 or later. For older Go versions, downgrade the library version below v0.7.1.

```bash
go get -u github.com/hasura/go-graphql-client
Expand Down
75 changes: 75 additions & 0 deletions example/realworld/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"context"
"encoding/json"
"flag"
"log"
"net/http"
"net/http/httptest"
"os"

graphql "github.com/hasura/go-graphql-client"
)

func main() {
flag.Parse()

err := run()
if err != nil {
log.Println(err)
}
}

func run() error {

url := "https://rickandmortyapi.com/graphql"

client := graphql.NewClient(url, nil)

/*
query {
character(id: 1) {
name
}
}
*/
var q struct {
Character struct {
Name graphql.String
} `graphql:"character(id: $characterID)"`
}
variables := map[string]interface{}{
"characterID": graphql.ID("1"),
}

err := client.Query(context.Background(), &q, variables)
if err != nil {
return err
}
print(q)

return nil
}

// print pretty prints v to stdout. It panics on any error.
func print(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
handler http.Handler
}

func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)
return w.Result(), nil
}
99 changes: 99 additions & 0 deletions example/subscription/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"

graphql "github.com/hasura/go-graphql-client"
)

func getServerEndpoint() string {
return fmt.Sprintf("http://localhost:%d/graphql", httpPort)
}

func startSubscription() error {

client := graphql.NewSubscriptionClient(getServerEndpoint()).
WithConnectionParams(map[string]interface{}{
"headers": map[string]string{
"foo": "bar",
},
}).WithLog(log.Println).
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE).
OnError(func(sc *graphql.SubscriptionClient, err error) error {
log.Print("err", err)
return err
})

defer client.Close()

/*
subscription {
helloSaid {
id
msg
}
}
*/
var sub struct {
HelloSaid struct {
ID graphql.String
Message graphql.String `graphql:"msg"`
} `graphql:"helloSaid"`
}

_, err := client.Subscribe(sub, nil, func(data *json.RawMessage, err error) error {

if err != nil {
log.Println(err)
return nil
}

if data == nil {
return nil
}
log.Println(string(*data))
return nil
})

if err != nil {
panic(err)
}

return client.Run()
}

// send hello mutations to the graphql server, so the subscription client can receive messages
func startSendHello() {

client := graphql.NewClient(getServerEndpoint(), &http.Client{Transport: http.DefaultTransport})

for i := 0; i < 120; i++ {
/*
mutation ($msg: String!) {
sayHello(msg: $msg) {
id
msg
}
}
*/
var q struct {
SayHello struct {
ID graphql.String
Msg graphql.String
} `graphql:"sayHello(msg: $msg)"`
}
variables := map[string]interface{}{
"msg": graphql.String(randomID()),
}
err := client.Mutate(context.Background(), &q, variables, graphql.OperationName("SayHello"))
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second)
}
}
91 changes: 3 additions & 88 deletions example/subscription/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,8 @@
// Better, actual examples will be created in the future.
package main

import (
"encoding/json"
"flag"
"log"
"os"
"time"

graphql "github.com/hasura/go-graphql-client"
)

func main() {
flag.Parse()

err := run()
if err != nil {
panic(err)
}
}

func run() error {
url := flag.Arg(0)
client := graphql.NewSubscriptionClient(url).
WithConnectionParams(map[string]interface{}{
"headers": map[string]string{
"x-hasura-admin-secret": "hasura",
},
}).WithLog(log.Println).
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE).
OnError(func(sc *graphql.SubscriptionClient, err error) error {
log.Print("err", err)
return err
})

defer client.Close()

/*
subscription($limit: Int!) {
users(limit: $limit) {
id
name
}
}
*/
var sub struct {
User struct {
ID graphql.ID
Name graphql.String
} `graphql:"users(limit: $limit, order_by: { id: desc })"`
}
type Int int
variables := map[string]interface{}{
"limit": Int(10),
}
_, err := client.Subscribe(sub, variables, func(data *json.RawMessage, err error) error {

if err != nil {
return nil
}

time.Sleep(10 * time.Second)
return nil
})

if err != nil {
panic(err)
}

go func() {
for {
time.Sleep(5 * time.Second)
log.Println("reseting...")
go client.Reset()
}
}()

go client.Run()

time.Sleep(time.Minute)
return nil
}

// print pretty prints v to stdout. It panics on any error.
func print(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
go startServer()
go startSubscription()
startSendHello()
}
Loading

0 comments on commit fd4ee75

Please sign in to comment.