-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
151 lines (135 loc) · 4.16 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package clickhousegraphqlgo
import (
"database/sql"
"log"
"github.com/graphql-go/graphql"
)
// createSchema creates tick graphql schema
func createSchema(dbConnect *sql.DB, resolverType string) (graphql.Schema, error) {
tickType := graphql.NewObject(graphql.ObjectConfig{
Name: "Tick",
Description: "Tick Data",
Fields: graphql.Fields{
"instrument_token": &graphql.Field{
Type: graphql.Int,
Description: "Instrument token",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if tick, ok := p.Source.(*tickData); ok {
return tick.InstrumentToken, nil
}
return nil, nil
},
},
"timestamp": &graphql.Field{
Type: graphql.DateTime,
Description: "Time stamp of tick",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if tick, ok := p.Source.(*tickData); ok {
return tick.Timestamp, nil
}
return nil, nil
},
},
"lastprice": &graphql.Field{
Type: graphql.Float,
Description: "Last Price of tick",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if tick, ok := p.Source.(*tickData); ok {
return tick.LastPrice, nil
}
return nil, nil
},
},
"volumetraded": &graphql.Field{
Type: graphql.Int,
Description: "Total volume",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if tick, ok := p.Source.(*tickData); ok {
return tick.VolumeTraded, nil
}
return nil, nil
},
},
"oi": &graphql.Field{
Type: graphql.Int,
Description: "Net open interest",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if tick, ok := p.Source.(*tickData); ok {
return tick.OI, nil
}
return nil, nil
},
},
},
})
rootQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"Tick": &graphql.Field{
Type: fieldType(resolverType, tickType),
Description: "Get tick detail",
Args: graphql.FieldConfigArgument{
"instrument_token": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
instrument_token, _ := params.Args["instrument_token"].(int)
tickDataRef := &tickData{}
rows, err := dbConnect.Query(`select instrument_token,
timestamp,
last_price,
volume_traded,
oi
from tickstore where (instrument_token = ?)`, instrument_token)
if err != nil {
log.Fatalf("Error quering tickstore DB : %v", err)
}
defer rows.Close()
// fetch tick data as per resolverType i.e list or single object
if resolverType == "List" {
return resolverList(rows, tickDataRef), nil
} else {
return resolverSingle(rows, tickDataRef), nil
}
},
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
})
if err != nil {
return graphql.Schema{}, err
}
return schema, nil
}
// resolverSingle return just the last tick object
func resolverSingle(rows *sql.Rows, tickDataRef *tickData) *tickData {
for rows.Next() {
if err := rows.Scan(&tickDataRef.InstrumentToken, &tickDataRef.Timestamp, &tickDataRef.LastPrice, &tickDataRef.VolumeTraded, &tickDataRef.OI); err != nil {
log.Fatalf("failed to parse DB rows for resolverSingle : %v", err)
}
}
return tickDataRef
}
// resolverList returns all list of available tick object
func resolverList(rows *sql.Rows, tickDataRef *tickData) []*tickData {
// fetch all available tick data as list
tickDataSum := make([]*tickData, 0)
for rows.Next() {
if err := rows.Scan(&tickDataRef.InstrumentToken, &tickDataRef.Timestamp, &tickDataRef.LastPrice, &tickDataRef.VolumeTraded, &tickDataRef.OI); err != nil {
log.Fatalf("failed to parse DB rows for resolverList: %v", err)
}
tickDataSum = append(tickDataSum, tickDataRef)
}
return tickDataSum
}
// Select output field type based on input resolverType for different schema
func fieldType(resolverType string, tickType *graphql.Object) graphql.Output {
if resolverType == "List" {
return graphql.NewList(tickType)
} else {
return tickType
}
}