Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.