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): Fix getType queries when id was used as a name for types other than ID #6130

Merged
merged 4 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 18 additions & 2 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@
}
}
}
dgquery: |
dgquery: |-
query {
getPost(func: uid(0x1)) @filter(type(Post)) {
postID : uid
Expand All @@ -1338,4 +1338,20 @@
url : Comment.url
}
}
}
}
- name: "getType by id should work"
gqlquery: |-
query {
getTweets(id: "1286891968727982081") {
score
id
}
}
dgquery: |-
query {
getTweets(func: eq(Tweets.id, "1286891968727982081")) @filter(type(Tweets)) {
score : Tweets.score
id : Tweets.id
dgraph.uid : uid
}
}
5 changes: 5 additions & 0 deletions graphql/resolve/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,8 @@ type Y implements X @auth(
){
userRole: String @search(by: [hash])
}

type Tweets {
id: String! @id
score: Int
}
16 changes: 12 additions & 4 deletions graphql/schema/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const (
HTTPMutation MutationType = "http"
NotSupportedMutation MutationType = "notsupported"
IDType = "ID"
IDArgName = "id"
InputArgName = "input"
FilterArgName = "filter"
)
Expand Down Expand Up @@ -735,8 +734,17 @@ func (f *field) HasCustomDirective() (bool, map[string]bool) {
func (f *field) XIDArg() string {
xidArgName := ""
passwordField := f.Type().PasswordField()
for _, arg := range f.field.Arguments {
if arg.Name != IDArgName && (passwordField == nil ||

args := f.field.Definition.Arguments
if len(f.field.Definition.Arguments) == 0 {
// For acl endpoints like getCurrentUser which redirects to getUser resolver, the field
// definition doesn't change and hence we can't find the arguments for getUser. As a
// fallback, we get the args from the query field arguments in that case.
args = f.op.inSchema.schema.Query.Fields.ForName(f.Name()).Arguments
}

for _, arg := range args {
if arg.Type.Name() != IDType && (passwordField == nil ||
arg.Name != passwordField.Name()) {
xidArgName = arg.Name
}
Expand Down Expand Up @@ -1612,7 +1620,7 @@ func (t *astType) Interfaces() []string {
// satisfy a valid post.
func (t *astType) EnsureNonNulls(obj map[string]interface{}, exclusion string) error {
for _, fld := range t.inSchema.schema.Types[t.Name()].Fields {
if fld.Type.NonNull && !isID(fld) && !(fld.Name == exclusion) {
if fld.Type.NonNull && !isID(fld) && fld.Name != exclusion {
if val, ok := obj[fld.Name]; !ok || val == nil {
return errors.Errorf(
"type %s requires a value for field %s, but no value present",
Expand Down