graphql

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 18 Imported by: 0

README

go-graphql-client

Preface: This is a fork of https://github.com/hasura/go-graphql-client reworked to work easily with Dgraph's GraphQL implementation and is a WIP.

References

License

Documentation

Overview

Package graphql provides a GraphQL client implementation.

For more information, see package github.com/shurcooL/githubv4, which is a specialized version targeting GitHub GraphQL API v4. That package is driving the feature development.

Status: In active early research and development. The API will change when opportunities for improvement are discovered; it is not yet frozen.

For now, see README for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a GraphQL client.

func NewClient

func NewClient(url string, httpClient *http.Client) *Client

NewClient creates a GraphQL client targeting the specified GraphQL server URL. If httpClient is nil, then http.DefaultClient is used.

func (*Client) Mutate

func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error

Mutate executes a single GraphQL mutation request, with a mutation derived from m, populating the response into it. m should be a pointer to struct that corresponds to the GraphQL schema.

func (*Client) MutateRaw

func (c *Client) MutateRaw(ctx context.Context, m interface{}, variables map[string]interface{}) (*json.RawMessage, error)

MutateRaw executes a single GraphQL mutation request, with a mutation derived from m, populating the response into it. m should be a pointer to struct that corresponds to the GraphQL schema. return raw bytes message.

func (*Client) NamedMutate

func (c *Client) NamedMutate(ctx context.Context, name string, m interface{}, variables map[string]interface{}) error

NamedMutate executes a single GraphQL mutation request, with operation name

func (*Client) NamedMutateRaw

func (c *Client) NamedMutateRaw(ctx context.Context, name string, m interface{}, variables map[string]interface{}) (*json.RawMessage, error)

NamedMutateRaw executes a single GraphQL mutation request, with operation name return raw bytes message.

func (*Client) NamedQuery

func (c *Client) NamedQuery(ctx context.Context, name string, q interface{}, variables map[string]interface{}) error

NamedQuery executes a single GraphQL query request, with operation name

func (*Client) NamedQueryRaw

func (c *Client) NamedQueryRaw(ctx context.Context, name string, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)

NamedQueryRaw executes a single GraphQL query request, with operation name return raw bytes message.

func (*Client) Query

func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error

Query executes a single GraphQL query request, with a query derived from q, populating the response into it. q should be a pointer to struct that corresponds to the GraphQL schema.

func (*Client) QueryRaw

func (c *Client) QueryRaw(ctx context.Context, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)

Query executes a single GraphQL query request, with a query derived from q, populating the response into it. q should be a pointer to struct that corresponds to the GraphQL schema. return raw bytes message.

type GqlBool

type GqlBool struct {
	Bool  bool
	Valid bool // Valid is true if Bool is not NULL
}

func NewBool

func NewBool(x bool) *GqlBool

func NewBoolStruct

func NewBoolStruct(x bool) GqlBool

func (GqlBool) MarshalJSON

func (nb GqlBool) MarshalJSON() ([]byte, error)

func (*GqlBool) UnmarshalJSON

func (nb *GqlBool) UnmarshalJSON(data []byte) error

type GqlFloat64

type GqlFloat64 struct {
	Float64 float64
	Valid   bool // Valid is true if Float64 is not NULL
}

func NewFloat64

func NewFloat64(x float64) *GqlFloat64

func NewFloat64Struct

func NewFloat64Struct(x float64) GqlFloat64

func (GqlFloat64) MarshalJSON

func (nf GqlFloat64) MarshalJSON() ([]byte, error)

func (*GqlFloat64) UnmarshalJSON

func (nf *GqlFloat64) UnmarshalJSON(data []byte) error

type GqlInt64

type GqlInt64 struct {
	Int64 int64
	Valid bool // Valid is true if Int64 is not NULL
}

func NewInt64

func NewInt64(x int64) *GqlInt64

func NewInt64Struct

func NewInt64Struct(x int64) GqlInt64

func (GqlInt64) MarshalJSON

func (ni GqlInt64) MarshalJSON() ([]byte, error)

func (*GqlInt64) UnmarshalJSON

func (ni *GqlInt64) UnmarshalJSON(data []byte) error

type GqlPoint added in v0.3.1

type GqlPoint struct {
	Latitude  *GqlFloat64 `json:"latitude,omitempty"`
	Longitude *GqlFloat64 `json:"longitude,omitempty"`
}

