Skip to content

Commit

Permalink
fix(GraphQL): fix internal Aliases name generation (#7009)
Browse files Browse the repository at this point in the history
This PR modifies how the internal graph aliases are generated in case of multiple aliases of same field in the query.
For the given graphql query:
```
query {
      queryAuthor {
        name
        p1: posts(filter: {isPublished: true}){
          title
          text
        }
        p2: posts(filter: {isPublished: true}){
          title
          text
        }
      }
    }
```
earlier it was rewritten into:
```
query {
      queryAuthor(func: type(Author)) {
        name : Author.name
        posts : Author.posts @filter(eq(Post.isPublished, true)) {
          title : Post.title
          text : Post.text
          dgraph.uid : uid
        }
        posts1 : Author.posts @filter(eq(Post.isPublished, true)) {
          title : Post.title
          text : Post.text
          dgraph.uid : uid
        }
        dgraph.uid : uid
      }
    }
```
Now it is changed to:
```
query {
      queryAuthor(func: type(Author)) {
        name : Author.name
        posts : Author.posts @filter(eq(Post.isPublished, true)) {
          title : Post.title
          text : Post.text
          dgraph.uid : uid
        }
        posts.1 : Author.posts @filter(eq(Post.isPublished, true)) {
          title : Post.title
          text : Post.text
          dgraph.uid : uid
        }
        dgraph.uid : uid
      }
    }
```

(cherry picked from commit 953f656)
  • Loading branch information
minhaj-shakeel authored and abhimanyusinghgaur committed Dec 14, 2020
1 parent 357e0a7 commit 2bc0d56
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,14 +1170,14 @@ func buildAggregateFields(

// Generate Unique Dgraph Alias for the field based on number of time it has been
// seen till now in the given query at current level. If it is seen first time then simply returns the field's DgraphAlias,
// and if it is seen let's say 3rd time then return "fieldAlias3" where "fieldAlias"
// and if it is seen let's say 3rd time then return "fieldAlias.3" where "fieldAlias"
// is the DgraphAlias of the field.
func generateUniqueDgraphAlias(f schema.Field, fieldSeenCount map[string]int) string {
alias := f.DgraphAlias()
if fieldSeenCount[alias] == 0 {
return alias
}
return alias + strconv.Itoa(fieldSeenCount[alias])
return alias + "." + strconv.Itoa(fieldSeenCount[alias])
}

// TODO(GRAPHQL-874), Optimise Query rewriting in case of multiple alias with same filter.
Expand Down
8 changes: 4 additions & 4 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,7 @@
text : Post.text
dgraph.uid : uid
}
posts1 : Author.posts @filter(eq(Post.isPublished, false)) {
posts.1 : Author.posts @filter(eq(Post.isPublished, false)) {
title : Post.title
text : Post.text
dgraph.uid : uid
Expand Down Expand Up @@ -2618,7 +2618,7 @@
text : Post.text
dgraph.uid : uid
}
posts1 : Author.posts @filter(eq(Post.isPublished, true)) {
posts.1 : Author.posts @filter(eq(Post.isPublished, true)) {
title : Post.title
text : Post.text
dgraph.uid : uid
Expand Down Expand Up @@ -2674,8 +2674,8 @@
query {
queryAuthor(func: type(Author)) {
count_postsAggregate : count(Author.posts)
count_postsAggregate1 : count(Author.posts) @filter(gt(Post.tags, "abc"))
count_postsAggregate2 : count(Author.posts) @filter(le(Post.tags, "xyz"))
count_postsAggregate.1 : count(Author.posts) @filter(gt(Post.tags, "abc"))
count_postsAggregate.2 : count(Author.posts) @filter(le(Post.tags, "xyz"))
dgraph.uid : uid
}
}
Expand Down

0 comments on commit 2bc0d56

Please sign in to comment.