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

Prevent empty values in fields having id directive. (#6177) #6197

Merged
merged 1 commit into from
Sep 1, 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
46 changes: 46 additions & 0 deletions graphql/resolve/add_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2218,3 +2218,49 @@
uid
}
}

-
name: "Add mutation error on @id field for empty value"
gqlmutation: |
mutation addState($input: AddStateInput!) {
addState(input: [$input]) {
state {
name
}
}
}
gqlvariables: |
{ "input":
{
"code": "",
"name": "NSW",
"country": { "id": "0x12" }
}
}
explanation: "The add mutation should not be allowed since value of @id field is empty."
error:
{ "message": "failed to rewrite mutation payload because encountered an empty value for @id field `State.code`" }

-
name: "Add mutation error on @id field for empty value (Nested)"
gqlmutation: |
mutation addCountry($input: AddCountryInput!) {
addCountry(input: [$input]) {
country {
name
}
}
}
gqlvariables: |
{ "input":
{
"name": "Dgraph Land",
"states": [ {
"code": "",
"name": "Dgraph"
} ]
}
}
explanation: "The add mutation should not be allowed since value of @id field is empty."
error:
{ "message": "failed to rewrite mutation payload because encountered an empty value for @id field `State.code`" }
7 changes: 7 additions & 0 deletions graphql/resolve/mutation_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,13 @@ func rewriteObject(
// e.g. to remove the text or
// { "friends": null, ... }
// to remove all friends

// Fields with `id` directive cannot have empty values.
if fieldDef.HasIDDirective() && val == "" {
errFrag := newFragment(nil)
errFrag.err = fmt.Errorf("encountered an empty value for @id field `%s`", fieldName)
return &mutationRes{secondPass: []*mutationFragment{errFrag}}
}
frags = &mutationRes{secondPass: []*mutationFragment{newFragment(val)}}
}

Expand Down
24 changes: 24 additions & 0 deletions graphql/resolve/update_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,30 @@
}
}

- name: "Update mutation error on @id field for empty value"
gqlmutation: |
mutation updateCountry($patch: UpdateCountryInput!) {
updateCountry(input: $patch) {
country {
id
}
}
}
gqlvariables: |
{
"patch": {
"filter": {
"id": ["0x123"]
},
"set": {
"states": [ { "code": "", "name": "Alphabet" } ]
}
}
}
explanation: "The update mutation should not be allowed since value of @id field is empty."
error:
{ "message": "failed to rewrite mutation payload because encountered an empty value for @id field `State.code`" }

- name: "Additional Deletes - Update references existing node by XID (updt single edge)"
gqlmutation: |
mutation updateComputerOwner($patch: UpdateComputerOwnerInput!) {
Expand Down
8 changes: 8 additions & 0 deletions graphql/schema/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type FieldDefinition interface {
Name() string
Type() Type
IsID() bool
HasIDDirective() bool
Inverse() FieldDefinition
// TODO - It might be possible to get rid of ForwardEdge and just use Inverse() always.
ForwardEdge() FieldDefinition
Expand Down Expand Up @@ -946,6 +947,13 @@ func (fd *fieldDefinition) IsID() bool {
return isID(fd.fieldDef)
}

func (fd *fieldDefinition) HasIDDirective() bool {
if fd.fieldDef == nil {
return false
}
return hasIDDirective(fd.fieldDef)
}

func hasIDDirective(fd *ast.FieldDefinition) bool {
id := fd.Directives.ForName("id")
return id != nil
Expand Down