forked from sony/appsync-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appsync_example_test.go
110 lines (91 loc) · 2.48 KB
/
appsync_example_test.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
package appsync_test
import (
"encoding/json"
"fmt"
"log"
"github.com/sony/appsync-client-go/internal/appsynctest"
appsync "github.com/sony/appsync-client-go"
"github.com/sony/appsync-client-go/graphql"
)
func ExampleClient_Post_query() {
server := appsynctest.NewAppSyncEchoServer()
defer server.Close()
query := `query Message() { message }`
client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
response, err := client.Post(graphql.PostRequest{
Query: query,
})
if err != nil {
log.Fatal(err)
}
data := new(string)
if err := response.DataAs(data); err != nil {
log.Fatalln(err, response)
}
fmt.Println(*data)
// Output:
// Hello, AppSync!
}
func ExampleClient_Post_mutation() {
server := appsynctest.NewAppSyncEchoServer()
defer server.Close()
client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
mutation := `mutation Echo($message: String!) { echo(message: $message) }`
variables := json.RawMessage(fmt.Sprintf(`{ "message": "%s" }`, "Hi, AppSync!"))
response, err := client.Post(graphql.PostRequest{
Query: mutation,
Variables: &variables,
})
if err != nil {
log.Fatal(err)
}
data := new(string)
if err := response.DataAs(data); err != nil {
log.Fatalln(err, response)
}
fmt.Println(*data)
// Output:
// Hi, AppSync!
}
func ExampleClient_Post_subscription() {
server := appsynctest.NewAppSyncEchoServer()
defer server.Close()
client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(server.URL)))
subscription := `subscription SubscribeToEcho() { subscribeToEcho }`
response, err := client.Post(graphql.PostRequest{
Query: subscription,
})
if err != nil {
log.Fatal(err)
}
ext, err := appsync.NewExtensions(response)
if err != nil {
log.Fatalln(err)
}
ch := make(chan *graphql.Response)
subscriber := appsync.NewSubscriber(*ext,
func(r *graphql.Response) { ch <- r },
func(err error) { log.Println(err) },
)
if err := subscriber.Start(); err != nil {
log.Fatalln(err)
}
defer subscriber.Stop()
mutation := `mutation Echo($message: String!) { echo(message: $message) }`
variables := json.RawMessage(fmt.Sprintf(`{ "message": "%s" }`, "Hi, AppSync!"))
_, err = client.Post(graphql.PostRequest{
Query: mutation,
Variables: &variables,
})
if err != nil {
log.Fatal(err)
}
response = <-ch
data := new(string)
if err := response.DataAs(data); err != nil {
log.Fatalln(err, response)
}
fmt.Println(*data)
// Output:
// Hi, AppSync!
}