Skip to content

Commit a266523

Browse files
committed
added some error handling help
1 parent 0b6016c commit a266523

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ Low-level GraphQL client for Go.
55
* Simple, familiar API
66
* Respects context.Context cancallation
77
* Build and execute any kind of GraphQL request
8+
* Use variables and upload files
89

910
```go
1011
ctx := context.Background()
1112
ctx := graphql.NewContext(ctx, "https://machinebox.io/graphql")
12-
r := graphql.NewRequest(`
13+
req := graphql.NewRequest(`
1314
query ($key: String!) {
1415
items (id:$key) {
1516
field1
@@ -18,9 +19,9 @@ r := graphql.NewRequest(`
1819
}
1920
}
2021
`)
21-
r.Var("key", "value")
22+
req.Var("key", "value")
2223
var res ResponseStruct
23-
if err := r.Run(ctx, &resp); err != nil {
24+
if err := req.Run(ctx, &resp); err != nil {
2425
log.Fatalln(err)
2526
}
2627
```

client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ func NewRequest(q string) *Request {
127127
return req
128128
}
129129

130-
// Run executes the query and unmarshals the response into response.
130+
// Run executes the query and unmarshals the response from the data field
131+
// into the response object.
132+
// Pass in a nil response object to skip response parsing.
133+
// If the request fails or the server returns an error, the first error
134+
// will be returned. Use IsGraphQLErr to determine which it was.
131135
func (req *Request) Run(ctx context.Context, response interface{}) error {
132136
client, err := fromContext(ctx)
133137
if err != nil {
@@ -152,6 +156,13 @@ func (req *Request) File(filename string, r io.Reader) {
152156
})
153157
}
154158

159+
// IsGraphQLErr gets whether the error is a remote GraphQL
160+
// server error or not.
161+
func IsGraphQLErr(err error) bool {
162+
_, ok := err.(graphErr)
163+
return ok
164+
}
165+
155166
// file represents a file to upload.
156167
type file struct {
157168
Name string

0 commit comments

Comments
 (0)