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

Feat(GraphQL): Add count queries feature at non-root levels #6834

Merged
merged 8 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
79 changes: 79 additions & 0 deletions graphql/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -355,6 +355,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)
Expand Down
7 changes: 4 additions & 3 deletions graphql/e2e/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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])
})
}

Expand Down
104 changes: 103 additions & 1 deletion graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -1356,6 +1359,7 @@ func queryApplicationGraphQl(t *testing.T) {
"queryCountry": [
{ "name": "Angola"},
{ "name": "Bangladesh"},
{ "name": "India"},
{ "name": "Mozambique"}
]
}`
Expand Down Expand Up @@ -1385,6 +1389,10 @@ func queryTypename(t *testing.T) {
{
"name": "Bangladesh",
"__typename": "Country"
},
{
"name": "India",
"__typename": "Country"
},
{
"name": "Mozambique",
Expand Down Expand Up @@ -2433,3 +2441,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
Expand Up @@ -82,7 +82,11 @@ const (
{
"name": "rank",
"description": ""
}
},
{
"name": "postsAggregate",
"description": ""
}
],
"enumValues":[]
}, "__typename" : "Query" }`
Expand Down
4 changes: 2 additions & 2 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions graphql/e2e/directives/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions graphql/e2e/normal/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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