Skip to content

Commit

Permalink
fix: perf updates and support for graphql variable default value
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jan 20, 2023
1 parent 68bee86 commit e04acb1
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 382 deletions.
22 changes: 6 additions & 16 deletions core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (g *GraphJin) GraphQL(c context.Context,

// if not production then save to allow list
if !gj.prod && r.name != "IntrospectionQuery" {
if err = gj.saveToAllowList(resp.qc, vars, resp.res.ns); err != nil {
if err = gj.saveToAllowList(resp.qc, resp.res.ns); err != nil {
return
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ type graphqlReq struct {
name string
query []byte
vars json.RawMessage
aschema json.RawMessage
aschema map[string]json.RawMessage
rc *ReqConfig
}

Expand Down Expand Up @@ -455,7 +455,7 @@ func (r *graphqlReq) Set(item allow.Item) {
r.op = qcode.GetQTypeByName(item.Operation)
r.name = item.Name
r.query = item.Query
r.aschema = item.Vars
r.aschema = item.ActionJSON
}

func (gj *graphjin) queryWithResult(c context.Context, r graphqlReq) (res *Result, err error) {
Expand Down Expand Up @@ -487,20 +487,10 @@ func (gj *graphjin) query(c context.Context, r graphqlReq) (
return
}

var role string

if v, ok := c.Value(UserRoleKey).(string); ok {
role = v
} else {
switch c.Value(UserIDKey).(type) {
case string, int:
role = "user"
default:
role = "anon"
}
s, err := newGState(c, gj, r)
if err != nil {
return
}

s := newGState(gj, r, role)
err = s.compileAndExecuteWrapper(c)

resp.qc = s.qcode()
Expand Down
33 changes: 12 additions & 21 deletions core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,27 @@ import (
"fmt"

"github.com/dosco/graphjin/v2/core/internal/psql"
"github.com/dosco/graphjin/v2/internal/jsn"
)

// argList function is used to create a list of arguments to pass
// to a prepared statement.

type args struct {
json []byte
values []interface{}
cindx int // index of cursor arg
}

func (gj *graphjin) argList(c context.Context,
md psql.Metadata,
vars []byte,
rc *ReqConfig) (args, error) {

ar := args{cindx: -1}
fields map[string]json.RawMessage,
rc *ReqConfig,
buildJSON bool,
) (ar args, err error) {
ar = args{cindx: -1}
params := md.Params()
vl := make([]interface{}, len(params))

var fields map[string]json.RawMessage
var err error

vars, err = decryptValues(vars, decPrefix, gj.encKey)
if err != nil {
return ar, err
}

if len(vars) != 0 {
fields, _, err = jsn.Tree(vars)
if err != nil {
return ar, err
}
}

for i, p := range params {
switch p.Name {
case "user_id", "userID", "userId":
Expand Down Expand Up @@ -111,13 +97,18 @@ func (gj *graphjin) argList(c context.Context,
vl[i] = fn()
}
}

} else {
return ar, argErr(p)
}
}
}
ar.values = vl

if buildJSON && len(vl) != 0 {
if ar.json, err = json.Marshal(vl); err != nil {
return
}
}
return ar, nil
}

Expand Down
24 changes: 11 additions & 13 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,20 @@ func (gj *graphjin) initCompilers() (err error) {

func (gj *graphjin) executeRoleQuery(c context.Context,
conn *sql.Conn,
vars json.RawMessage,
vmap map[string]json.RawMessage,
rc *ReqConfig,
) (role string, err error) {
if c.Value(UserIDKey) == nil {
role = "anon"
return
}

var ar args
if ar, err = gj.argList(c,
ar, err := gj.argList(c,
gj.roleStmtMD,
vars,
rc); err != nil {
vmap,
rc,
false)
if err != nil {
return
}

Expand Down Expand Up @@ -374,7 +375,7 @@ func retryIfDBError(err error) bool {
return (err == driver.ErrBadConn)
}

func (gj *graphjin) saveToAllowList(qc *qcode.QCode, vars json.RawMessage, ns string) (err error) {
func (gj *graphjin) saveToAllowList(qc *qcode.QCode, ns string) (err error) {
if gj.conf.DisableAllowList {
return nil
}
Expand All @@ -386,16 +387,13 @@ func (gj *graphjin) saveToAllowList(qc *qcode.QCode, vars json.RawMessage, ns st
Fragments: make([]allow.Fragment, len(qc.Fragments)),
}

if qc.ActionVar != "" {
if len(qc.ActionVal) != 0 {
var buf bytes.Buffer
if err = jsn.Clear(&buf, []byte(vars)); err != nil {
if err = jsn.Clear(&buf, qc.ActionVal); err != nil {
return
}

v := json.RawMessage(buf.Bytes())
item.Vars, err = json.MarshalIndent(v, "", " ")
if err != nil {
return
item.ActionJSON = map[string]json.RawMessage{
qc.ActionVar: json.RawMessage(buf.Bytes()),
}
}

Expand Down
Loading

1 comment on commit e04acb1

@vercel
Copy link

@vercel vercel bot commented on e04acb1 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.