Skip to content

Commit

Permalink
Feat(GraphQL): Add count queries feature at non-root levels (#6834)
Browse files Browse the repository at this point in the history
* Make graphql schema changes for child count queries

* Add resolve for count queries at child level

* Add e2e tests and refactor code

* Address comments

* Change aggregate query field name

* Fix failing tests

* Fix Tests

* Fix wrappers test
vmrajas authored Nov 4, 2020
1 parent c7ea4a1 commit 59e3406
Showing 44 changed files with 891 additions and 76 deletions.
79 changes: 79 additions & 0 deletions graphql/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1513,3 +1513,82 @@ func TestMain(m *testing.M) {
}
os.Exit(0)
}

func TestChildCountQueryWithDeepRBAC(t *testing.T) {
testCases := []TestCase{
{
user: "user1",
role: "USER",
result: `{"queryUser": [{"username": "user1", "issuesAggregate":{"count": null}}]}`},
{
user: "user1",
role: "ADMIN",
result: `{"queryUser":[{"username":"user1","issuesAggregate":{"count":1}}]}`},
}

query := `
query {
queryUser (filter:{username:{eq:"user1"}}) {
username
issuesAggregate {
count
}
}
}
`

for _, tcase := range testCases {
t.Run(tcase.role+tcase.user, func(t *testing.T) {
getUserParams := &common.GraphQLParams{
Headers: common.GetJWT(t, tcase.user, tcase.role, metaInfo),
Query: query,
}

gqlResponse := getUserParams.ExecuteAsPost(t, graphqlURL)
require.Nil(t, gqlResponse.Errors)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}
}

func TestChildCountQueryWithOtherFields(t *testing.T) {
testCases := []TestCase{
{
user: "user1",
role: "USER",
result: `{"queryUser": [{"username": "user1","issues":[],"issuesAggregate":{"count": null}}]}`},
{
user: "user1",
role: "ADMIN",
result: `{"queryUser":[{"username":"user1","issues":[{"msg":"Issue1"}],"issuesAggregate":{"count":1}}]}`},
}

query := `
query {
queryUser (filter:{username:{eq:"user1"}}) {
username
issuesAggregate {
count
}
issues {
msg
}
}
}
`

for _, tcase := range testCases {
t.Run(tcase.role+tcase.user, func(t *testing.T) {
getUserParams := &common.GraphQLParams{
Headers: common.GetJWT(t, tcase.user, tcase.role, metaInfo),
Query: query,
}

gqlResponse := getUserParams.ExecuteAsPost(t, graphqlURL)
require.Nil(t, gqlResponse.Errors)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
})
}
}
3 changes: 3 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
@@ -357,6 +357,9 @@ func RunAll(t *testing.T) {
t.Run("query count without filter", queryCountWithoutFilter)
t.Run("query count with filter", queryCountWithFilter)
t.Run("query count with alias", queryCountWithAlias)
t.Run("query count at child level", queryCountAtChildLevel)
t.Run("query count at child level with filter", queryCountAtChildLevelWithFilter)
t.Run("query count and other fields at child level", queryCountAndOtherFieldsAtChildLevel)

// mutation tests
t.Run("add mutation", addMutation)
7 changes: 4 additions & 3 deletions graphql/e2e/common/error.go
Original file line number Diff line number Diff line change
@@ -92,10 +92,11 @@ func graphQLCompletionOn(t *testing.T) {
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
require.NoError(t, err)
require.Equal(t, 4, len(result.QueryCountry))
require.Equal(t, 5, len(result.QueryCountry))
expected.QueryCountry = []*country{
&country{Name: "Angola"},
&country{Name: "Bangladesh"},
&country{Name: "India"},
&country{Name: "Mozambique"},
nil,
}
@@ -107,11 +108,11 @@ func graphQLCompletionOn(t *testing.T) {
return result.QueryCountry[i].Name < result.QueryCountry[j].Name
})

for i := 0; i < 3; i++ {
for i := 0; i < 4; i++ {
require.NotNil(t, result.QueryCountry[i])
require.Equal(t, result.QueryCountry[i].Name, expected.QueryCountry[i].Name)
}
require.Nil(t, result.QueryCountry[3])
require.Nil(t, result.QueryCountry[4])
})
}

104 changes: 103 additions & 1 deletion graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
@@ -153,6 +153,7 @@ func queryByTypeWithEncoding(t *testing.T, acceptGzip, gzipEncoding bool) {
expected.QueryCountry = []*country{
&country{Name: "Angola"},
&country{Name: "Bangladesh"},
&country{Name: "India"},
&country{Name: "Mozambique"},
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
@@ -188,6 +189,7 @@ func uidAlias(t *testing.T) {
expected.QueryCountry = []*countryUID{
&countryUID{UID: "Angola"},
&countryUID{UID: "Bangladesh"},
&countryUID{UID: "India"},
&countryUID{UID: "Mozambique"},
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
@@ -216,6 +218,7 @@ func orderAtRoot(t *testing.T) {
expected.QueryCountry = []*country{
&country{Name: "Angola"},
&country{Name: "Bangladesh"},
&country{Name: "India"},
&country{Name: "Mozambique"},
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
@@ -242,8 +245,8 @@ func pageAtRoot(t *testing.T) {
QueryCountry []*country
}
expected.QueryCountry = []*country{
&country{Name: "India"},
&country{Name: "Bangladesh"},
&country{Name: "Angola"},
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
require.NoError(t, err)
@@ -1356,6 +1359,7 @@ func queryApplicationGraphQl(t *testing.T) {
"queryCountry": [
{ "name": "Angola"},
{ "name": "Bangladesh"},
{ "name": "India"},
{ "name": "Mozambique"}
]
}`
@@ -1385,6 +1389,10 @@ func queryTypename(t *testing.T) {
{
"name": "Bangladesh",
"__typename": "Country"
},
{
"name": "India",
"__typename": "Country"
},
{
"name": "Mozambique",
@@ -2819,3 +2827,97 @@ func queryCountWithAlias(t *testing.T) {
`{"aggregatePost":{"cnt":4}}`,
string(gqlResponse.Data))
}

func queryCountAtChildLevel(t *testing.T) {
queryNumberOfStates := &GraphQLParams{
Query: `query
{
queryCountry(filter: { name: { eq: "India" } }) {
name
ag : statesAggregate {
count
}
}
}`,
}
gqlResponse := queryNumberOfStates.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`
{
"queryCountry": [{
"name": "India",
"ag": {
"count" : 3
}
}]
}`,
string(gqlResponse.Data))
}

func queryCountAtChildLevelWithFilter(t *testing.T) {
queryNumberOfIndianStates := &GraphQLParams{
Query: `query
{
queryCountry(filter: { name: { eq: "India" } }) {
name
ag : statesAggregate(filter: {xcode: {in: ["ka", "mh"]}}) {
count
}
}
}`,
}
gqlResponse := queryNumberOfIndianStates.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`
{
"queryCountry": [{
"name": "India",
"ag": {
"count" : 2
}
}]
}`,
string(gqlResponse.Data))
}

func queryCountAndOtherFieldsAtChildLevel(t *testing.T) {
queryNumberOfIndianStates := &GraphQLParams{
Query: `query
{
queryCountry(filter: { name: { eq: "India" } }) {
name
ag : statesAggregate {
count
},
states {
name
}
}
}`,
}
gqlResponse := queryNumberOfIndianStates.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`
{
"queryCountry": [{
"name": "India",
"ag": {
"count" : 3
},
"states": [
{
"name": "Maharashtra"
},
{
"name": "Gujarat"
},
{
"name": "Karnataka"
}]
}]
}`,
string(gqlResponse.Data))
}
6 changes: 5 additions & 1 deletion graphql/e2e/common/schema.go
Original file line number Diff line number Diff line change
@@ -86,7 +86,11 @@ const (
{
"name": "rank",
"description": ""
}
},
{
"name": "postsAggregate",
"description": ""
}
],
"enumValues":[]
}, "__typename" : "Query" }`
4 changes: 2 additions & 2 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ type State {
id: ID!
xcode: String! @id @search(by: [regexp])
name: String!
capital: String
country: Country @dgraph(pred: "inCountry")
capital: String
country: Country @dgraph(pred: "inCountry")
}

# **Don't delete** Comments in the middle of schemas should work
27 changes: 27 additions & 0 deletions graphql/e2e/directives/test_data.json
Original file line number Diff line number Diff line change
@@ -103,5 +103,32 @@
"dgraph.type": "State",
"State.name": "Nusa",
"State.xcode": "nusa"
},
{
"uid": "_:mh",
"dgraph.type": "State",
"State.name": "Maharashtra",
"State.xcode": "mh",
"inCountry": {"uid": "_:india"}
},
{
"uid": "_:gj",
"dgraph.type": "State",
"State.name": "Gujarat",
"State.xcode": "gj",
"inCountry": {"uid": "_:india"}
},
{
"uid": "_:ka",
"dgraph.type": "State",
"State.name": "Karnataka",
"State.xcode": "ka",
"inCountry": {"uid": "_:india"}
},
{
"uid": "_:india",
"dgraph.type": "Country",
"Country.name": "India",
"hasStates": [{ "uid": "_:mh" }, { "uid": "_:gj" }, { "uid": "_:ka"}]
}
]
4 changes: 2 additions & 2 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ type State {
id: ID!
xcode: String! @id @search(by: [regexp])
name: String!
capital: String
country: Country
capital: String
country: Country
}

# **Don't delete** Comments in the middle of schemas should work
27 changes: 27 additions & 0 deletions graphql/e2e/normal/test_data.json
Original file line number Diff line number Diff line change
@@ -103,5 +103,32 @@
"dgraph.type": "State",
"State.name": "Nusa",
"State.xcode": "nusa"
},
{
"uid": "_:mh",
"dgraph.type": "State",
"State.name": "Maharashtra",
"State.xcode": "mh",
"State.country": {"uid": "_:india"}
},
{
"uid": "_:gj",
"dgraph.type": "State",
"State.name": "Gujarat",
"State.xcode": "gj",
"State.country": {"uid": "_:india"}
},
{
"uid": "_:ka",
"dgraph.type": "State",
"State.name": "Karnataka",
"State.xcode": "ka",
"State.country": {"uid": "_:india"}
},
{
"uid": "_:india",
"dgraph.type": "Country",
"Country.name": "India",
"Country.states": [{ "uid": "_:mh" }, { "uid": "_:gj" }, { "uid": "_:ka"}]
}
]
Loading

0 comments on commit 59e3406

Please sign in to comment.