Gosura
Go client for the Hasura API.
Table of Contents
Implemented query types
Schema/Metadata API
- Bulk
- Run SQL
- Tables/Views
- track_table
- set_table_is_enum
- track_table v2
- set_table_custom_fields
- untrack_table
- Custom Functions
- track_function
- untrack_function
- Relationships
- create_object_relationship
- create_array_relationship
- drop_relationship
- set_relationship_comment
- Permissions
- create_insert_permission
- drop_insert_permission
- create_select_permission
- drop_select_permission
- create_update_permission
- drop_update_permission
- create_delete_permission
- drop_delete_permission
- set_permission_comment
- Computed Fields
- add_computed_field
- drop_computed_field
- Event Triggers
- create_event_trigger
- delete_event_trigger
- invoke_event_trigger
- Remote Schemas
- add_remote_schema
- remove_remote_schema
- reload_remote_schema
- Query Collections
- create_query_collection
- drop_query_collection
- add_query_to_collection
- drop_query_from_collection
- add_collection_to_allowlist
- drop_collection_from_allowlist
- Manage Metadata
- export_metadata
- replace_metadata
- reload_metadata
- clear_metadata
- get_inconsistent_metadata
- drop_inconsistent_metadata
PG Dump API
- pg_dump
Config API
- config
Usage
Simple query
client := NewHasuraClient().
URL("https://hasura.example.com").
Endpoint("/location_behind_proxy/v1/query").
SetAdminSecret("super_secret").
SkipTLSVerify(true)
query := NewRunSqlQuery()
if err := query.SetArgs(RunSqlArgs{
SQL: "select count(*) from public.posts",
}); err != nil {
panic(err)
}
data, err := client.Do(query)
if err != nil {
log.Fatal(err)
}
if data != nil {
log.Printf("Result type: %s\nResult: %+v",
data.(RunSqlResponse).ResultType,
data.(RunSqlResponse).Result,
)
}
Permissions DSL
package main
import (
"encoding/json"
"fmt"
"github.com/wgnet/gosura"
)
func main() {
insert := gosura.NewCreateInsertPermissionQuery()
perm := gosura.NewInsertPermission()
perm.Columns = gosura.NewPGColumn().AddColumn("*")
perm.Set = map[string]interface{}{
"AuthorID": "X-HASURA-USER-ID",
}
andExp := gosura.NewBoolExp()
exp1 := gosura.NewBoolExp()
exp1.AddKV("UserID", 1)
exp2 := gosura.NewBoolExp()
exp2.AddKV("GroupID", 100)
andExp.AddExp(gosura.AND_EXP_TYPE, exp1, exp2)
perm.Check = andExp
perm.Check.AddKV("AuthorID", "X-HASURA-USER-ID")
insert.SetArgs(gosura.CreateInsertPermissionArgs{
Table: "test",
Role: "my_role",
Comment: "This is a test insert permission",
Permission: perm,
})
data, err := json.MarshalIndent(insert, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
Result JSON:
{
"args": {
"table": "test",
"role": "my_role",
"permission": {
"check": {
"$and": [
{
"UserID": 1
},
{
"GroupID": 100
}
],
"AuthorID": "X-HASURA-USER-ID"
},
"set": {
"AuthorID": "X-HASURA-USER-ID"
},
"columns": "*"
},
"comment": "This is a test insert permission"
},
"type": "create_insert_permission"
}
Bulk queries
Ok, let's go to rewriting previous example
package main
import (
"encoding/json"
"fmt"
"github.com/wgnet/gosura"
)
func insertQuery() gosura.Query {
insert := gosura.NewCreateInsertPermissionQuery()
perm := gosura.NewInsertPermission()
perm.Columns = gosura.NewPGColumn().AddColumn("*")
perm.Set = map[string]interface{}{
"AuthorID": "X-HASURA-USER-ID",
}
andExp := gosura.NewBoolExp()
exp1 := gosura.NewBoolExp()
exp1.AddKV("UserID", 1)
exp2 := gosura.NewBoolExp()
exp2.AddKV("GroupID", 100)
andExp.AddExp(gosura.AND_EXP_TYPE, exp1, exp2)
perm.Check = andExp
perm.Check.AddKV("AuthorID", "X-HASURA-USER-ID")
insert.SetArgs(gosura.CreateInsertPermissionArgs{
Table: "test",
Role: "my_role",
Comment: "This is a test insert permission",
Permission: perm,
})
return insert
}
func runSQLQuery() gosura.Query {
q := gosura.NewRunSqlQuery()
q.SetArgs(gosura.RunSqlArgs{
SQL: "select count(*) from public.test",
})
return q
}
func main() {
bulk := gosura.NewBulkQuery()
bulk.SetArgs(insertQuery())
bulk.SetArgs(runSQLQuery())
data, err := json.MarshalIndent(bulk, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
And result JSON is
{
"args": [
{
"args": {
"table": "test",
"role": "my_role",
"permission": {
"check": {
"$and": [
{
"UserID": 1
},
{
"GroupID": 100
}
],
"AuthorID": "X-HASURA-USER-ID"
},
"set": {
"AuthorID": "X-HASURA-USER-ID"
},
"columns": "*"
},
"comment": "This is a test insert permission"
},
"type": "create_insert_permission"
},
{
"args": {
"sql": "select count(*) from public.test",
"cascade": false,
"check_metadata_consistency": false
},
"version": 1,
"type": "run_sql"
}
],
"type": "bulk"
}
Run this query with the client.Do()
data, err := client.Do(bulk)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", data)