graphqltransportws

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CloseDeadlineDuration time.Duration = 100 * time.Millisecond
)

Functions

func NewConnection

func NewConnection(ctx context.Context, config Config) (*wsConnection, error)

NewConnection establishes a GraphQL WebSocket connection. It implements the GraphQL WebSocket protocol by managing its internal state and handling the client-server communication.

Types

type CloseCode

type CloseCode int

CloseCode a closing code

const (
	// Subprotocol - https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
	// this is the successor protocol to graphql-ws which is confusing because the
	// library for the spec is graphql-ws
	Subprotocol = "graphql-transport-ws"

	// Close codes
	Noop                             CloseCode = -1
	NormalClosure                    CloseCode = 1000
	InternalServerError              CloseCode = 4500
	InternalClientError              CloseCode = 4005
	BadRequest                       CloseCode = 4400
	BadResponse                      CloseCode = 4004
	Unauthorized                     CloseCode = 4401
	Forbidden                        CloseCode = 4403
	SubprotocolNotAcceptable         CloseCode = 4406
	ConnectionInitialisationTimeout  CloseCode = 4408
	ConnectionAcknowledgementTimeout CloseCode = 4504
	SubscriberAlreadyExists          CloseCode = 4409
	TooManyInitialisationRequests    CloseCode = 4429

	// Thresholds
	WriteTimeout = 10 * time.Second
)

type CompleteMessage

type CompleteMessage struct {
	ID   string               `json:"id"`
	Type protocol.MessageType `json:"type"`
}

type Config

type Config struct {
	WS                        *websocket.Conn
	Schema                    *graphql.Schema
	Logger                    *logger.LogWrapper
	Request                   *http.Request
	ConnectionInitWaitTimeout time.Duration
	RootValueFunc             func(ctx context.Context, r *http.Request, op *ast.OperationDefinition) map[string]interface{}
	ContextValueFunc          func(c protocol.Context, msg protocol.OperationMessage, execArgs graphql.Params) (context.Context, gqlerrors.FormattedErrors)
	OnConnect                 func(c protocol.Context) (interface{}, error)
	OnPing                    func(c protocol.Context, payload map[string]interface{})
	OnPong                    func(c protocol.Context, payload map[string]interface{})
	OnDisconnect              func(c protocol.Context, code CloseCode, reason string)
	OnClose                   func(c protocol.Context, code CloseCode, reason string)
	OnSubscribe               func(c protocol.Context, msg SubscribeMessage) (*graphql.Params, gqlerrors.FormattedErrors)
	OnNext                    func(c protocol.Context, msg NextMessage, args graphql.Params, Result *graphql.Result) (*protocol.ExecutionResult, error)
	OnError                   func(c protocol.Context, msg ErrorMessage, errs gqlerrors.FormattedErrors) (gqlerrors.FormattedErrors, error)
	OnComplete                func(c protocol.Context, msg CompleteMessage) error
	OnOperation               func(c protocol.Context, msg SubscribeMessage, args graphql.Params, result interface{}) (interface{}, error)
}

ConnectionConfig defines the configuration parameters of a GraphQL WebSocket connection.

type ErrorMessage

type ErrorMessage struct {
	ID      string                    `json:"id"`
	Type    protocol.MessageType      `json:"type"`
	Payload gqlerrors.FormattedErrors `json:"payload"`
}

ErrorMessage

type NextMessage

type NextMessage struct {
	ID      string                   `json:"id"`
	Type    protocol.MessageType     `json:"type"`
	Payload protocol.ExecutionResult `json:"payload"`
}

type RawMessage

type RawMessage map[string]interface{}

RawMessage is the raw message data

func (RawMessage) HasPayload

func (m RawMessage) HasPayload() bool

HasPayload returns true if the payload field exists and is not null

func (RawMessage) ID

func (m RawMessage) ID() (string, error)

ID validates and extracts the id field value from a raw message

func (RawMessage) Payload

func (m RawMessage) Payload() interface{}

Payload returns the raw payload

func (RawMessage) RecordPayload

func (m RawMessage) RecordPayload() (map[string]interface{}, error)

PayloadRecord converts the payload to a record

func (RawMessage) SubscribePayload

func (m RawMessage) SubscribePayload() (*SubscribePayload, error)

SubscribePayload converts the payload to a subscribe payload

func (RawMessage) Type

func (m RawMessage) Type() (protocol.MessageType, error)

Type validates and extracts the type field value from a raw message

type SubscribeMessage

type SubscribeMessage struct {
	ID      string               `json:"id"`
	Type    protocol.MessageType `json:"type"`
	Payload SubscribePayload     `json:"payload"`
}

type SubscribePayload

type SubscribePayload struct {
	OperationName string                 `json:"operationName"`
	Query         string                 `json:"query"`
	Variables     map[string]interface{} `json:"variables"`
	Extensions    map[string]interface{} `json:"extensions"`
}

SubscribePayload payload for a subscribe operation

Jump to

Keyboard shortcuts

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