Skip to content

Commit

Permalink
fix: blocked tables throw introspection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed May 20, 2021
1 parent b25d087 commit 66e060d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
3 changes: 3 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type Config struct {
// Default to 20
DefaultLimit int `mapstructure:"default_limit"`

// DisableAgg disables all aggregation functions like count, sum, etc
DisableAgg bool `mapstructure:"disable_agg_functions"`

// Enable production mode. This defaults to true if GO_ENV is set to
// "production". When this is true the allow list is enforced.
Production bool
Expand Down
1 change: 1 addition & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (gj *GraphJin) initCompilers() error {
TConfig: gj.conf.tmap,
DefaultBlock: gj.conf.DefaultBlock,
DefaultLimit: gj.conf.DefaultLimit,
DisableAgg: gj.conf.DisableAgg,
EnableInflection: gj.conf.EnableInflection,
DBSchema: gj.schema.DBSchema(),
}
Expand Down
1 change: 1 addition & 0 deletions core/internal/qcode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
FragmentFetcher func(name string) (string, error)
DefaultBlock bool
DefaultLimit int
DisableAgg bool
EnableInflection bool
DBSchema string
defTrv trval
Expand Down
8 changes: 5 additions & 3 deletions core/internal/qcode/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ func (co *Compiler) isFunction(sel *Select, fname, alias string) (Function, bool
}

func (co *Compiler) funcPrefixLen(col string) int {
for _, v := range stdFuncs {
if strings.HasPrefix(col, v) {
return len(v)
if !co.c.DisableAgg {
for _, v := range stdFuncs {
if strings.HasPrefix(col, v) {
return len(v)
}
}
}
fnLen := len(col)
Expand Down
29 changes: 20 additions & 9 deletions core/introspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var funcListString []funcInfo = []funcInfo{
type intro struct {
*schema.Schema
*sdata.DBSchema
gj *GraphJin
query *schema.Object
mutation *schema.Object
subscription *schema.Object
Expand All @@ -126,6 +127,7 @@ func (gj *GraphJin) initGraphQLEgine() error {
in := &intro{
Schema: engine.Schema,
DBSchema: gj.schema,
gj: gj,
query: &schema.Object{Name: "Query", Fields: schema.FieldList{}},
mutation: &schema.Object{Name: "Mutation", Fields: schema.FieldList{}},
subscription: &schema.Object{Name: "Subscribe", Fields: schema.FieldList{}},
Expand Down Expand Up @@ -281,6 +283,9 @@ func (in *intro) addTable(name string, ti sdata.DBTable, singular bool) error {
}

for k, t := range relTables1 {
if t.Blocked {
continue
}
ot.Fields = append(ot.Fields, &schema.Field{
Name: k,
Type: &schema.TypeName{Name: t.Name + "Output"},
Expand All @@ -293,6 +298,9 @@ func (in *intro) addTable(name string, ti sdata.DBTable, singular bool) error {
}

for k, t := range relTables2 {
if t.Blocked {
continue
}
ot.Fields = append(ot.Fields, &schema.Field{
Name: k,
Type: &schema.TypeName{Name: t.Name + "Output"},
Expand Down Expand Up @@ -377,7 +385,7 @@ func (in *intro) addColumn(
Type: colType,
})

if col.PrimaryKey {
if col.PrimaryKey && !in.gj.conf.DisableAgg {
ot.Fields = append(ot.Fields, &schema.Field{
Name: funcCount.name + "_" + colName,
Type: colType,
Expand All @@ -388,14 +396,17 @@ func (in *intro) addColumn(
// No functions on foreign key columns
if col.FKeyCol == "" {
// If it's a numeric type...
if typeName == "Float" || typeName == "Int" {
for _, v := range funcListNum {
desc := fmt.Sprintf(v.desc, colName)
ot.Fields = append(ot.Fields, &schema.Field{
Name: v.name + "_" + colName,
Type: colType,
Desc: schema.NewDescription(desc),
})

if !in.gj.conf.DisableAgg {
if typeName == "Float" || typeName == "Int" {
for _, v := range funcListNum {
desc := fmt.Sprintf(v.desc, colName)
ot.Fields = append(ot.Fields, &schema.Field{
Name: v.name + "_" + colName,
Type: colType,
Desc: schema.NewDescription(desc),
})
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions examples/webshop/config/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ cors_debug: false
# Defaults to 20
default_limit: 20

# DisableAgg disables all aggregation functions like count, sum, etc
disable_agg_functions: true

# Set session variable "user.id" to the user id
# Enable this if you need the user id in triggers, etc
# Note: This will not work with subscriptions
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/tmpl/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ poll_every_seconds: 5
# Defaults to 20
default_limit: 20

# DisableAgg disables all aggregation functions like count, sum, etc
disable_agg_functions: false

# Set session variable "user.id" to the user id
# Enable this if you need the user id in triggers, etc
# Note: This will not work with subscriptions
Expand Down

0 comments on commit 66e060d

Please sign in to comment.