clickhousegraphqlgo

package module
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 10 Imported by: 0

README

clickhouse-graphql-go

Run Tests

GraphQL implementation for clickhouse in Go. This package stores real time streaming websocket data in clickhouse and uses GraphQL to consume the same.

Installation

go get github.com/ranjanrak/clickhouse-graphql-go/v4

Usage

import (
    clickhousegraphqlgo "github.com/ranjanrak/clickhouse-graphql-go/v4"
)

// Create new graphql instance
client := clickhousegraphqlgo.New(clickhousegraphqlgo.ClientParam{
		DBSource:    "tcp://127.0.0.1:9000?debug=true",
		ApiKey:      "your_api_key",
		AccessToken: "your_access_token",
}))

// Dump tick websocket data to clickhouse
// Pass list of instrument token for subscription to websocket feeds
// Nothing will run after this
client.ClickhouseDump([]uint32{779521, 256265, 1893123, 13209858})

// Query
reqQuery := `query {
		Tick(instrument_token:779521) {
		  instrument_token
		  timestamp
		  lastprice
		  volumetraded
		  oi
		}
	}`

// Make single object schema graphql Query
singleQueryStruct, err := client.GraphqlQuery(reqQuery)
if err != nil {
  log.Fatalf("failed to execute single object graphql query, errors: %+v", err)
}
fmt.Printf("%+v\n", singleQueryStruct)

// Make list of object schema graphqlQuery
listQueryStruct, err := client.GraphqlQueryList(reqQuery)
if err != nil {
  log.Fatalf("failed to execute object list graphql query, errors: %+v", err)
}
fmt.Printf("%+v\n", listQueryStruct)

// Run graphql server on clickhouse with single schema
client.GraphqlServer("")

// Run graphql server to fetch list of object schema GraphQL
client.GraphqlServer("List")

GraphQL request query

1> GraphqlQuery(reqQuery)

reqQuery := `query {
		Tick(instrument_token:779521) {
		  instrument_token
		  timestamp
		  lastprice
		  volumetraded
		  oi
		}
	}`

2> GraphqlQueryList(reqQuery)

reqQuery := `query {
		Tick(instrument_token:779521) {
		  instrument_token
		  timestamp
		  lastprice
		  volumetraded
		  oi
		}
	}`

3> GraphqlServer("")


query {
  Tick(instrument_token:779521) {
    instrument_token
    timestamp
    lastprice
    volumetraded
    oi
  }
}

4> GraphqlServer("List")


query {
  Tick(instrument_token:779521) {
    instrument_token
    timestamp
    lastprice
    volumetraded
    oi
  }
}

Response

1> GraphqlQuery(reqQuery)

{Output:{InstrumentToken:779521 LastPrice:463.4 OI:0 Timestamp:2022-06-07 17:12:48 +0530 IST
VolumeTraded:7672515}}

2> GraphqlQueryList(reqQuery)

{Output:[{InstrumentToken:779521 LastPrice:463.4 OI:0 Timestamp:2022-06-07 17:12:48 +0530 IST
VolumeTraded:7672515}
{InstrumentToken:779521 LastPrice:463.4 OI:0 Timestamp:2022-06-07 17:12:48 +0530 IST
VolumeTraded:7672515}
{InstrumentToken:779521 LastPrice:463.4 OI:0 Timestamp:2022-06-07 17:12:48 +0530 IST
VolumeTraded:7672515}
....

3> GraphqlServer("")


{
  "data": {
    "Tick": {
    "instrument_token": 1893123,
    "lastprice": 74.245,
    "oi": 1990638,
    "timestamp": "2021-08-24T16:38:39+05:30",
    "volumetraded": 1099802
    }
  }
}

4> GraphqlServer("List")

{
  "data": {
    "Tick": [
    {
    "instrument_token": 779521,
    "lastprice": 412.65,
    "oi": 0,
    "timestamp": "2021-08-26T12:19:09+05:30",
    "volumetraded": 7619425
    },
    {
    "instrument_token": 779521,
    "lastprice": 412.65,
    "oi": 0,
    "timestamp": "2021-08-26T12:19:09+05:30",
    "volumetraded": 7619425
    },
    ......]
  }
}

Sample query on graphiQL UI

1> GraphqlServer("")

graphQL_dash

2> GraphqlServer("List")

graphQL_dash_list


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents clickhouse DB client connection

func New

func New(clientPar ClientParam) *Client

Create new client instance

func (*Client) ClickhouseDump

func (c *Client) ClickhouseDump(tokens []uint32)

ClickhouseDump starts ticker and dumps tickdata to clickhouse

func (*Client) GraphqlQuery

func (c *Client) GraphqlQuery(reqQuery string) (Result, error)

GraphqlQuery returns graphql query output performed on the tick schema

func (*Client) GraphqlQueryList

func (c *Client) GraphqlQueryList(reqQuery string) (ResultList, error)

GraphqlQueryList returns graphql query output performed on the tick list schema

func (*Client) GraphqlServer

func (c *Client) GraphqlServer(reqSchemaType string)

GraphqlServer runs graphql query server for singleResolver or listResolver on localhost:8080

type ClientParam

type ClientParam struct {
	DBSource    string
	ApiKey      string
	AccessToken string
}

ClientParam represents interface to connect clickhouse and kite ticker stream

type Result

type Result struct {
	Output tickOutput `json:"Tick"`
}

Result represents graphql output struct

type ResultList

type ResultList struct {
	Output []tickOutput `json:"Tick"`
}

ResultList represents graphql tick list schema output struct

Jump to

Keyboard shortcuts

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