A simple Hasura client for query and metadata APIs

Hasura Client

A Hasura client for schema APIs https://hasura.io/docs/latest/graphql/core/api-reference/index.html

Usage

Create client

client := hasura.NewClient(http.DefaultClient, "http://localhost:8080", "admin_secret")

Query

Run SQL

client.Query.RunSQL(source string, sql string, cascade bool) (io.ReadCloser, error)

Example

reader, err := client.Query.RunSQL("default", "SELECT 1", false)

if err != nil {
  return err
}

// result
// { 
//   "result_type": "TuplesOk",
//   "result": [
//     ["?column?"],
//     ["1"]
//   ]
// }

Bulk requests

client.Query.Bulk(args []*RequestBody) (io.ReadCloser, error)

Example

reader, err := client.Query.Bulk([]*RequestBody{
  NewRunSQLRequest("default", "SELECT 1", false),
  NewRunSQLRequest("default", "SELECT NOW()", false),
})

if err != nil {
  return err
}

// result
// [{ 
//   "result_type": "TuplesOk",
//   "result": [
//     ["?column?"],
//     ["1"]
//   ]
// }, { 
//   "result_type": "TuplesOk",
//   "result": [
//     ["?column?"],
//     ["2021-11-01 15:04:05.999999+00"]
//   ]
// }]

Not all responses are necessary to be decoded, so the client doesn't automatically do it. Instead we separate the decoder into following utilities.

Unsafe decoder

The unsafe decoder uses reflect to decode query results to slice of structure objects.

unsafe.Decode(r io.ReadCloser, v interface{}) error
// or
unsafe.DecodeBytes(input []byte, v interface{}) error
var output []struct {
  Column1 string `column:"col1"`
  Column2 int    `column:"col2"`
}


err := Decode(reader, &output)
if err != nil {
  return err
}

For bulk request, the result are the list of raw table data. To decode multiple objects, use DecodeBulk.

unsafe.DecodeBulk(r io.ReadCloser, v interface{}, args ...interface{}) error
// or
unsafe.DecodeBulkBytes(r io.ReadCloser, v interface{}, args ...interface{}) error
var output1 []struct {
  Column1 string `column:"col1"`
  Column2 int    `column:"col2"`
}

var output2 []struct {
  Column3 int `column:"col3"`
  Column4 float64    `column:"col4"`
}

err := DecodeBulk(reader, &output1, output2)
if err != nil {
  return err
}
Owner
Toan Nguyen
Fullstack, DevOps and Functional Programming learner
Toan Nguyen
Similar Resources

Implementation of Minecraft protocols : ping, query and icon.

Implementation of Minecraft protocols : ping, query and icon.

mcutils - Implementation of Minecraft protocols in Go Informations General All protocols are implemented in Go, without any external dependency. All p

Dec 19, 2022

IP2Proxy Go package allows users to query an IP address to determine if it was being used as open proxy, web proxy, VPN anonymizer and TOR exits.

IP2Proxy Go Package This package allows user to query an IP address if it was being used as VPN anonymizer, open proxies, web proxies, Tor exits, data

Sep 15, 2022

Server and client implementation of the grpc go libraries to perform unary, client streaming, server streaming and full duplex RPCs from gRPC go introduction

Description This is an implementation of a gRPC client and server that provides route guidance from gRPC Basics: Go tutorial. It demonstrates how to u

Nov 24, 2021

Mackerel plugin to post bigquery's query result

mackerel-plugin-bigquery-query-result-importer Synopsis % mackerel-plugin-bigque

Feb 5, 2022

Prisma Client Go is an auto-generated and fully type-safe database client

Prisma Client Go Typesafe database access for Go Quickstart • Website • Docs • API reference • Blog • Slack • Twitter Prisma Client Go is an auto-gene

Jan 9, 2023

The Dual-Stack Dynamic DNS client, the world's first dynamic DNS client built for IPv6.

dsddns DsDDNS is the Dual-Stack Dynamic DNS client. A dynamic DNS client keeps your DNS records in sync with the IP addresses associated with your hom

Sep 27, 2022

Go Substrate RPC Client (GSRPC)Go Substrate RPC Client (GSRPC)

Go Substrate RPC Client (GSRPC) Substrate RPC client in Go. It provides APIs and types around Polkadot and any Substrate-based chain RPC calls. This c

Nov 11, 2021

Godaddy-domains-client-go - Godaddy domains api Client golang - Write automaticly from swagger codegen

Go API client for swagger Overview This API client was generated by the swagger-codegen project. By using the swagger-spec from a remote server, you c

Jan 9, 2022

Tailscale-client-go - A client implementation for the Tailscale HTTP API

tailscale-client-go A client implementation for the Tailscale HTTP API Example p

Sep 8, 2022
A proxy to hide NFT metadata during the sale and prevent people from sniping specific NFTs.

NFT Sale Proxy A proxy to hide NFT metadata during the sale and prevent people from sniping specific NFTs. Check alephao/nft-sale-proxy-examples to se

Nov 1, 2022
DipDup plugin for selective metadata indexing

DipDup metadata indexer DipDup service for indexing contract and token metadata. Based on TzKT indexer. For start synchronization of DipDup state TzKT

Nov 25, 2022
Fast and idiomatic client-driven REST APIs.
Fast and idiomatic client-driven REST APIs.

Vulcain is a brand new protocol using HTTP/2 Server Push to create fast and idiomatic client-driven REST APIs. An open source gateway server which you

Jan 8, 2023
The Go client to access APIs on the Micro Platform

This is the Go client to access APIs on the Micro Platform

Nov 1, 2022
whois-go is a simple Go module for domain and ip whois info query

whois-go is a simple Go module for domain and ip whois info query

Feb 11, 2022
Mauliasproxy - a simple room alias proxy that can respond to the federation alias query endpoint

Mauliasproxy - a simple room alias proxy that can respond to the federation alias query endpoint

Dec 22, 2022
A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.
A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.

Realtime API Gateway Synchronize Your Clients Visit Resgate.io for guides, live demos, and resources. Resgate is a Go project implementing a realtime

Dec 31, 2022
kcp is a prototype of a Kubernetes API server that is not a Kubernetes cluster - a place to create, update, and maintain Kube-like APis with controllers above or without clusters.
kcp is a prototype of a Kubernetes API server that is not a Kubernetes cluster - a place to create, update, and maintain Kube-like APis with controllers above or without clusters.

kcp is a minimal Kubernetes API server How minimal exactly? kcp doesn't know about Pods or Nodes, let alone Deployments, Services, LoadBalancers, etc.

Jan 6, 2023
Spin up a local instance of an Avalanche network to interact with the standard APIs or to test a custom VM
Spin up a local instance of an Avalanche network to interact with the standard APIs or to test a custom VM

ava-sim ava-sim makes it easy for anyone to spin up a local instance of an Avalanche network to interact with the standard APIs or to test a custom VM

Oct 24, 2022
Manabie todo apis

Manabie TODO apis Introduction This is a simple backend for a todo service, right now this service can handle login/list/create simple tasks, to make

Nov 13, 2021