Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lelebus/go-gqlclient
A low-level GraphQL client for golang, specifically thought for testing GraphQL services
https://github.com/lelebus/go-gqlclient
golang golang-package golang-testing graphql-client
Last synced: about 1 month ago
JSON representation
A low-level GraphQL client for golang, specifically thought for testing GraphQL services
- Host: GitHub
- URL: https://github.com/lelebus/go-gqlclient
- Owner: lelebus
- License: mit
- Created: 2023-04-21T20:45:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-06-01T19:58:42.000Z (over 1 year ago)
- Last Synced: 2024-10-18T15:36:13.403Z (2 months ago)
- Topics: golang, golang-package, golang-testing, graphql-client
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gqlclient [![GoDoc](https://godoc.org/github.com/lelebus/go-gqlclient?status.png)](https://godoc.org/github.com/lelebus/go-gqlclient)
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## Table of contents
- [Getting started](#getting-started)
- [Usage](#usage)
- [Credits](#credits)## Getting started
Make sure you have a working Go environment. To install gqlclient, simply run:
```
$ go get github.com/lelebus/go-gqlclient
```## Usage
```go
// 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()
```### File support via multipart form data
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](https://godoc.org/github.com/lelebus/go-gqlclient)
## Credits
The idea comes from [machinebox's repository](https://github.com/machinebox/graphql). Seemed like an abandoned project, so I got the basic idea and built it out further.