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

fix(GraphQL): Requesting only __typename now returns results #5659

Merged
merged 28 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f6dde05
Added case to handle typename issue
JatinDev543 Jun 16, 2020
7bf123c
fixed build issue
JatinDev543 Jun 16, 2020
5efa53d
Added tests
JatinDev543 Jun 17, 2020
7f6143d
Merge branch 'master' of https://github.com/dgraph-io/dgraph into jat…
JatinDev543 Jun 17, 2020
907c156
Modified code and test as per Michael review
JatinDev543 Jun 18, 2020
e73fba1
modified uid function
JatinDev543 Jun 22, 2020
e53e4f9
added auth check
JatinDev543 Jun 22, 2020
5a0144f
simlified function
JatinDev543 Jun 22, 2020
3df38ea
corrected logic
JatinDev543 Jun 22, 2020
1c22537
modified function
JatinDev543 Jun 22, 2020
3df3fd8
removed comment
JatinDev543 Jun 22, 2020
5e08fe3
Merge branch 'master' of https://github.com/dgraph-io/dgraph into jat…
JatinDev543 Jun 22, 2020
0893b4a
added numuid check
JatinDev543 Jun 23, 2020
4301d0c
added tests to common.go
JatinDev543 Jun 23, 2020
9d3c92d
modified code and tests
JatinDev543 Jun 23, 2020
31c0fa0
testing
JatinDev543 Jun 23, 2020
4154df0
fix
abhimanyusinghgaur Jun 24, 2020
0ab81c0
fix
abhimanyusinghgaur Jun 24, 2020
8a641d5
Changed dgraph.type to dgraph.uid a per pawan review
JatinDev543 Jun 24, 2020
abd6be0
added test for interface
JatinDev543 Jun 24, 2020
6fd891d
Merge branch 'master' of github.com:dgraph-io/dgraph into jatin/graph…
JatinDev543 Jun 25, 2020
c623ad2
add test
JatinDev543 Jun 25, 2020
d6fcc95
removed antipattern
JatinDev543 Jun 25, 2020
829c4ef
,odified test
JatinDev543 Jun 25, 2020
bd145a7
removed test
JatinDev543 Jun 25, 2020
3e30010
modified tests
JatinDev543 Jun 25, 2020
a6904df
added comment
JatinDev543 Jun 25, 2020
ba8d22a
Merge branch 'master' of github.com:dgraph-io/dgraph into jatin/graph…
JatinDev543 Jun 25, 2020
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
59 changes: 59 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,65 @@ func queryTypename(t *testing.T) {

}

func queryOnlyTypename(t *testing.T) {
getCountryParams := &GraphQLParams{
Query: `query queryCountry {
queryCountry {
__typename
}
}`,
}

gqlResponse := getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)

expected := `{
"queryCountry": [
{
"__typename": "Country"
},
{
"__typename": "Country"
},
{
"__typename": "Country"
}
]
}`
testutil.CompareJSON(t, expected, string(gqlResponse.Data))

}

func queryNestedOnlyTypename(t *testing.T) {
getCountryParams := &GraphQLParams{
Query: `query {
queryAuthor(filter: { name: { eq: "Ann Author" } }) {
posts {
__typename
}
}
}`,
}

gqlResponse := getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)

expected := `{
"queryAuthor": [
{
"posts": [
{
"__typename": "Post"
},
{
"__typename": "Post"
}
]
}
]
}`
testutil.CompareJSON(t, expected, string(gqlResponse.Data))
}
func queryNestedTypename(t *testing.T) {
getCountryParams := &GraphQLParams{
Query: `query {
Expand Down
5 changes: 5 additions & 0 deletions graphql/resolve/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (qr *queryResolver) rewriteAndExecute(ctx context.Context, query schema.Que
return emptyResult(schema.GQLWrapf(err, "Dgraph query failed"))
}

//if string(resp.GetJson()) == fmt.Sprintf(`{"%s":[]}`, query.Name()) {
// resp.Json = []byte(fmt.Sprintf(`{"%s":[{"dgraph.type":"%s"}]}`, query.Name(),
// query.Type().Name()))
// }

resolved := completeDgraphResult(ctx, query, resp.GetJson(), err)
resolved.Extensions =
&schema.Extensions{TouchedUids: resp.GetMetrics().GetNumUids()[touchedUidsKey]}
Expand Down
40 changes: 33 additions & 7 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,38 @@ func intersection(a, b []uint64) []uint64 {
// then Dgraph would just return 7 posts. And we'd have no way of knowing if
// there's only 7 posts, or if there's more that are missing 'text'.
// But, for GraphQL, we want to know about those missing values.
func addUID(dgQuery *gql.GraphQuery) {
if len(dgQuery.Children) == 0 {
func addUID(dgQuery *gql.GraphQuery, addIfNoChild bool) {
if len(dgQuery.Children) == 0 && addIfNoChild {
addUIDChild(dgQuery)
return
}
hasUid := false
for _, c := range dgQuery.Children {
if c.Attr == "uid" {
hasUid = true
}
addUID(c)
addUID(c, false)
}

// If uid was already requested by the user then we don't need to add it again.
if hasUid {
return
}
addUIDChild(dgQuery)
}

func addUIDChild(dgQuery *gql.GraphQuery) {
uidChild := &gql.GraphQuery{
Attr: "uid",
Alias: "dgraph.uid",
}
dgQuery.Children = append(dgQuery.Children, uidChild)
}
func (authRw *authRewriter) writingAuth() bool {
if authRw == nil || !authRw.isWritingAuth {
return false
}
return true
}

func rewriteAsQueryByIds(field schema.Field, uids []uint64, authRw *authRewriter) *gql.GraphQuery {
rbac := authRw.evaluateStaticRules(field.Type())
Expand All @@ -217,7 +227,13 @@ func rewriteAsQueryByIds(field schema.Field, uids []uint64, authRw *authRewriter

addArgumentsToField(dgQuery, field)
selectionAuth := addSelectionSetFrom(dgQuery, field, authRw)
addUID(dgQuery)

if authRw.writingAuth() {
addUID(dgQuery, true)
} else {
addUID(dgQuery, false)
}

addCascadeDirective(dgQuery, field)

if rbac == schema.Uncertain {
Expand Down Expand Up @@ -314,7 +330,11 @@ func rewriteAsGet(
}
}
selectionAuth := addSelectionSetFrom(dgQuery, field, auth)
addUID(dgQuery)
if auth.writingAuth() {
addUID(dgQuery, true)
} else {
addUID(dgQuery, false)
}
addTypeFilter(dgQuery, field.Type())
addCascadeDirective(dgQuery, field)

Expand Down Expand Up @@ -362,7 +382,13 @@ func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {

addArgumentsToField(dgQuery, field)
selectionAuth := addSelectionSetFrom(dgQuery, field, authRw)
addUID(dgQuery)

if authRw.writingAuth() {
addUID(dgQuery, true)
} else {
addUID(dgQuery, false)
}

addCascadeDirective(dgQuery, field)

if rbac == schema.Uncertain {
Expand Down