forked from shurcooL/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more examples and subscription tests (shurcooL#37)
- Loading branch information
Showing
8 changed files
with
625 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.