Skip to content

Commit e05f0fd

Browse files
author
Pawan Rawal
committed
Support GraphQL vars in first, offset and after at root.
1 parent 87aa10b commit e05f0fd

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

gql/parser.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,16 @@ func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) {
21862186
}
21872187
item := it.Item()
21882188

2189-
if item.Typ == itemMathOp {
2189+
if item.Typ == itemDollar {
2190+
it.Next()
2191+
item = it.Item()
2192+
if item.Typ == itemName {
2193+
val = fmt.Sprintf("$%s", item.Val)
2194+
} else {
2195+
return nil, x.Errorf("Expecting a variable name. Got: %v", item)
2196+
}
2197+
goto ASSIGN
2198+
} else if item.Typ == itemMathOp {
21902199
if item.Val != "+" && item.Val != "-" {
21912200
return nil,
21922201
x.Errorf("Only Plus and minus are allowed unary ops. Got: %v",
@@ -2247,6 +2256,7 @@ func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) {
22472256
continue
22482257
}
22492258

2259+
ASSIGN:
22502260
if _, ok := gq.Args[key]; ok {
22512261
return gq, x.Errorf("Repeated key %q at root", key)
22522262
}

gql/parser_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,3 +4186,67 @@ func TestParseMissingGraphQLVar(t *testing.T) {
41864186
require.Error(t, err)
41874187
}
41884188
}
4189+
4190+
func TestParseGraphQLVarPaginationRoot(t *testing.T) {
4191+
for _, q := range []string{
4192+
"query test($a: int = 2){ q(func: uid(0x1), first: $a) { name }}",
4193+
"query test($a: int = 2){ q(func: uid(0x1), offset: $a) { name }}",
4194+
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name, first: $a) { name }}",
4195+
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name, offset: $a) { name }}",
4196+
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name, first: $a) { name }}",
4197+
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name, offset: $a) { name }}",
4198+
} {
4199+
r := Request{
4200+
Str: q,
4201+
Variables: map[string]string{"$a": "3"},
4202+
}
4203+
gq, err := Parse(r)
4204+
t.Log(q)
4205+
t.Log(err)
4206+
require.NoError(t, err)
4207+
args := gq.Query[0].Args
4208+
require.True(t, args["first"] == "3" || args["offset"] == "3")
4209+
}
4210+
}
4211+
4212+
func TestParseGraphQLVarPaginationChild(t *testing.T) {
4213+
for _, q := range []string{
4214+
"query test($a: int = 2){ q(func: uid(0x1)) { friend(first: $a) }}",
4215+
"query test($a: int = 2){ q(func: uid(0x1)) { friend(offset: $a) }}",
4216+
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name) { friend(first: $a) }}",
4217+
"query test($a: int = 2){ q(func: uid(0x1), orderdesc: name) { friend(offset: $a) }}",
4218+
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name) { friend(first: $a) }}",
4219+
"query test($a: int = 2){ q(func: eq(name, \"abc\"), orderdesc: name) { friend(offset: $a) }}",
4220+
} {
4221+
r := Request{
4222+
Str: q,
4223+
Variables: map[string]string{"$a": "3"},
4224+
}
4225+
gq, err := Parse(r)
4226+
t.Log(q)
4227+
t.Log(err)
4228+
require.NoError(t, err)
4229+
args := gq.Query[0].Children[0].Args
4230+
require.True(t, args["first"] == "3" || args["offset"] == "3")
4231+
}
4232+
}
4233+
4234+
func TestParseGraphQLVarPaginationRootMultiple(t *testing.T) {
4235+
q := `query test($a: int, $b: int, $after: string){
4236+
q(func: uid(0x1), first: $a, offset: $b, after: $after, orderasc: name) {
4237+
friend
4238+
}
4239+
}`
4240+
4241+
r := Request{
4242+
Str: q,
4243+
Variables: map[string]string{"$a": "3", "$b": "5", "$after": "0x123"},
4244+
}
4245+
gq, err := Parse(r)
4246+
require.NoError(t, err)
4247+
args := gq.Query[0].Args
4248+
require.Equal(t, args["first"], "3")
4249+
require.Equal(t, args["offset"], "5")
4250+
require.Equal(t, args["after"], "0x123")
4251+
require.Equal(t, gq.Query[0].Order[0].Attr, "name")
4252+
}

0 commit comments

Comments
 (0)