Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning error for duplicate query aliases #2708

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ func Parse(r Request) (res Result, rerr error) {
var qu *GraphQuery
it := lexer.NewIterator()
fmap := make(fragmentMap)
seenQueryAliases := make(map[string]bool)
for it.Next() {
item := it.Item()
switch item.Typ {
Expand Down Expand Up @@ -502,18 +503,32 @@ func Parse(r Request) (res Result, rerr error) {
if qu, rerr = getVariablesAndQuery(it, vmap); rerr != nil {
return res, rerr
}

if _, found := seenQueryAliases[qu.Alias]; found {
return res, x.Errorf("Duplicate aliases not allowed: %v", qu.Alias)
}
seenQueryAliases[qu.Alias] = true
res.Query = append(res.Query, qu)
}
case itemLeftCurl:
if qu, rerr = getQuery(it); rerr != nil {
return res, rerr
}

if _, found := seenQueryAliases[qu.Alias]; found {
return res, x.Errorf("Duplicate aliases not allowed: %v", qu.Alias)
}
seenQueryAliases[qu.Alias] = true
res.Query = append(res.Query, qu)
case itemName:
it.Prev()
if qu, rerr = getQuery(it); rerr != nil {
return res, rerr
}
if _, found := seenQueryAliases[qu.Alias]; found {
return res, x.Errorf("Duplicate aliases not allowed: %v", qu.Alias)
}
seenQueryAliases[qu.Alias] = true
res.Query = append(res.Query, qu)
}
}
Expand Down
34 changes: 34 additions & 0 deletions gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ func TestParseVarError(t *testing.T) {
require.Contains(t, err.Error(), "Cannot do uid() of a variable")
}

func TestDuplicateQueryAliasesError(t *testing.T) {
query := `
{
find_michael(func: eq(name@., "Michael")) {
uid
name@.
age
}
find_michael(func: eq(name@., "Amit")) {
uid
name@.
}
}`
_, err := Parse(Request{Str: query})
require.Error(t, err)

queryInOpType := `
{
find_michael(func: eq(name@., "Michael")) {
uid
name@.
age
}
}
query {find_michael(func: eq(name@., "Amit")) {
uid
name@.
}
}
`
_, err = Parse(Request{Str: queryInOpType})
require.Error(t, err)
}

func TestParseQueryListPred1(t *testing.T) {
query := `
{
Expand Down