hasura

package module
v0.0.0-...-86153d9 Latest Latest
Warning

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

Go to latest
Published: May 28, 2019 License: MIT Imports: 3 Imported by: 0

README

go-hasura

A simple Go clien for Hasura JSON APIs

Installation
go get github.com/shahidhk/go-hasura
Usage
// import the packages
import (
    "fmt"
    "log"

    "github.com/shahidhk/go-hasura" // imported as 'hasura'
)

// response type
type author struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func main() {
    // create a client
    c := hasura.NewClient("http://localhost:8080/v1/query", nil)
    // if you need to add access key or other headers,
    // c := hasura.NewClient("http://localhost:8080/v1/query", map[string]string{
    //     "X-Hausra-Access-Key": "xyz",
    // })

    // build a query object
	q := hasura.Query{
		Type: "select",
		Args: hasura.Args{
			Table: "author",
			Columns: []string{
				"id",
				"name",
			},
		},
    }
    
    // create response object
	var authors []author
    
    // execute the query
    err := c.Execute(q, &response)
    // error is thrown for non-200 responses also
    if err != nil {
        log.Fatal(err)
    }

    // print the response
    fmt.Println(authors)
}

Documentation

Index

Constants

View Source
const (
	TuplesOK  = "TuplesOk"
	CommandOK = "CommandOk"
)

Constants defined for SQL response

Variables

This section is empty.

Functions

This section is empty.

Types

type Args

type Args struct {
	SQL        string        `json:"sql,omitempty"`
	Table      interface{}   `json:"table,omitempty"`
	Columns    interface{}   `json:"columns,omitempty"`
	Where      interface{}   `json:"where,omitempty"`
	OrderBy    interface{}   `json:"order_by,omitempty"`
	Objects    []interface{} `json:"objects,omitempty"`
	Limit      int           `json:"limit,omitempty"`
	Returning  []string      `json:"returning,omitempty"`
	Set        interface{}   `json:"$set,omitempty"`
	OnConflict `json:"on_conflict,omitempty"`
}

Args for a query

type Bulk

type Bulk struct {
	Type string  `json:"type"`
	Args []Query `json:"args"`
}

Bulk is a query in which multiple queries can be executed.

type Client

type Client struct {
	Endpoint string
	Headers  map[string]string
	// contains filtered or unexported fields
}

Client can execute queries against an endpoint

func NewClient

func NewClient(endpoint string, headers map[string]string) *Client

NewClient returns a Client for given endpoint and headers

func (*Client) Execute

func (c *Client) Execute(r Query, response interface{}) error

Execute executes the Query r using the Client c and returns an error Response data can be unmarshalled to the passed interface

type Error

type Error struct {
	Path     string         `json:"path"`
	Err      string         `json:"error"`
	Internal *InternalError `json:"internal,omitempty"`
	Message  string         `json:"message,omitempty"`
	Code     string         `json:"code"`
}

Error is a Hasura erro response structure;e

func (Error) Error

func (e Error) Error() string

Error returns the error message

type InternalError

type InternalError struct {
	Arguments []string      `json:"arguments"`
	Error     PostgresError `json:"error"`
	Prepared  bool          `json:"prepared"`
	Statement string        `json:"statement"`
}

InternalError is thrown when SQL execution fails

type OnConflict

type OnConflict struct {
	// action: one of update or ignore
	Action       string   `json:"action"`
	Constraint   string   `json:"constraint,omitempty"`
	ConstraintOn []string `json:"constraint_on,omitempty"`
}

OnConflict argument

type OrderBy

type OrderBy struct {
	Column string `json:"column,omitempty"`
	Type   string `json:"type,omitempty"`
	Nulls  string `json:"nulls,omitempty"`
}

OrderBy is the Hasura order_by expression

type PostgresError

type PostgresError struct {
	StatusCode  string `json:"status_code"`
	ExecStatus  string `json:"exec_status"`
	Message     string `json:"message"`
	Description string `json:"description"`
	Hint        string `json:"hint"`
}

PostgresError is the error thrown by Postgres

type Query

type Query struct {
	Type string `json:"type"`
	Args `json:"args"`
}

Query is the Hasura Query object

type RelatedColumn

type RelatedColumn struct {
	Name    string      `json:"name"`
	Columns interface{} `json:"columns,omitempty"`
}

RelatedColumn is a relationship expression in a select query

type RunSQLResponse

type RunSQLResponse struct {
	ResultType string     `json:"result_type"`
	Result     [][]string `json:"result"`
}

RunSQLResponse is the structured response obtained when SQL is executed

Jump to

Keyboard shortcuts

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