transport

package
v0.17.94 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 22 Imported by: 605

Documentation

Index

Constants

View Source
const (
	// WebsocketCloseNormalClosure is the RFC 6455 normal closure status code.
	WebsocketCloseNormalClosure = 1000
	// WebsocketCloseNoStatusReceived is the RFC 6455 no-status-received code.
	WebsocketCloseNoStatusReceived = 1005
	// WebsocketCloseProtocolError is the RFC 6455 protocol error status code.
	WebsocketCloseProtocolError = 1002
)

Variables

View Source
var ErrWebsocketClosed = errors.New("websocket connection closed")

ErrWebsocketClosed indicates that the peer closed the websocket connection.

Functions

func AddSubscriptionError added in v0.17.23

func AddSubscriptionError(ctx context.Context, err *gqlerror.Error)

AddSubscriptionError is used to let websocket return an error message after subscription resolver returns a channel. for example:

func (r *subscriptionResolver) Method(ctx context.Context) (<-chan *model.Message, error) {
	ch := make(chan *model.Message)
	go func() {
     defer func() {
			close(ch)
     }
		// some kind of block processing (e.g.: gRPC client streaming)
		stream, err := gRPCClientStreamRequest(ctx)
		if err != nil {
			   transport.AddSubscriptionError(ctx, err)
            return // must return and close channel so websocket can send error back
     }
		for {
			m, err := stream.Recv()
			if err == io.EOF {
				return
			}
			if err != nil {
			   transport.AddSubscriptionError(ctx, err)
            return // must return and close channel so websocket can send error back
			}
			ch <- m
		}
	}()

	return ch, nil
}

see https://github.com/99designs/gqlgen/pull/2506 for more details

func AppendCloseReason added in v0.15.0

func AppendCloseReason(ctx context.Context, reason string) context.Context

func SendError

func SendError(w http.ResponseWriter, code int, errors ...*gqlerror.Error)

SendError sends a best effort error to a raw response writer. It assumes the client can understand the standard json error response

func SendErrorf

func SendErrorf(w http.ResponseWriter, code int, format string, args ...any)

SendErrorf wraps SendError to add formatted messages

func WithWebsocketCloseCode added in v0.17.91

func WithWebsocketCloseCode(ctx context.Context, v int) context.Context

Types

type CoderWebsocketImplementation added in v0.17.91

type CoderWebsocketImplementation struct {
	AcceptOptions coderws.AcceptOptions
}

CoderWebsocketImplementation adapts github.com/coder/websocket to the gqlgen websocket transport.

func (CoderWebsocketImplementation) Accept added in v0.17.91

type GET

type GET struct {
	// Map of all headers that are added to graphql response. If not
	// set, only one header: Content-Type: application/graphql-response+json will be set.
	ResponseHeaders map[string][]string
	// UseGrapQLResponseJsonByDefault determines whether to use 'application/graphql-response+json'
	// as the response content type
	// when the Accept header is empty or 'application/*' or '*/*'.
	UseGrapQLResponseJsonByDefault bool
}

GET implements the GET side of the default HTTP transport defined in https://github.com/APIs-guru/graphql-over-http#get

func (GET) Do

func (GET) Supports

func (h GET) Supports(r *http.Request) bool

type GRAPHQL added in v0.17.29

type GRAPHQL struct {
	// Map of all headers that are added to graphql response. If not
	// set, only one header: Content-Type: application/json will be set.
	ResponseHeaders map[string][]string
}

GRAPHQL implements the application/graphql side of the HTTP transport see: https://graphql.org/learn/serving-over-http/#post-request If the "application/graphql" Content-Type header is present, treat the HTTP POST body contents as the GraphQL query string.

func (GRAPHQL) Do added in v0.17.29

func (GRAPHQL) Supports added in v0.17.29

func (h GRAPHQL) Supports(r *http.Request) bool

type InitPayload

type InitPayload map[string]any

InitPayload is a structure that is parsed from the websocket init message payload. TO use request headers for non-websocket, instead wrap the graphql handler in a middleware.

