gqlclient

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: MIT Imports: 8 Imported by: 0

README

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.

Documentation

Overview

Package gqlclient provides a low level GraphQL client.

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// Log is called with various debug information.
	// To log to standard out, use:
	//  client.Log = func(s string) { log.Println(s) }
	Log func(s string)
	// contains filtered or unexported fields
}

Client is a client for interacting with a GraphQL API.

func NewClient

func NewClient(endpoint string, opts ...ClientOption) *Client

NewClient makes a new Client, optimized for GraphQL requests.

func (*Client) HttpClient

func (c *Client) HttpClient() *http.Client

HttpClient gets the underlying http.Client.

func (*Client) Run

func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) (*http.Response, error)

Run executes the query and unmarshals the response from the data field into the response object. Pass in a nil response object to skip response parsing. If the request fails or the server returns multiple errors, the first error will be returned.

type ClientOption

type ClientOption func(*Client)

ClientOption are functions that are passed into NewClient to modify the behaviour of the Client.

func ImmediatelyCloseReqBody

func ImmediatelyCloseReqBody() ClientOption

ImmediatelyCloseReqBody will close the req body immediately after each request body is ready

func UseMultipartForm

func UseMultipartForm() ClientOption

UseMultipartForm uses multipart/form-data and activates support for files.

func WithHTTPClient

func WithHTTPClient(httpclient *http.Client) ClientOption

WithHTTPClient specifies the underlying http.Client to use when making requests.

NewClient(endpoint, WithHTTPClient(specificHTTPClient))

type File

type File struct {
	Field string
	Name  string
	R     io.Reader
}

File represents a file to upload.

type Request

type Request struct {

	// Header represent any request headers that will be set
	// when the request is made.
	Header http.Header
	// contains filtered or unexported fields
}

Request is a GraphQL request struct.

func NewRequest

func NewRequest(query string) *Request

NewRequest makes a new Request with the specified string as query.

func (*Request) File

func (req *Request) File(fieldname, filename string, r io.Reader)

File sets a file to upload. Files are only supported with a Client that was created with the UseMultipartForm option.

client := gqlclient.NewClient(URL, gqlclient.UseMultipartForm())

func (*Request) Files

func (req *Request) Files() []File

Files gets the files in this request.

func (*Request) Query

func (req *Request) Query() string

Query gets the query string of this request.

func (*Request) Vars

func (req *Request) Vars() map[string]interface{}

Vars gets the variables for this Request.

func (*Request) WithVars

func (req *Request) WithVars(variables map[string]interface{}) *Request

WithVars adds variables for a Request.

// Add the variable `username` with value `lelebus`
req.WithVars(map[string]interface{}{ "username": "lelebus" })

// You would use it also like this:
req = NewRequest(query).WithVars(variables)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL