Skip to content

Commit

Permalink
fix(GraphQL): fixes panic in update mutation without set & remove (#6073
Browse files Browse the repository at this point in the history
) (#6160)

FIxes GRAPHQL-589.
See [Discuss](https://discuss.dgraph.io/t/panic-when-update-with-filter-but-without-set-remove/8623) for more details.

(cherry picked from commit d82ecb4)
  • Loading branch information
abhimanyusinghgaur authored Aug 11, 2020
1 parent 80e85ab commit fd04c7e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func RunAll(t *testing.T) {
t.Run("mutations have extensions", mutationsHaveExtensions)
t.Run("alias works for mutations", mutationsWithAlias)
t.Run("three level deep", threeLevelDeepMutation)
t.Run("update mutation without set & remove", updateMutationWithoutSetRemove)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
30 changes: 29 additions & 1 deletion graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2908,7 +2908,6 @@ func threeLevelDeepMutation(t *testing.T) {
}

gqlResponse := postExecutor(t, graphqlURL, addStudentParams)
fmt.Println(string(gqlResponse.Data))
RequireNoGQLErrors(t, gqlResponse)

var actualResult struct {
Expand Down Expand Up @@ -3175,3 +3174,32 @@ func mutationsWithAlias(t *testing.T) {

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

func updateMutationWithoutSetRemove(t *testing.T) {
country := addCountry(t, postExecutor)

updateCountryParams := &GraphQLParams{
Query: `mutation updateCountry($id: ID!){
updateCountry(input: {filter: {id: [$id]}}) {
numUids
country {
id
name
}
}
}`,
Variables: map[string]interface{}{"id": country.ID},
}
gqlResponse := updateCountryParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, gqlResponse)

require.JSONEq(t, `{
"updateCountry": {
"numUids": 0,
"country": []
}
}`, string(gqlResponse.Data))

// cleanup
deleteCountry(t, map[string]interface{}{"id": []string{country.ID}}, 1, nil)
}
12 changes: 12 additions & 0 deletions graphql/resolve/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,18 @@ func (mr *dgraphResolver) rewriteAndExecute(ctx context.Context,
return emptyResult(schema.GQLWrapf(err, "couldn't rewrite mutation %s", mutation.Name())),
resolverFailed
}
if len(upserts) == 0 {
return &Resolved{
Data: map[string]interface{}{
mutation.Name(): map[string]interface{}{
schema.NumUid: 0,
mutation.QueryField().Name(): nil,
}},
Field: mutation,
Err: nil,
Extensions: ext,
}, resolverSucceeded
}

result := make(map[string]interface{})
req := &dgoapi.Request{}
Expand Down

0 comments on commit fd04c7e

Please sign in to comment.