func GetInitPayload

func GetInitPayload(ctx context.Context) InitPayload

GetInitPayload gets a map of the data sent with the connection_init message, which is used by graphql clients as a stand-in for HTTP headers.

func (InitPayload) Authorization

func (p InitPayload) Authorization() string

Authorization is a short hand for getting the Authorization header from the payload.

func (InitPayload) GetString

func (p InitPayload) GetString(key string) string

GetString safely gets a string value from the payload. It returns an empty string if the payload is nil or the value isn't set.

type MultipartForm

type MultipartForm struct {
	// MaxUploadSize sets the maximum number of bytes used to parse a request body
	// as multipart/form-data.
	MaxUploadSize int64

	// MaxMemory defines the maximum number of bytes used to parse a request body
	// as multipart/form-data in memory, with the remainder stored on disk in
	// temporary files.
	MaxMemory int64

	// Map of all headers that are added to graphql response. If not
	// set, only one header: Content-Type: application/json will be set.
	ResponseHeaders map[string][]string
}

MultipartForm the Multipart request spec https://github.com/jaydenseric/graphql-multipart-request-spec

func (MultipartForm) Do

func (MultipartForm) Supports

func (f MultipartForm) Supports(r *http.Request) bool

type MultipartMixed added in v0.17.56

type MultipartMixed struct {
	Boundary        string
	DeliveryTimeout time.Duration
}

MultipartMixed is a transport that supports the multipart/mixed spec

func (MultipartMixed) Do added in v0.17.56

Do implements the multipart/mixed spec as a multipart/mixed response

func (MultipartMixed) Supports added in v0.17.56

func (t MultipartMixed) Supports(r *http.Request) bool

Supports checks if the request supports the multipart/mixed spec Might be worth check the spec required, but Apollo Client mislabel the spec in the headers.

type Options

type Options struct {
	// AllowedMethods is a list of allowed HTTP methods.
	AllowedMethods []string
}

Options responds to http OPTIONS and HEAD requests

func (Options) Do

func (Options) Supports

func (o Options) Supports(r *http.Request) bool

type POST

type POST struct {
	// Map of all headers that are added to graphql response. If not
	// set, only one header: Content-Type: application/graphql-response+json will be set.
	ResponseHeaders map[string][]string

	// UseGrapQLResponseJsonByDefault determines whether to use 'application/graphql-response+json'
	// as the response content type
	// when the Accept header is empty or 'application/*' or '*/*'.
	UseGrapQLResponseJsonByDefault bool
}

POST implements the POST side of the default HTTP transport defined in https://github.com/APIs-guru/graphql-over-http#post

func (POST) Do

func (POST) Supports

func (h POST) Supports(r *http.Request) bool

type SSE added in v0.17.23

type SSE struct {
	KeepAlivePingInterval time.Duration
	// MinEventInterval optionally paces SSE event frames for high-throughput streams.
	MinEventInterval time.Duration
}

func (SSE) Do added in v0.17.23

func (SSE) Supports added in v0.17.23

func (t SSE) Supports(r *http.Request) bool

type UrlEncodedForm added in v0.17.29

type UrlEncodedForm struct {
	// Map of all headers that are added to graphql response. If not
	// set, only one header: Content-Type: application/json will be set.
	ResponseHeaders map[string][]string
}

FORM implements the application/x-www-form-urlencoded side of the default HTTP transport

func (UrlEncodedForm) Do added in v0.17.29

func (UrlEncodedForm) Supports added in v0.17.29

func (h UrlEncodedForm) Supports(r *http.Request) bool

type Websocket

