-
Notifications
You must be signed in to change notification settings - Fork 82
/
graph_record.go
72 lines (63 loc) · 2.24 KB
/
graph_record.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
package analytics
import (
"github.com/TykTechnologies/storage/persistent/model"
)
// GraphSQLTableName should be defined before SQL migration is called on the GraphRecord
// the reason this approach is used to define the table name is due to gorm's inability to
// read values from the fields of the GraphRecord/AnalyticsRecord struct when it is migrating, due to that
// a single static value is going to be returned as TableName and it will be used as the prefix for index/relationship creation no matter the
// value passed to db.Table()
var GraphSQLTableName string
type GraphRecord struct {
Types map[string][]string `gorm:"types"`
AnalyticsRecord AnalyticsRecord `bson:",inline" gorm:"embedded;embeddedPrefix:analytics_"`
OperationType string `gorm:"column:operation_type"`
Variables string `gorm:"variables"`
RootFields []string `gorm:"root_fields"`
Errors []GraphError `gorm:"errors"`
HasErrors bool `gorm:"has_errors"`
}
// TableName is used by both the sql orm and mongo driver the table name and collection name used for operations on this model
// the conditional return is to ensure the right value is used for both the sql and mongo operations
func (g *GraphRecord) TableName() string {
if GraphSQLTableName == "" {
return g.AnalyticsRecord.TableName()
}
return GraphSQLTableName
}
// GetObjectID is a dummy function to satisfy the interface
func (*GraphRecord) GetObjectID() model.ObjectID {
return ""
}
// SetObjectID is a dummy function to satisfy the interface
func (*GraphRecord) SetObjectID(model.ObjectID) {
// empty
}
func (a *AnalyticsRecord) ToGraphRecord() GraphRecord {
if !a.IsGraphRecord() {
return GraphRecord{}
}
opType := ""
switch a.GraphQLStats.OperationType {
case OperationQuery:
opType = "Query"
case OperationMutation:
opType = "Mutation"
case OperationSubscription:
opType = "Subscription"
default:
}
record := GraphRecord{
AnalyticsRecord: *a,
RootFields: a.GraphQLStats.RootFields,
Types: a.GraphQLStats.Types,
Errors: a.GraphQLStats.Errors,
HasErrors: a.GraphQLStats.HasErrors,
Variables: a.GraphQLStats.Variables,
OperationType: opType,
}
if a.ResponseCode >= 400 {
record.HasErrors = true
}
return record
}