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

graphql: Updated mutation rewriting to fix OOM issue #5536

Merged
merged 24 commits into from
Jun 16, 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
2 changes: 1 addition & 1 deletion graphql/admin/add_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewAddGroupRewriter() resolve.MutationRewriter {
// A rule is duplicate if it has same predicate name as another rule.
func (mrw *addGroupRewriter) Rewrite(
ctx context.Context,
m schema.Mutation) (*resolve.UpsertMutation, error) {
m schema.Mutation) ([]*resolve.UpsertMutation, error) {

addGroupInput, _ := m.ArgValue(schema.InputArgName).([]interface{})

Expand Down
2 changes: 1 addition & 1 deletion graphql/admin/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type updateGQLSchemaInput struct {

func (asr *updateSchemaResolver) Rewrite(
ctx context.Context,
m schema.Mutation) (*resolve.UpsertMutation, error) {
m schema.Mutation) ([]*resolve.UpsertMutation, error) {

glog.Info("Got updateGQLSchema request")

Expand Down
7 changes: 4 additions & 3 deletions graphql/admin/update_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"context"
"fmt"

dgoapi "github.com/dgraph-io/dgo/v2/protos/api"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/graphql/resolve"
Expand All @@ -23,7 +24,7 @@ func NewUpdateGroupRewriter() resolve.MutationRewriter {
// name as another rule.
func (urw *updateGroupRewriter) Rewrite(
ctx context.Context,
m schema.Mutation) (*resolve.UpsertMutation, error) {
m schema.Mutation) ([]*resolve.UpsertMutation, error) {

inp := m.ArgValue(schema.InputArgName).(map[string]interface{})
setArg := inp["set"]
Expand Down Expand Up @@ -125,10 +126,10 @@ func (urw *updateGroupRewriter) Rewrite(
return nil, nil
}

return &resolve.UpsertMutation{
return []*resolve.UpsertMutation{{
Query: &gql.GraphQuery{Children: []*gql.GraphQuery{upsertQuery}},
Mutations: append(mutSet, mutDel...),
}, schema.GQLWrapf(schema.AppendGQLErrs(errSet, errDel), "failed to rewrite mutation payload")
}}, schema.GQLWrapf(schema.AppendGQLErrs(errSet, errDel), "failed to rewrite mutation payload")
}

// FromMutationResult rewrites the query part of a GraphQL update mutation into a Dgraph query.
Expand Down
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func RunAll(t *testing.T) {
t.Run("password in mutation", passwordTest)
t.Run("duplicate xid in single mutation", deepMutationDuplicateXIDsSameObjectTest)
t.Run("alias works for mutations", mutationsWithAlias)
t.Run("three level deep", threeLevelDeepMutation)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
83 changes: 78 additions & 5 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,8 @@ func updateMutationOnlyUpdatesRefsIfDifferent(t *testing.T, executeRequest reque
set: $set
}
) {
post {
postID
post {
postID
text
author { id }
}
Expand All @@ -1313,9 +1313,9 @@ func updateMutationOnlyUpdatesRefsIfDifferent(t *testing.T, executeRequest reque
// The text is updated as expected
// The author is unchanged
expected := fmt.Sprintf(`
{ "updatePost": { "post": [
{
"postID": "%s",
{ "updatePost": { "post": [
{
"postID": "%s",
"text": "The Updated Text",
"author": { "id": "%s" }
}
Expand Down Expand Up @@ -2863,6 +2863,79 @@ func passwordTest(t *testing.T) {
deleteUser(t, *newUser)
}

func threeLevelDeepMutation(t *testing.T) {
newStudent := &student{
Xid: "HS1",
Name: "Stud1",
TaughtBy: []*teacher{
{
Xid: "HT0",
Name: "Teacher0",
Subject: "English",
Teaches: []*student{{
Xid: "HS2",
Name: "Stud2",
}},
},
},
}

newStudents := []*student{newStudent}

addStudentParams := &GraphQLParams{
Query: `mutation addStudent($input: [AddStudentInput!]!) {
addStudent(input: $input) {
student {
xid
name
taughtBy {
id
xid
name
subject
teaches (order: {asc:xid}) {
xid
taughtBy {
name
xid
subject
}
}
}
}
}
}`,
Variables: map[string]interface{}{"input": newStudents},
}

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

var actualResult struct {
AddStudent struct {
Student []*student
}
}

err := json.Unmarshal(gqlResponse.Data, &actualResult)
require.NoError(t, err)

require.Equal(t, actualResult.AddStudent.Student[0].Xid, "HS1")
require.Equal(t, actualResult.AddStudent.Student[0].TaughtBy[0].Xid, "HT0")
require.Equal(t, actualResult.AddStudent.Student[0].TaughtBy[0].Teaches[0].Xid, "HS1")
require.Equal(t, actualResult.AddStudent.Student[0].TaughtBy[0].Teaches[0].TaughtBy[0].Xid, "HT0")
require.Equal(t, actualResult.AddStudent.Student[0].TaughtBy[0].Teaches[1].Xid, "HS2")
require.Equal(t, actualResult.AddStudent.Student[0].TaughtBy[0].Teaches[1].TaughtBy[0].Xid, "HT0")

// cleanup
filter := getXidFilter("xid", []string{"HS1", "HS2"})
deleteGqlType(t, "Student", filter, 2, nil)
filter = getXidFilter("xid", []string{"HT0"})
deleteGqlType(t, "Teacher", filter, 1, nil)

}

func deepMutationDuplicateXIDsSameObjectTest(t *testing.T) {
newStudents := []*student{
{
Expand Down
3 changes: 1 addition & 2 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ type Teacher implements People {
}

type Student implements People {
taughtBy: [Teacher]
taughtBy: [Teacher] @hasInverse(field: "teaches")
}

type Message {
content: String! @dgraph(pred: "post")
author: String @dgraph(pred: "<职业>")
Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ type Teacher implements People {
}

type Student implements People {
taughtBy: [Teacher]
taughtBy: [Teacher] @hasInverse(field: teaches)
}

Loading