Giter Site home page Giter Site logo

lukaszraczylo / simple-gql-client Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 82 KB

Simple GraphQL client without the mapping of the query into the struct. Compatible with Hasura and its custom types ( hey, bigint, we meet again ).

License: MIT License

Go 93.75% Makefile 6.25%
hasura graphql golang graphql-clients

simple-gql-client's Introduction

Simple GraphQL client

Run unit tests codecov Go Reference

Ps. It's Hasura friendly.

Project updated and moved

This repository has been replaced by new, faster and better go-simple-graphql.

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")
}

simple-gql-client's People

Contributors

lukaszraczylo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.