Fix issue with cursor as a variable

This commit is contained in:
Vikram Rangnekar 2020-02-11 11:41:35 +05:30
parent 7413813138
commit 67b4a4d945
4 changed files with 22 additions and 15 deletions

View File

@ -1024,7 +1024,7 @@ mutation {
}
```
#### Pagination
### Pagination
This is a must have feature of any API. When you want your users to go thought a list page by page or implement some fancy infinite scroll you're going to need pagination. There are two ways to paginate in Super Graph.

View File

@ -77,7 +77,7 @@ func (co *Compiler) compileMutation(qc *qcode.QCode, w io.Writer, vars Variables
root.Where = nil
root.Args = nil
return c.compileQuery(qc, w)
return c.compileQuery(qc, w, vars)
}
type kvitem struct {

View File

@ -71,7 +71,7 @@ func (co *Compiler) CompileEx(qc *qcode.QCode, vars Variables) (uint32, []byte,
func (co *Compiler) Compile(qc *qcode.QCode, w io.Writer, vars Variables) (uint32, error) {
switch qc.Type {
case qcode.QTQuery:
return co.compileQuery(qc, w)
return co.compileQuery(qc, w, vars)
case qcode.QTInsert, qcode.QTUpdate, qcode.QTDelete, qcode.QTUpsert:
return co.compileMutation(qc, w, vars)
}
@ -79,7 +79,7 @@ func (co *Compiler) Compile(qc *qcode.QCode, w io.Writer, vars Variables) (uint3
return 0, fmt.Errorf("Unknown operation type %d", qc.Type)
}
func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) {
func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer, vars Variables) (uint32, error) {
if len(qc.Selects) == 0 {
return 0, errors.New("empty query")
}
@ -150,7 +150,7 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) {
c.renderLateralJoin(sel)
}
skipped, err := c.renderSelect(sel, ti)
skipped, err := c.renderSelect(sel, ti, vars)
if err != nil {
return 0, err
}
@ -196,7 +196,7 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) {
return ignored, nil
}
func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint32, []*qcode.Column, error) {
func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo, vars Variables) (uint32, []*qcode.Column, error) {
var skipped uint32
cols := make([]*qcode.Column, 0, len(sel.Cols))
@ -232,7 +232,15 @@ func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint
})
if len(sel.Paging.Cursor) != 0 {
v, err := c.decryptor(sel.Paging.Cursor)
var v []byte
var err error
if cursor, ok := vars[sel.Paging.Cursor]; ok && cursor[0] == '"' {
v, err = c.decryptor(string(cursor[1 : len(cursor)-1]))
} else {
v, err = c.decryptor(sel.Paging.Cursor)
}
if err != nil {
return 0, nil, err
}
@ -289,7 +297,7 @@ func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint
return skipped, cols, nil
}
func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo) (uint32, error) {
func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo, vars Variables) (uint32, error) {
var rel *DBRel
var err error
@ -302,7 +310,7 @@ func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo) (uint
}
}
skipped, childCols, err := c.initSelector(sel, ti)
skipped, childCols, err := c.initSelector(sel, ti, vars)
if err != nil {
return 0, err
}

View File

@ -711,7 +711,7 @@ func (com *Compiler) compileArgOrderBy(sel *Select, arg *Arg) (error, bool) {
}
if _, ok := com.bl[node.Name]; ok {
//FreeNode(node, 2)
FreeNode(node, 2)
continue
}
@ -719,7 +719,7 @@ func (com *Compiler) compileArgOrderBy(sel *Select, arg *Arg) (error, bool) {
for i := range node.Children {
st.Push(node.Children[i])
}
//FreeNode(node, 3)
FreeNode(node, 3)
continue
}
@ -768,7 +768,6 @@ func (com *Compiler) compileArgDistinctOn(sel *Select, arg *Arg) (error, bool) {
sel.DistinctOn = append(sel.DistinctOn, node.Children[i].Val)
FreeNode(node.Children[i], 5)
}
//FreeNode(node, 5)
return nil, false
}
@ -788,7 +787,7 @@ func (com *Compiler) compileArgLimit(sel *Select, arg *Arg) (error, bool) {
func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) (error, bool) {
node := arg.Val
if node.Type != NodeInt {
if node.Type != NodeInt && node.Type != NodeVar {
return fmt.Errorf("expecting an integer"), false
}
@ -799,7 +798,7 @@ func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) (error, bool) {
func (com *Compiler) compileArgFirstLast(sel *Select, arg *Arg, pt PagingType) (error, bool) {
node := arg.Val
if node.Type != NodeInt {
if node.Type != NodeInt && node.Type != NodeVar {
return fmt.Errorf("expecting an integer"), false
}
@ -812,7 +811,7 @@ func (com *Compiler) compileArgFirstLast(sel *Select, arg *Arg, pt PagingType) (
func (com *Compiler) compileArgAfterBefore(sel *Select, arg *Arg, pt PagingType) (error, bool) {
node := arg.Val
if node.Type != NodeStr {
if node.Type != NodeStr && node.Type != NodeVar {
return fmt.Errorf("expecting a string"), false
}