Low-level GraphQL client for Go, specifically thought for testing of GraphQL services.
- Simple, familiar API
- Respects
context.Context
timeouts and cancellation - Build and execute any kind of GraphQL request
- Use strong Go types for response data
- Use variables and upload files
- Simple error handling
Make sure you have a working Go environment. To install gqlclient, simply run:
$ go get github.com/lelebus/go-gqlclient
// create a client (safe to share across requests)
client := gqlclient.NewClient("http://localhost:4000/graphql")
// to specify your own http.Client, use the WithHTTPClient option:
customHttpClient := &http.Client{}
customClient := gqlclient.NewClient("http://localhost:4000/graphql", gqlclient.WithHTTPClient(customHttpClient))
// make a request
req := gqlclient.NewRequest(`
query {
items {
field1
field2
field3
}
}
`)
// make a request with variables
req := gqlclient.NewRequest(`
query ($key: String!) {
items (id:$key) {
field1
field2
field3
}
}
`).WithVars(map[string]interface{}{
"key": "value",
})
// run it and capture the response
var responseData map[string]interface{}
res, err := client.Run(ctx, req, &responseData)
if err != nil {
log.Fatal(err)
}
// read the cookies in the response
cookies := res.Cookies()
By default, the package will send a JSON body. To enable the sending of files, you can opt to
use multipart form data instead using the UseMultipartForm
option when you create your Client
:
client := gqlclient.NewClient("http://localhost:4000/graphql", gqlclient.UseMultipartForm())
For more information, read the godoc package documentation
The idea comes from machinebox's repository. Seemed like an abandoned project, so I got the basic idea and built it out further.