clientv2

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 17 Imported by: 12 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

func MarshalJSON(ctx context.Context, v any) ([]byte, error)

MarshalJSON encodes v to JSON with a default Encoder.

Arguments:

  • ctx: passed to MarshalGQLContext of values implementing graphql.ContextMarshaler
  • v: the value to encode; nil is encoded as null

Returns:

  • []byte: the JSON encoding of v
  • error: non-nil if v or one of its elements cannot be encoded

Preconditions:

  • none

Postconditions:

  • nil slices are encoded as null; use a Client with Options.EncodeNilSliceAsEmptyArray to encode them as []

Types

type Client

type Client struct {
	Client                     HttpClient
	BaseURL                    string
	RequestInterceptor         RequestInterceptor
	CustomDo                   RequestInterceptorFunc
	ParseDataWhenErrors        bool
	IsUnsafeRequestInterceptor bool
	EncodeNilSliceAsEmptyArray bool
}

Client is the http client wrapper

func NewClient

func NewClient(client HttpClient, baseURL string, options *Options, interceptors ...RequestInterceptor) *Client

NewClient creates a new http client wrapper

func NewClientWithUnsafeRequestInterceptor

func NewClientWithUnsafeRequestInterceptor(client HttpClient, baseURL string, options *Options, interceptors ...RequestInterceptor) *Client

func (*Client) Post

func (c *Client) Post(ctx context.Context, operationName, query string, respData any, vars map[string]any, interceptors ...RequestInterceptor) error

Post support send multipart form with files https://gqlgen.com/reference/file-upload/ https://github.com/jaydenseric/graphql-multipart-request-spec

type Encoder

type Encoder struct {
	// NilSliceAsEmptyArray encodes nil slices as [] instead of null.
	// See Options.EncodeNilSliceAsEmptyArray for details.
	NilSliceAsEmptyArray bool
}

Encoder encodes GraphQL request bodies and variables to JSON.

It delegates to encoding/json/v2 configured with encoding/json v1 semantics (omitempty on Go emptiness, HTML escaping, sorted map keys, nil maps as null) and layers the gqlgen marshaler conventions on top: values implementing graphql.ContextMarshaler or graphql.Marshaler are encoded with MarshalGQLContext or MarshalGQL, which take precedence over json.Marshaler and encoding.TextMarshaler.

func (*Encoder) Encode

func (e *Encoder) Encode(v reflect.Value) ([]byte, error)

Encode encodes a reflect.Value to JSON.

Arguments:

  • v: the value to encode; an invalid reflect.Value is encoded as null

Returns:

  • []byte: the JSON encoding of v
  • error: non-nil if v or one of its elements cannot be encoded

Preconditions:

  • v must be obtained from an exported value so that v.Interface() does not panic

Postconditions:

  • values implementing graphql.ContextMarshaler receive context.Background(); use MarshalJSON to pass a context

type ErrorResponse

type ErrorResponse struct {
	// populated when http status code is not OK
	NetworkError *HTTPError `json:"networkErrors"`
	// populated when http status code is OK but the server returned at least one graphql error
	GqlErrors *gqlerror.List `json:"graphqlErrors"`
}

ErrorResponse represent an handled error

func (*ErrorResponse) Error

func (er *ErrorResponse) Error() string

func (*ErrorResponse) HasErrors

func (er *ErrorResponse) HasErrors() bool

HasErrors returns true when at least one error is declared

type FormField

type FormField struct {
	Name  string
	Value any
}

type GQLRequestInfo

type GQLRequestInfo struct {
	Request *Request
}

func NewGQLRequestInfo

func NewGQLRequestInfo(r *Request) *GQLRequestInfo

type GqlErrorList

type GqlErrorList struct {
	Errors gqlerror.List `json:"errors"`
}

GqlErrorList is the struct of a standard graphql error response

func (*GqlErrorList) Error

func (e *GqlErrorList) Error() string

type HTTPError

type HTTPError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

HTTPError is the error when a GqlErrorList cannot be parsed

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
	Post(url, contentType string, body io.Reader) (*http.Response, error)
}

type MultipartFile

type MultipartFile struct {
	File  graphql.Upload
	Index int
}

type MultipartFilesGroup

type MultipartFilesGroup struct {
	Files      []MultipartFile
	IsMultiple bool
}

type Options

type Options struct {
	// ParseDataAlongWithErrors is a flag that indicates whether the client should try to parse and return the data along with error
	// when error appeared. So in the end you'll get list of gql errors and data.
	ParseDataAlongWithErrors bool
	// EncodeNilSliceAsEmptyArray is a flag that indicates whether the client should encode nil slices
	// in request variables as [] instead of null. This restores the encoding behavior prior to
	// https://github.com/gqlgo/gqlgenc/pull/265, which is useful when a server schema declares
	// non-null list inputs (e.g. [Item!]!) that reject null.
	EncodeNilSliceAsEmptyArray bool
}

Options is a struct that holds some client-specific options that can be passed to NewClient.

type Request

type Request struct {
	Query         string         `json:"query"`
	Variables     map[string]any `json:"variables,omitempty"`
	OperationName string         `json:"operationName,omitempty"`
}

Request represents an outgoing GraphQL request

type RequestInterceptor

type RequestInterceptor func(ctx context.Context, req *http.Request, gqlInfo *GQLRequestInfo, res any, next RequestInterceptorFunc) error

func ChainInterceptor

func ChainInterceptor(interceptors ...RequestInterceptor) RequestInterceptor

func UnsafeChainInterceptor

func UnsafeChainInterceptor(interceptors ...RequestInterceptor) RequestInterceptor

type RequestInterceptorFunc

type RequestInterceptorFunc func(ctx context.Context, req *http.Request, gqlInfo *GQLRequestInfo, res any) error

Jump to

Keyboard shortcuts

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