2021-03-27 09:11:38 +00:00
2021-12-03 02:11:37 +00:00
2021-12-03 02:11:37 +00:00
2021-10-02 20:22:00 +01:00
2021-04-02 21:18:11 +01:00
2021-12-03 02:11:37 +00:00
2021-12-03 02:11:37 +00:00
2021-12-03 02:11:37 +00:00
2021-12-03 15:12:02 +00:00
2021-12-02 08:54:42 +00:00

Simple GraphQL client

Run unit tests codecov Go Reference

Ps. It's Hasura friendly.

Reasoning

I've tried to run few graphQL clients with hasura, all of them required conversion of the data into the appropriate structures, causing issues with non-existing types ( thanks to Hasura ), for example bigint which was difficult to export. Therefore, I present you the simple client to which you can copy & paste your graphQL query, variables and you are good to go.

Features

  • Executing GraphQL queries as they are, without types declaration
  • HTTP2 support!
  • Support for additional headers
  • Query cache

Usage example

Environment variables

  • GRAPHQL_ENDPOINT - Your GraphQL endpoint. Default: http://127.0.0.1:9090/v1/graphql
  • GRAPHQL_CACHE - Should the query cache be enabled? Default: false
  • GRAPHQL_CACHE_TTL - Cache TTL in seconds for SELECT type of queries. Default: 5
  • LOG_LEVEL - Logging level. Default: info

Example reader code

import (
  fmt
  gql "github.com/lukaszraczylo/go-simple-graphql"
)

headers := map[string]interface{}{
  "x-hasura-user-id":   37,
  "x-hasura-user-uuid": "bde3262e-b42e-4151-ac10-d43f0bef44a5",
}

variables := map[string]interface{}{
"fileHash": "123deadc0w321",
}
var query = `query searchFileKnown($fileHash: String) {
  tbl_file_scans(where: {file_hash: {_eq: $fileHash}}) {
  	porn
  	racy
  	violence
  	virus
  }
}

graphql := NewConnection()
result, err := graphql.Query(query, variables, nil)
if err != nil {
  fmt.Println("Query error", err)
  return
}
fmt.Println(result)

Result

{"tbl_user_group_admins":[{"id":109,"is_admin":1}]}

Working with results

I'm using an amazing library tidwall/gjson to parse the results and extract information required in further steps and I strongly recommend this approach as the easiest and close to painless.

result = gjson.Get(result, "tbl_user_group_admins.0.is_admin").Bool()
if result {
  fmt.Println("User is an admin")
}
Languages
Go 99.6%
Makefile 0.4%