Skip to content

Commit

Permalink
Support GraphQL vars in first, offset and after at root.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawan Rawal committed Feb 27, 2018
1 parent 87aa10b commit e05f0fd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion gql/parser.go
Expand Up @@ -2186,7 +2186,16 @@ func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) {
}
item := it.Item()

if item.Typ == itemMathOp {
if item.Typ == itemDollar {
it.Next()
item = it.Item()
if item.Typ == itemName {
val = fmt.Sprintf("$%s", item.Val)
} else {
return nil, x.Errorf("Expecting a variable name. Got: %v", item)
}
goto ASSIGN
} else if item.Typ == itemMathOp {
if item.Val != "+" && item.Val != "-" {
return nil,
x.Errorf("Only Plus and minus are allowed unary ops. Got: %v",
Expand Down Expand Up @@ -2247,6 +2256,7 @@ func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) {
continue
}

ASSIGN:
if _, ok := gq.Args[key]; ok {
return gq, x.Errorf("Repeated key %q at root", key)
}
Expand Down
64 changes: 64 additions & 0 deletions gql/parser_test.go
Expand Up @@ -4186,3 +4186,67 @@ func TestParseMissingGraphQLVar(t *testing.T) {
require.Error(t, err)
}
}

func TestParseGraphQLVarPaginationRoot(t *testing.T) {
for _, q := range []string{
"query test($a: int = 2){ q(func: uid(0x1), first: $a) { name }}",
"query test($a: int = 2){ q(func: uid(0x1), offset: $a) { name }}",
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name, first: $a) { name }}",
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name, offset: $a) { name }}",
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name, first: $a) { name }}",
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name, offset: $a) { name }}",
} {
r := Request{
Str: q,
Variables: map[string]string{"$a": "3"},
}
gq, err := Parse(r)
t.Log(q)
t.Log(err)
require.NoError(t, err)
args := gq.Query[0].Args
require.True(t, args["first"] == "3" || args["offset"] == "3")
}
}

func TestParseGraphQLVarPaginationChild(t *testing.T) {
for _, q := range []string{
"query test($a: int = 2){ q(func: uid(0x1)) { friend(first: $a) }}",
"query test($a: int = 2){ q(func: uid(0x1)) { friend(offset: $a) }}",
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name) { friend(first: $a) }}",
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name) { friend(offset: $a) }}",
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name) { friend(first: $a) }}",
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name) { friend(offset: $a) }}",
} {
r := Request{
Str: q,
Variables: map[string]string{"$a": "3"},
}
gq, err := Parse(r)
t.Log(q)
t.Log(err)
require.NoError(t, err)
args := gq.Query[0].Children[0].Args
require.True(t, args["first"] == "3" || args["offset"] == "3")
}
}

func TestParseGraphQLVarPaginationRootMultiple(t *testing.T) {
q := `query test($a: int, $b: int, $after: string){
q(func: uid(0x1), first: $a, offset: $b, after: $after, orderasc: name) {
friend
}
}`

r := Request{
Str: q,
Variables: map[string]string{"$a": "3", "$b": "5", "$after": "0x123"},
}
gq, err := Parse(r)
require.NoError(t, err)
args := gq.Query[0].Args
require.Equal(t, args["first"], "3")
require.Equal(t, args["offset"], "5")
require.Equal(t, args["after"], "0x123")
require.Equal(t, gq.Query[0].Order[0].Attr, "name")
}

0 comments on commit e05f0fd

Please sign in to comment.