Skip to content

A low-level GraphQL client for golang, specifically thought for testing GraphQL services

License

Notifications You must be signed in to change notification settings

lelebus/go-gqlclient

Repository files navigation

gqlclient GoDoc

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

Make sure you have a working Go environment. To install gqlclient, simply run:

$ go get github.com/lelebus/go-gqlclient

Usage

// 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

Credits

The idea comes from machinebox's repository. Seemed like an abandoned project, so I got the basic idea and built it out further.

About

A low-level GraphQL client for golang, specifically thought for testing GraphQL services

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages