Skip to content

Commit

Permalink
fix(GraphQL): Fix getType queries when id was used as a name for type…
Browse files Browse the repository at this point in the history
…s other than ID (#6130)

For the schema

type Tweets {
	id: String! @id
	score: Int
}

getTweets query was not being properly re-written. This is because we were using the name id to signify that a field is of type ID instead of the type ID itself. That has been fixed now. So getTweets would work if the field was named anything.

query MyQuery {
  getTweets(id: "1286891968727982081") {
    score
    id
  }
}

(cherry picked from commit bb8e4b9)
  • Loading branch information
pawanrawal authored and JatinDev543 committed Aug 13, 2020
1 parent 9cbe51f commit 722e428
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
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 @@ -731,8 +730,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 @@ -1608,7 +1616,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

0 comments on commit 722e428

Please sign in to comment.