package client

import "github.com/99designs/gqlgen/client"

Index

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client used for testing GraphQL servers. Not for production use.

func New

func New(h http.Handler, opts ...Option) *Client

New creates a graphql client Options can be set that should be applied to all requests made with this client

func (*Client) IncrementalHTTP

func (p *Client) IncrementalHTTP(ctx context.Context, query string, options ...Option) *IncrementalHandler

IncrementalHTTP returns a GraphQL response handler for the current GQLGen implementation of the incremental delivery over HTTP spec. The IncrementalHTTP spec provides for "streaming" responses triggered by the use of @stream or @defer as an alternate approach to SSE. To that end, the client retains the interface of the handler returned from Client.SSE.

IncrementalHTTP delivery using multipart/mixed is just the structure of the response: the payloads are specified by the defer-stream spec, which are in transition. For more detail, see the links in the definition for transport.MultipartMixed. We use the name IncrementalHTTP here to distinguish from the multipart form upload (the term "multipart" usually referring to the latter).

IncrementalHandler is not safe for concurrent use, or for production use at all.

func (*Client) MustPost

func (p *Client) MustPost(query string, response any, options ...Option)

MustPost is a convenience wrapper around Post that automatically panics on error

func (*Client) Post

func (p *Client) Post(query string, response any, options ...Option) error

Post sends a http POST request to the graphql endpoint with the given query then unpacks the response into the given object.

func (*Client) RawPost

func (p *Client) RawPost(query string, options ...Option) (*Response, error)

RawPost is similar to Post, except it skips decoding the raw json response unpacked onto Response. This is used to test extension keys which are not available when using Post.

func (*Client) SSE

func (p *Client) SSE(ctx context.Context, query string, options ...Option) *SSE

func (*Client) SetCustomDecodeConfig

func (p *Client) SetCustomDecodeConfig(dc *mapstructure.DecoderConfig)

SetCustomDecodeConfig sets a custom decode hook for the client

func (*Client) SetCustomTarget

func (p *Client) SetCustomTarget(target string)

SetCustomTarget sets a custom target path for the client

func (*Client) Websocket

func (p *Client) Websocket(query string, options ...Option) *Subscription

func (*Client) WebsocketOnce

func (p *Client) WebsocketOnce(query string, resp any, options ...Option) error

Grab a single response from a websocket based query

func (*Client) WebsocketWithPayload

func (p *Client) WebsocketWithPayload(query string, initPayload map[string]any, options ...Option) *Subscription

type IncrementalData

type IncrementalData struct {
	Data       any             `json:"data"`
	Label      string          `json:"label"`
	Path       []any           `json:"path"`
	HasNext    bool            `json:"hasNext"`
	Errors     json.RawMessage `json:"errors"`
	Extensions map[string]any  `json:"extensions"`
}

type IncrementalHandler

type IncrementalHandler struct {
	// contains filtered or unexported fields
}

func (*IncrementalHandler) Close

func (i *IncrementalHandler) Close() error

func (*IncrementalHandler) Next

func (i *IncrementalHandler) Next(response any) error

type IncrementalInitialResponse

type IncrementalInitialResponse struct {
	Data       any             `json:"data"`
	Label      string          `json:"label"`
	Path       []any           `json:"path"`
	HasNext    bool            `json:"hasNext"`
	Errors     json.RawMessage `json:"errors"`
	Extensions map[string]any  `json:"extensions"`
}

type IncrementalResponse

type IncrementalResponse struct {
	Incremental []IncrementalData `json:"incremental"`
	HasNext     bool              `json:"hasNext"`
	Errors      json.RawMessage   `json:"errors"`
	Extensions  map[string]any    `json:"extensions"`
}

type Option

type Option func(bd *Request)

Option implements a visitor that mutates an outgoing GraphQL request

This is the Option pattern - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

func AddCookie

func AddCookie(cookie *http.Cookie) Option

AddCookie adds a cookie to the outgoing request

func AddHeader

func AddHeader(key, value string) Option

AddHeader adds a header to the outgoing request. This is useful for setting expected Authentication headers for example.

func BasicAuth

func BasicAuth(username, password string) Option

BasicAuth authenticates the request using http basic auth.

func Extensions

func Extensions(extensions map[string]any) Option

Extensions sets the extensions to be sent with the outgoing request

func Operation

func Operation(name string) Option

Operation sets the operation name for the outgoing request

func Path

func Path(url string) Option

Path sets the url that this request will be made against, useful if you are mounting your entire router and need to specify the url to the graphql endpoint.

func Var

func Var(name string, value any) Option

Var adds a variable into the outgoing request

func WithFiles

func WithFiles() Option

WithFiles encodes the outgoing request body as multipart form data for file variables

type RawJsonError

type RawJsonError struct {
	json.RawMessage
}

RawJsonError is a json formatted error from a GraphQL server.

func (RawJsonError) Error

func (r RawJsonError) Error() string

type Request

type Request struct {
	Query         string         `json:"query"`
	Variables     map[string]any `json:"variables,omitempty"`
	OperationName string         `json:"operationName,omitempty"`
	Extensions    map[string]any `json:"extensions,omitempty"`
	HTTP          *http.Request  `json:"-"`
}

Request represents an outgoing GraphQL request

type Response

type Response struct {
	Data       any
	Errors     json.RawMessage
	Extensions map[string]any
}

Response is a GraphQL layer response from a handler.

type SSE

type SSE struct {
	Close func() error
	Next  func(response any) error
}

type SSEResponse

type SSEResponse struct {
	Data       any             `json:"data"`
	Label      string          `json:"label"`
	Path       []any           `json:"path"`
	HasNext    bool            `json:"hasNext"`
	Errors     json.RawMessage `json:"errors"`
	Extensions map[string]any  `json:"extensions"`
}

type Subscription

type Subscription struct {
	Close func() error
	Next  func(response any) error
}

Source Files

client.go errors.go incremental_http.go options.go sse.go websocket.go withfilesoption.go

Version
v0.17.74 (latest)
Published
Jun 2, 2025
Platform
linux/amd64
Imports
18 packages
Last checked
4 days ago

Tools for package owners.