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 18 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
2 changes: 2 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ func RunAll(t *testing.T) {
t.Run("query typename", queryTypename)
t.Run("query nested typename", queryNestedTypename)
t.Run("typename for interface", typenameForInterface)
t.Run("query only typename", queryOnlyTypename)
t.Run("query nested only typename", queryNestedOnlyTypename)

t.Run("get state by xid", getStateByXid)
t.Run("get state without args", getStateWithoutArgs)
Expand Down
95 changes: 95 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,101 @@ func queryNestedTypename(t *testing.T) {
testutil.CompareJSON(t, expected, string(gqlResponse.Data))
}

func queryOnlyTypename(t *testing.T) {
var countryCountResp struct {
QueryCountry []*country
}

getCountryParams := &GraphQLParams{
Query: `query queryCountry {
queryCountry {
id
}
}`,
}
gqlResponse := getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)
require.NoError(t, json.Unmarshal([]byte(gqlResponse.Data), &countryCountResp))
require.True(t, len(countryCountResp.QueryCountry) > 0)

getCountryParams = &GraphQLParams{
Query: `query queryCountry {
queryCountry {
__typename
}
}`,
}
gqlResponse = getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)

expectedResponse := `{
"queryCountry": [%s
]
}`
var builder strings.Builder
for i := 0; i < len(countryCountResp.QueryCountry); i++ {
builder.WriteString(`
{
"__typename": "Country"
}`)
if i != len(countryCountResp.QueryCountry)-1 {
builder.WriteString(",")
}
}
expectedResponse = fmt.Sprintf(expectedResponse, builder.String())

require.JSONEq(t, expectedResponse, string(gqlResponse.Data))
}

func queryNestedOnlyTypename(t *testing.T) {
var authorCountResp struct {
QueryAuthor []*author
}

getCountryParams := &GraphQLParams{
Query: `query {
queryAuthor {
id
}
}`,
}
gqlResponse := getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)
require.NoError(t, json.Unmarshal([]byte(gqlResponse.Data), &authorCountResp))
require.True(t, len(authorCountResp.QueryAuthor) > 0)

getCountryParams = &GraphQLParams{
Query: `query {
queryAuthor {
country {
__typename
}
}
}`,
}
gqlResponse = getCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)

expectedResponse := `{
"queryAuthor": [%s
]
}`
var builder strings.Builder
for i := 0; i < len(authorCountResp.QueryAuthor); i++ {
builder.WriteString(`
{
"country": {
"__typename": "Country"
}
}`)
if i != len(authorCountResp.QueryAuthor)-1 {
builder.WriteString(",")
}
}
expectedResponse = fmt.Sprintf(expectedResponse, builder.String())

require.JSONEq(t, expectedResponse, string(gqlResponse.Data))
}
func typenameForInterface(t *testing.T) {
newStarship := addStarship(t)
humanID := addHuman(t, newStarship.ID)
Expand Down
15 changes: 14 additions & 1 deletion graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {
return dgQuery
}

func (authRw *authRewriter) writingAuth() bool {
if authRw == nil || !authRw.isWritingAuth {
return false
}
return true
}

// addAuthQueries takes a field and the GraphQuery that has so far been constructed for
// the field and builds any auth queries that are need to restrict the result to only
// the nodes authorized to be queried, returning a new graphQuery that does the
Expand Down Expand Up @@ -596,11 +603,17 @@ func addSelectionSetFrom(
// Only add dgraph.type as a child if this field is an interface type and has some children.
// dgraph.type would later be used in completeObject as different objects in the resulting
// JSON would return different fields based on their concrete type.
if field.InterfaceType() && len(field.SelectionSet()) > 0 {
selSet := field.SelectionSet()
if len(selSet) > 0 &&
(field.InterfaceType() ||
(!auth.writingAuth() &&
len(selSet) == 1 &&
selSet[0].Name() == schema.Typename)) {
q.Children = append(q.Children, &gql.GraphQuery{
Attr: "dgraph.type",
})
}
selSet = nil

// These fields might not have been requested by the user directly as part of the query but
// are required in the body template for other fields requested within the query. We must
Expand Down