gqlclient

package
v0.0.0-...-442daeb Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package graphql provides a low level GraphQL client.

// create a client (safe to share across requests)
client := graphql.NewClient("https://machinebox.io/graphql")

// make a request
req := graphql.NewRequest(`
    query ($key: String!) {
        items (id:$key) {
            field1
            field2
            field3
        }
    }
`)

// set any variables
req.Var("key", "value")

// run it and capture the response
var respData ResponseStruct
if err := client.Run(ctx, req, &respData); err != nil {
    log.Fatal(err)
}

Specify client

To specify your own http.Client, use the WithHTTPClient option:

httpclient := &http.Client{}
client := graphql.NewClient("https://machinebox.io/graphql", graphql.WithHTTPClient(httpclient))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BearerTokenProvider

type BearerTokenProvider interface {
	BearerToken() string
}

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 New

func New(url string, timeout time.Duration) *Client

func NewWithOpts

func NewWithOpts(url string, timeout time.Duration, opts ...ClientOption) *Client

func (*Client) EnableDebugLog

func (c *Client) EnableDebugLog()

func (*Client) Run

func (c *Client) Run(request Request, response interface{}) error

func (*Client) RunWithContext

func (c *Client) RunWithContext(ctx context.Context, req Request, resp interface{}) (rerr error)

RunWithContext 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 an error, the first error will be returned.

func (*Client) SetTokenProvider

func (c *Client) SetTokenProvider(tokenProvider BearerTokenProvider)

type ClientOption

type ClientOption func(*Client)

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

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 GraphErr

type GraphErr struct {
	Message    string                 `json:"message"`
	Extensions map[string]interface{} `json:"extensions"`
}

func (GraphErr) Error

func (e GraphErr) Error() string

type PostProcessor

type PostProcessor interface {
	PostProcess() error
}

type Request

type Request interface {
	Query() string
	Vars() (map[string]interface{}, error)
}

type RequestError

type RequestError struct {
	Request Request
	// contains filtered or unexported fields
}

func NewRequestError

func NewRequestError(err error, req Request) *RequestError

func (*RequestError) Error

func (e *RequestError) Error() string

func (*RequestError) Unwrap

func (e *RequestError) Unwrap() error

type RequestWithFiles

type RequestWithFiles interface {
	Request
	Files() []File
}

type RequestWithHeaders

type RequestWithHeaders interface {
	Request
	Headers() map[string][]string
}

type StandardizedErrors

type StandardizedErrors struct {
	Message string
	Error   string
	Errors  []graphErr
}

StandardizedErrors works around API's that don't follow the graphql standard It looks redundant because it needs to address two different API responses. https://activestatef.atlassian.net/browse/PB-4291

func (StandardizedErrors) HasErrors

func (e StandardizedErrors) HasErrors() bool

func (StandardizedErrors) Values

func (e StandardizedErrors) Values() []string

Values tells us all the relevant error messages returned. We don't include e.Error because it's an unhelpful generic error code redundant with the message.

Jump to

Keyboard shortcuts

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