func NewPoint added in v0.3.1

func NewPoint(lat, lng float64) *GqlPoint

func NewPointStruct added in v0.3.1

func NewPointStruct(lat, lng float64) GqlPoint

type GqlString

type GqlString struct {
	String string
	Valid  bool // Valid is true if String is not NULL
}

func NewString

func NewString(x string) *GqlString

func NewStringStruct

func NewStringStruct(x string) GqlString

func (GqlString) MarshalJSON

func (ns GqlString) MarshalJSON() ([]byte, error)

func (*GqlString) UnmarshalJSON

func (ns *GqlString) UnmarshalJSON(data []byte) error

type GqlTime

type GqlTime struct {
	Time  time.Time
	Valid bool // Valid is true if Time is not NULL
}

func NewTime

func NewTime(x time.Time) *GqlTime

func NewTimeStruct

func NewTimeStruct(x time.Time) GqlTime

func (GqlTime) MarshalJSON

func (nt GqlTime) MarshalJSON() ([]byte, error)

func (*GqlTime) UnmarshalJSON

func (nt *GqlTime) UnmarshalJSON(data []byte) error

type OperationMessage

type OperationMessage struct {
	ID      string               `json:"id,omitempty"`
	Type    OperationMessageType `json:"type"`
	Payload json.RawMessage      `json:"payload,omitempty"`
}

func (OperationMessage) String

func (om OperationMessage) String() string

type OperationMessageType

type OperationMessageType string

OperationMessageType

const (
	// Client sends this message after plain websocket connection to start the communication with the server
	GQL_CONNECTION_INIT OperationMessageType = "connection_init"
	// The server may responses with this message to the GQL_CONNECTION_INIT from client, indicates the server rejected the connection.
	GQL_CONNECTION_ERROR OperationMessageType = "conn_err"
	// Client sends this message to execute GraphQL operation
	GQL_START OperationMessageType = "start"
	// Client sends this message in order to stop a running GraphQL operation execution (for example: unsubscribe)
	GQL_STOP OperationMessageType = "stop"
	// Server sends this message upon a failing operation, before the GraphQL execution, usually due to GraphQL validation errors (resolver errors are part of GQL_DATA message, and will be added as errors array)
	GQL_ERROR OperationMessageType = "error"
	// The server sends this message to transfter the GraphQL execution result from the server to the client, this message is a response for GQL_START message.
	GQL_DATA OperationMessageType = "data"
	// Server sends this message to indicate that a GraphQL operation is done, and no more data will arrive for the specific operation.
	GQL_COMPLETE OperationMessageType = "complete"
	// Server message that should be sent right after each GQL_CONNECTION_ACK processed and then periodically to keep the client connection alive.
	// The client starts to consider the keep alive message only upon the first received keep alive message from the server.
	GQL_CONNECTION_KEEP_ALIVE OperationMessageType = "ka"
	// The server may responses with this message to the GQL_CONNECTION_INIT from client, indicates the server accepted the connection. May optionally include a payload.
	GQL_CONNECTION_ACK OperationMessageType = "connection_ack"
	// Client sends this message to terminate the connection.
	GQL_CONNECTION_TERMINATE OperationMessageType = "connection_terminate"
	// Unknown operation type, for logging only
	GQL_UNKNOWN OperationMessageType = "unknown"
	// Internal status, for logging only
	GQL_INTERNAL OperationMessageType = "internal"
)

type Query

type Query struct {
	Data interface{} `graphql:"data"`
}

type SubscriptionClient

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

SubscriptionClient is a GraphQL subscription client.

func NewSubscriptionClient

func NewSubscriptionClient(url string) *SubscriptionClient

func (*SubscriptionClient) Close

func (sc *SubscriptionClient) Close() (err error)

Close closes all subscription channel and websocket as well

func (*SubscriptionClient) GetContext

func (sc *SubscriptionClient) GetContext() context.Context

GetContext returns current context of subscription client

func (*SubscriptionClient) GetTimeout

func (sc *SubscriptionClient) GetTimeout() time.Duration

GetContext returns write timeout of websocket client

func (*SubscriptionClient) GetURL

func (sc *SubscriptionClient) GetURL() string

GetURL returns GraphQL server's URL

func (*SubscriptionClient) NamedSubscribe

func (sc *SubscriptionClient) NamedSubscribe(name string, v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error) (string, error)

NamedSubscribe sends start message to server and open a channel to receive data, with operation name

