gql

package module
v1.32.14 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: MIT Imports: 8 Imported by: 1

README

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
  • Compressing produced queries
  • Support for additional headers
  • Support for gzip compression ( built into Hasura )

Usage example

Setting GraphQL endpoint

You can set the endpoint variable within your code

gql.GraphQLUrl = "http://127.0.0.1:9090/v1/graphql"

or as an environment variable GRAPHQL_ENDPOINT=http://127.0.0.1:9090/v1/graphql

Example reader code
import (
  fmt
  gql "github.com/lukaszraczylo/simple-gql-client"
)

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
  }
}`
result, err := Query(query, variables, nil)
if err != nil {
  fmt.Println("Query error", err)
  return
}
fmt.Println(result)
`
result := Query(query, variables, headers)
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")
}

Documentation

Overview

Package / library or rather wrapper for GraphQL queries execution in the painless way. Using it is as easy as copy / paste the query itself and set appropriate variables in.

Library supports basic error reporting on unsuccessful queries and setting appropriate headers.

Index

Examples

Constants

This section is empty.

Variables

View Source
var GraphQLUrl string

Endpoint of your GraphQL server to query this variable can be overwritten by setting env variable, for example: GRAPHQL_ENDPOINT=http://hasura.local/v1/graphql

Functions

func Query

func Query(query string, variables interface{}, headers map[string]interface{}) (string, error)

Query allows you to execute the GraphQL query. Query is a string ( copy paste from Hasura or any other query builder ) Variables and Headers are maps of strings ( see the example ) Function returns whatever specified query returns and/or error.

Example
variables := map[string]interface{}{
	"fileHash": "123deadc0w321",
}
var query = `query searchFileKnown($fileHash: String) {
		tbl_file_scans(where: {file_hash: {_eq: $fileHash}}) {
			porn
			racy
			violence
			virus
		}
	}`
result, err := Query(query, variables, nil)
if err != nil {
	fmt.Println("Query error", err)
	return
}
fmt.Println(result)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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