Skip to content

Commit

Permalink
Add support for multiple mutation blocks (#4210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 authored Nov 27, 2019
1 parent b5d1beb commit 2cdbbf2
Show file tree
Hide file tree
Showing 6 changed files with 773 additions and 93 deletions.
60 changes: 48 additions & 12 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,27 +318,63 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
return
}

mu := &api.Mutation{}
req = &api.Request{Mutations: []*api.Mutation{mu}}
if setJSON, ok := ms["set"]; ok && setJSON != nil {
mu.SetJson = setJSON.bs
}
if delJSON, ok := ms["delete"]; ok && delJSON != nil {
mu.DeleteJson = delJSON.bs
}
req = &api.Request{}
if queryText, ok := ms["query"]; ok && queryText != nil {
req.Query, err = strconv.Unquote(string(queryText.bs))
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
}
if condText, ok := ms["cond"]; ok && condText != nil {
mu.Cond, err = strconv.Unquote(string(condText.bs))
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())

// JSON API support both keys 1. mutations 2. set,delete,cond
// We want to maintain the backward compatibility of the API here.
extractMutation := func(jsMap map[string]*skipJSONUnmarshal) (*api.Mutation, error) {
mu := &api.Mutation{}
empty := true
if setJSON, ok := jsMap["set"]; ok && setJSON != nil {
empty = false
mu.SetJson = setJSON.bs
}
if delJSON, ok := jsMap["delete"]; ok && delJSON != nil {
empty = false
mu.DeleteJson = delJSON.bs
}
if condText, ok := jsMap["cond"]; ok && condText != nil {
mu.Cond, err = strconv.Unquote(string(condText.bs))
if err != nil {
return nil, err
}
}

if empty {
return nil, nil
}

return mu, nil
}
if mu, err := extractMutation(ms); err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
} else if mu != nil {
req.Mutations = append(req.Mutations, mu)
}
if mus, ok := ms["mutations"]; ok && mus != nil {
var mm []map[string]*skipJSONUnmarshal
if err := json.Unmarshal(mus.bs, &mm); err != nil {
jsonErr := convertJSONError(string(mus.bs), err)
x.SetStatus(w, x.ErrorInvalidRequest, jsonErr.Error())
return
}

for _, m := range mm {
if mu, err := extractMutation(m); err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
} else if mu != nil {
req.Mutations = append(req.Mutations, mu)
}
}
}

case "application/rdf":
Expand Down
Loading

0 comments on commit 2cdbbf2

Please sign in to comment.