func (*SubscriptionClient) OnConnected

func (sc *SubscriptionClient) OnConnected(fn func()) *SubscriptionClient

OnConnected event is triggered when the websocket connected to GraphQL server sucessfully

func (*SubscriptionClient) OnDisconnected

func (sc *SubscriptionClient) OnDisconnected(fn func()) *SubscriptionClient

OnDisconnected event is triggered when the websocket server was stil down after retry timeout

func (*SubscriptionClient) OnError

func (sc *SubscriptionClient) OnError(onError func(sc *SubscriptionClient, err error) error) *SubscriptionClient

OnConnected event is triggered when there is any connection error. This is bottom exception handler level If this function is empty, or returns nil, the error is ignored If returns error, the websocket connection will be terminated

func (*SubscriptionClient) Reset

func (sc *SubscriptionClient) Reset() error

Reset restart websocket connection and subscriptions

func (*SubscriptionClient) Run

func (sc *SubscriptionClient) Run() error

Run start websocket client and subscriptions. If this function is run with goroutine, it can be stopped after closed

func (*SubscriptionClient) Subscribe

func (sc *SubscriptionClient) Subscribe(v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error) (string, error)

Subscribe sends start message to server and open a channel to receive data. The handler callback function will receive raw message data or error. If the call return error, onError event will be triggered The function returns subscription ID and error. You can use subscription ID to unsubscribe the subscription

func (*SubscriptionClient) Unsubscribe

func (sc *SubscriptionClient) Unsubscribe(id string) error

Unsubscribe sends stop message to server and close subscription channel The input parameter is subscription ID that is returned from Subscribe function

func (*SubscriptionClient) WithConnectionParams

func (sc *SubscriptionClient) WithConnectionParams(params map[string]interface{}) *SubscriptionClient

WithConnectionParams updates connection params for sending to server through GQL_CONNECTION_INIT event It's usually used for authentication handshake

func (*SubscriptionClient) WithLog

func (sc *SubscriptionClient) WithLog(logger func(args ...interface{})) *SubscriptionClient

WithLog sets loging function to print out received messages. By default, nothing is printed

func (*SubscriptionClient) WithReadLimit

func (sc *SubscriptionClient) WithReadLimit(limit int64) *SubscriptionClient

WithReadLimit set max size of response message

func (*SubscriptionClient) WithRetryTimeout

func (sc *SubscriptionClient) WithRetryTimeout(timeout time.Duration) *SubscriptionClient

WithRetryTimeout updates reconnecting timeout. When the websocket server was stopped, the client will retry connecting every second until timeout

func (*SubscriptionClient) WithTimeout

func (sc *SubscriptionClient) WithTimeout(timeout time.Duration) *SubscriptionClient

WithTimeout updates write timeout of websocket client

func (*SubscriptionClient) WithWebSocket

func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (WebsocketConn, error)) *SubscriptionClient

WithWebSocket replaces customized websocket client constructor In default, subscription client uses https://github.com/nhooyr/websocket

func (*SubscriptionClient) WithoutLogTypes

func (sc *SubscriptionClient) WithoutLogTypes(types ...OperationMessageType) *SubscriptionClient

WithoutLogTypes these operation types won't be printed

type WebsocketConn

type WebsocketConn interface {
	ReadJSON(v interface{}) error
	WriteJSON(v interface{}) error
	Close() error
	// SetReadLimit sets the maximum size in bytes for a message read from the peer. If a
	// message exceeds the limit, the connection sends a close message to the peer
	// and returns ErrReadLimit to the application.
	SetReadLimit(limit int64)
}

WebsocketHandler abstracts WebSocket connecton functions ReadJSON and WriteJSON data of a frame from the WebSocket connection. Close the WebSocket connection.

Directories

Path Synopsis
example
graphqldev
graphqldev is a test program currently being used for developing graphql package.
graphqldev is a test program currently being used for developing graphql package.
subscription
subscription is a test program currently being used for developing graphql package.
subscription is a test program currently being used for developing graphql package.
Package ident provides functions for parsing and converting identifier names between various naming convention.
Package ident provides functions for parsing and converting identifier names between various naming convention.
internal
jsonutil
Package jsonutil provides a function for decoding JSON into a GraphQL query data structure.
Package jsonutil provides a function for decoding JSON into a GraphQL query data structure.

Jump to

Keyboard shortcuts

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