type Websocket struct {
	// Implementation accepts HTTP requests as websocket connections. If nil,
	// gqlgen uses a CoderWebsocketImplementation with zero-valued accept
	// options.
	Implementation WebsocketImplementation

	InitFunc              WebsocketInitFunc
	InitTimeout           time.Duration
	ErrorFunc             WebsocketErrorFunc
	CloseFunc             WebsocketCloseFunc
	KeepAlivePingInterval time.Duration
	PongOnlyInterval      time.Duration
	PingPongInterval      time.Duration
	/* If PingPongInterval has a non-0 duration, then when the server sends a ping
	 * it sets a ReadDeadline of PingPongInterval*2 and if the client doesn't respond
	 * with pong before that deadline is reached then the connection will die with a
	 * 1006 error code.
	 *
	 * MissingPongOk if true, tells the server to not use a ReadDeadline such that a
	 * missing/slow pong response from the client doesn't kill the connection.
	 */
	MissingPongOk bool

	// PayloadReadLimit is the maximum size in bytes of a WebSocket message read from the client.
	// If nil, defaults to 1 MB. Set to a pointer of -1 to disable the limit entirely.
	// Custom websocket adapters must implement WebsocketReadLimiter to enforce this limit.
	PayloadReadLimit *int64
}

Websocket transport configuration.

Note: when an OperationInterceptor returns an error instead of calling next(), it must use graphql.OneShot to ensure the transport doesn't infinitely loop on a continuous stream of errors.

func (Websocket) Do

func (Websocket) Supports

func (t Websocket) Supports(r *http.Request) bool

type WebsocketAcceptOptions added in v0.17.91

type WebsocketAcceptOptions struct {
	// ResponseHeader is passed back with the HTTP 101 response.
	ResponseHeader http.Header
	// Subprotocols are the GraphQL websocket subprotocols supported by gqlgen.
	// Implementations should merge these with their own implementation-specific
	// subprotocol configuration when negotiating the websocket connection.
	Subprotocols []string
}

WebsocketAcceptOptions contains gqlgen websocket transport options that an implementation adapter can apply while accepting a websocket connection.

type WebsocketCloseFunc added in v0.17.29

type WebsocketCloseFunc func(ctx context.Context, closeCode int)

Callback called when websocket is closed.

type WebsocketConn added in v0.17.91

type WebsocketConn interface {
	io.Closer
	NextReader() (messageType int, r io.Reader, err error)
	WriteJSON(v any) error
	WriteClose(closeCode int, message string) error
	Subprotocol() string
}

WebsocketConn is the websocket connection behavior required by the GraphQL websocket transport. Adapters for websocket libraries should return ErrWebsocketClosed from NextReader when the peer performs a normal close.

type WebsocketError added in v0.17.6

type WebsocketError struct {
	Err error

	// IsReadError flags whether the error occurred on read or write to the websocket
	IsReadError bool
}

func (WebsocketError) Error added in v0.17.6

func (e WebsocketError) Error() string

func (WebsocketError) Unwrap added in v0.17.91

func (e WebsocketError) Unwrap() error

type WebsocketErrorFunc added in v0.17.0

type WebsocketErrorFunc func(ctx context.Context, err error)

type WebsocketImplementation added in v0.17.91

type WebsocketImplementation interface {
	Accept(
		w http.ResponseWriter,
		r *http.Request,
		options WebsocketAcceptOptions,
	) (WebsocketConn, error)
}

WebsocketImplementation accepts an HTTP request as a websocket connection.

type WebsocketInitFunc

type WebsocketInitFunc func(ctx context.Context, initPayload InitPayload) (context.Context, *InitPayload, error)

type WebsocketReadDeadliner added in v0.17.91

type WebsocketReadDeadliner interface {
	SetReadDeadline(t time.Time) error
}

WebsocketReadDeadliner is an optional interface implemented by websocket connections that can enforce read deadlines. If an adapter does not implement this interface, PingPongInterval can send pings but cannot enforce missing pong timeouts.

type WebsocketReadLimiter added in v0.17.91

type WebsocketReadLimiter interface {
	SetReadLimit(limit int64)
}

WebsocketReadLimiter is an optional interface implemented by websocket connections that can enforce a maximum read size. If an adapter does not implement this interface, the transport does not enforce PayloadReadLimit.

Jump to

Keyboard shortcuts

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