From 7f332df86dc46ed897a51d473f4456010cde0c87 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 12:07:41 +0530 Subject: [PATCH 01/14] fix mutation with Xid Int variables --- graphql/e2e/common/mutation.go | 7 ++- graphql/resolve/mutation_rewriter.go | 68 ++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 88a32dd2e0b..9b369e3a176 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4886,10 +4886,10 @@ func filterInUpdateMutationsWithFilterAndOr(t *testing.T) { func idDirectiveWithInt64Mutation(t *testing.T) { query := &GraphQLParams{ - Query: `mutation { + Query: `mutation addBook($bookId: Int64!){ addBook(input:[ { - bookId: 1234567890123 + bookId: $bookId name: "Graphql" desc: "Graphql is the next big thing" } @@ -4897,6 +4897,9 @@ func idDirectiveWithInt64Mutation(t *testing.T) { numUids } }`, + Variables: map[string]interface{}{ + "bookId": 1234567890123, + } } response := query.ExecuteAsPost(t, GraphqlURL) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index e7fabbcb97c..f9f9313d009 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -1382,12 +1382,24 @@ func rewriteObject( if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { // TODO: Add a function for parsing idVal. This is repeatitive switch xid.Type().Name() { - case "Int": - val, _ := xidVal.(int64) - xidString = strconv.FormatInt(val, 10) + case "Int", "Int64": + switch xidVal.(type) { + case json.Number: + val, _ := xidVal.(json.Number).Int64() + xidString = strconv.FormatInt(val, 10) + default: + val, _ := xidVal.(int64) + xidString = strconv.FormatInt(val, 10) + } case "Float": - val, _ := xidVal.(float64) - xidString = strconv.FormatFloat(val, 'f', -1, 64) + switch xidVal.(type) { + case json.Number: + val, _ := xidVal.(json.Number).Float64() + xidString = strconv.FormatFloat(val, 'f', -1, 64) + default: + val, _ := xidVal.(float64) + xidString = strconv.FormatFloat(val, 'f', -1, 64) + } default: xidString, _ = xidVal.(string) } @@ -1728,22 +1740,42 @@ func existenceQueries( for _, xid := range xids { if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { switch xid.Type().Name() { - case "Int": - val, ok := xidVal.(int64) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int but data type in schema is Int", xid.Name(), xid.Type().Name()))) - return nil, retErrors + case "Int", "Int64": + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Int64() + if err != nil { + retErrors = append(retErrors, err) + return nil, retErrors + } + xidString = strconv.FormatInt(val, 10) + default: + val, ok := xidVal.(int64) + if !ok { + retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int but data type in schema is Int", xid.Name(), xid.Type().Name()))) + return nil, retErrors + } + xidString = strconv.FormatInt(val, 10) } - xidString = strconv.FormatInt(val, 10) case "Float": - val, ok := xidVal.(float64) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Float but data type in schema is Float", xid.Name(), xid.Type().Name()))) - return nil, retErrors + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Float64() + if err != nil { + retErrors = append(retErrors, err) + return nil, retErrors + } + xidString = strconv.FormatFloat(val, 'f', -1, 64) + default: + val, ok := xidVal.(float64) + if !ok { + retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Float but data type in schema is Float", xid.Name(), xid.Type().Name()))) + return nil, retErrors + } + xidString = strconv.FormatFloat(val, 'f', -1, 64) } - xidString = strconv.FormatFloat(val, 'f', -1, 64) default: xidString, ok = xidVal.(string) if !ok { From bc1d25260a10da4857416ed9e6bfd88c28b80589 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 12:25:00 +0530 Subject: [PATCH 02/14] fix e2e test --- graphql/e2e/common/mutation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 9b369e3a176..fc76d428f2d 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4899,7 +4899,7 @@ func idDirectiveWithInt64Mutation(t *testing.T) { }`, Variables: map[string]interface{}{ "bookId": 1234567890123, - } + }, } response := query.ExecuteAsPost(t, GraphqlURL) From c5b469b1ea2c4d9575646698244472883c6d758f Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 12:49:45 +0530 Subject: [PATCH 03/14] remove int64 --- graphql/resolve/mutation_rewriter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index f9f9313d009..cd1f2ec406a 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -1382,7 +1382,7 @@ func rewriteObject( if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { // TODO: Add a function for parsing idVal. This is repeatitive switch xid.Type().Name() { - case "Int", "Int64": + case "Int": switch xidVal.(type) { case json.Number: val, _ := xidVal.(json.Number).Int64() @@ -1740,7 +1740,7 @@ func existenceQueries( for _, xid := range xids { if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { switch xid.Type().Name() { - case "Int", "Int64": + case "Int": switch xidVal.(type) { case json.Number: val, err := xidVal.(json.Number).Int64() From 58e7fafa1b21c562f9c2157c4062a002e5873248 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 16:04:24 +0530 Subject: [PATCH 04/14] handle int64 --- graphql/resolve/mutation_rewriter.go | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index cd1f2ec406a..8381aa72f6d 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -1391,6 +1391,17 @@ func rewriteObject( val, _ := xidVal.(int64) xidString = strconv.FormatInt(val, 10) } + case "Int64": + switch xidVal.(type) { + case json.Number: + val, _ := xidVal.(json.Number).Int64() + xidString = strconv.FormatInt(val, 10) + case int64: + val, _ := xidVal.(int64) + xidString = strconv.FormatInt(val, 10) + default: + xidString, _ = xidVal.(string) + } case "Float": switch xidVal.(type) { case json.Number: @@ -1758,6 +1769,31 @@ func existenceQueries( } xidString = strconv.FormatInt(val, 10) } + case "Int64": + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Int64() + if err != nil { + retErrors = append(retErrors, err) + return nil, retErrors + } + xidString = strconv.FormatInt(val, 10) + case int64: + val, ok := xidVal.(int64) + if !ok { + retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int64 but data type in schema is Int64", xid.Name(), xid.Type().Name()))) + return nil, retErrors + } + xidString = strconv.FormatInt(val, 10) + default: + xidString, ok = xidVal.(string) + if !ok { + retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int64", xid.Name(), xid.Type().Name()))) + return nil, retErrors + } + } case "Float": switch xidVal.(type) { case json.Number: @@ -1780,7 +1816,7 @@ func existenceQueries( xidString, ok = xidVal.(string) if !ok { retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a String or Int64", xid.Name(), xid.Type().Name()))) + "a String", xid.Name(), xid.Type().Name()))) return nil, retErrors } } From 36f03b7b95c9c88884f143a81c49a365e1ffa13c Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 16:08:29 +0530 Subject: [PATCH 05/14] modify e2e test --- graphql/e2e/common/mutation.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index fc76d428f2d..3491d2bcdc5 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4898,7 +4898,7 @@ func idDirectiveWithInt64Mutation(t *testing.T) { } }`, Variables: map[string]interface{}{ - "bookId": 1234567890123, + "bookId": "1234567890123", }, } @@ -4920,14 +4920,17 @@ func idDirectiveWithInt64Mutation(t *testing.T) { func idDirectiveWithIntMutation(t *testing.T) { query := &GraphQLParams{ - Query: `mutation { + Query: `mutation addChapter($chId: Int!){ addChapter(input:[{ - chapterId: 2 + chapterId: $chId name: "Graphql and more" }]) { numUids } }`, + Variables: map[string]interface{}{ + "chId": 1, + }, } response := query.ExecuteAsPost(t, GraphqlURL) From 4607fad6706cf395c4450438df9c060fadbf1821 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 16:24:25 +0530 Subject: [PATCH 06/14] fix e2e test --- graphql/e2e/common/mutation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 3491d2bcdc5..61490fdff5e 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4929,7 +4929,7 @@ func idDirectiveWithIntMutation(t *testing.T) { } }`, Variables: map[string]interface{}{ - "chId": 1, + "chId": 2, }, } From 7f2c24035c418426a9a5b55926a614973e91924c Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 19:22:13 +0530 Subject: [PATCH 07/14] restore old cases --- graphql/e2e/common/mutation.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 61490fdff5e..7aa83da03f5 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4889,16 +4889,21 @@ func idDirectiveWithInt64Mutation(t *testing.T) { Query: `mutation addBook($bookId: Int64!){ addBook(input:[ { - bookId: $bookId + bookId: 1234567890123 name: "Graphql" desc: "Graphql is the next big thing" - } + }, + { + bookId: $bookId + name: "Dgraph" + desc: "A GraphQL database" + } ]) { numUids } }`, Variables: map[string]interface{}{ - "bookId": "1234567890123", + "bookId": "1234512345", }, } @@ -4906,7 +4911,7 @@ func idDirectiveWithInt64Mutation(t *testing.T) { RequireNoGQLErrors(t, response) expected := `{ "addBook": { - "numUids": 1 + "numUids": 2 } }` require.JSONEq(t, expected, string(response.Data)) @@ -4915,21 +4920,25 @@ func idDirectiveWithInt64Mutation(t *testing.T) { response = query.ExecuteAsPost(t, GraphqlURL) require.Contains(t, response.Errors.Error(), "already exists") - DeleteGqlType(t, "Book", map[string]interface{}{}, 2, nil) + DeleteGqlType(t, "Book", map[string]interface{}{}, 3, nil) } func idDirectiveWithIntMutation(t *testing.T) { query := &GraphQLParams{ Query: `mutation addChapter($chId: Int!){ addChapter(input:[{ - chapterId: $chId + chapterId: 2 name: "Graphql and more" + }, + { + chapterId: $chID + name: "Authorization" }]) { numUids } }`, Variables: map[string]interface{}{ - "chId": 2, + "chId": 10, }, } @@ -4937,7 +4946,7 @@ func idDirectiveWithIntMutation(t *testing.T) { RequireNoGQLErrors(t, response) var expected = `{ "addChapter": { - "numUids": 1 + "numUids": 2 } }` require.JSONEq(t, expected, string(response.Data)) @@ -4946,7 +4955,7 @@ func idDirectiveWithIntMutation(t *testing.T) { response = query.ExecuteAsPost(t, GraphqlURL) require.Contains(t, response.Errors.Error(), "already exists") - DeleteGqlType(t, "Chapter", map[string]interface{}{}, 2, nil) + DeleteGqlType(t, "Chapter", map[string]interface{}{}, 3, nil) } func idDirectiveWithFloatMutation(t *testing.T) { From a0cb23086e47e1fb79ab0be57135d124328d994b Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 19:37:16 +0530 Subject: [PATCH 08/14] fix test --- graphql/e2e/common/mutation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 7aa83da03f5..8dce73b120a 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4931,7 +4931,7 @@ func idDirectiveWithIntMutation(t *testing.T) { name: "Graphql and more" }, { - chapterId: $chID + chapterId: $chId name: "Authorization" }]) { numUids From ed3d0fb1293e786e095719a10807388c5e69359d Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 15 Mar 2021 19:56:12 +0530 Subject: [PATCH 09/14] create function for extractVal --- graphql/resolve/mutation_rewriter.go | 177 +++++++++++---------------- 1 file changed, 74 insertions(+), 103 deletions(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index 8381aa72f6d..1c6f2f6f86c 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -1380,40 +1380,7 @@ func rewriteObject( for _, xid := range xids { var xidString string if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { - // TODO: Add a function for parsing idVal. This is repeatitive - switch xid.Type().Name() { - case "Int": - switch xidVal.(type) { - case json.Number: - val, _ := xidVal.(json.Number).Int64() - xidString = strconv.FormatInt(val, 10) - default: - val, _ := xidVal.(int64) - xidString = strconv.FormatInt(val, 10) - } - case "Int64": - switch xidVal.(type) { - case json.Number: - val, _ := xidVal.(json.Number).Int64() - xidString = strconv.FormatInt(val, 10) - case int64: - val, _ := xidVal.(int64) - xidString = strconv.FormatInt(val, 10) - default: - xidString, _ = xidVal.(string) - } - case "Float": - switch xidVal.(type) { - case json.Number: - val, _ := xidVal.(json.Number).Float64() - xidString = strconv.FormatFloat(val, 'f', -1, 64) - default: - val, _ := xidVal.(float64) - xidString = strconv.FormatFloat(val, 'f', -1, 64) - } - default: - xidString, _ = xidVal.(string) - } + xidString, _ = extractVal(xidVal, xid.Name(), xid.Type().Name()) variable = varGen.Next(typ, xid.Name(), xidString, false) // Three cases: @@ -1747,78 +1714,13 @@ func existenceQueries( xids := typ.XIDFields() var xidString string + var err error if len(xids) != 0 { for _, xid := range xids { if xidVal, ok := obj[xid.Name()]; ok && xidVal != nil { - switch xid.Type().Name() { - case "Int": - switch xidVal.(type) { - case json.Number: - val, err := xidVal.(json.Number).Int64() - if err != nil { - retErrors = append(retErrors, err) - return nil, retErrors - } - xidString = strconv.FormatInt(val, 10) - default: - val, ok := xidVal.(int64) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int but data type in schema is Int", xid.Name(), xid.Type().Name()))) - return nil, retErrors - } - xidString = strconv.FormatInt(val, 10) - } - case "Int64": - switch xidVal.(type) { - case json.Number: - val, err := xidVal.(json.Number).Int64() - if err != nil { - retErrors = append(retErrors, err) - return nil, retErrors - } - xidString = strconv.FormatInt(val, 10) - case int64: - val, ok := xidVal.(int64) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int64 but data type in schema is Int64", xid.Name(), xid.Type().Name()))) - return nil, retErrors - } - xidString = strconv.FormatInt(val, 10) - default: - xidString, ok = xidVal.(string) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int64", xid.Name(), xid.Type().Name()))) - return nil, retErrors - } - } - case "Float": - switch xidVal.(type) { - case json.Number: - val, err := xidVal.(json.Number).Float64() - if err != nil { - retErrors = append(retErrors, err) - return nil, retErrors - } - xidString = strconv.FormatFloat(val, 'f', -1, 64) - default: - val, ok := xidVal.(float64) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Float but data type in schema is Float", xid.Name(), xid.Type().Name()))) - return nil, retErrors - } - xidString = strconv.FormatFloat(val, 'f', -1, 64) - } - default: - xidString, ok = xidVal.(string) - if !ok { - retErrors = append(retErrors, errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a String", xid.Name(), xid.Type().Name()))) - return nil, retErrors - } + xidString, err = extractVal(xidVal, xid.Name(), xid.Type().Name()) + if err != nil { + return nil, append(retErrors, err) } variable := varGen.Next(typ, xid.Name(), xidString, false) // There are two cases: @@ -2355,3 +2257,72 @@ func copyTypeMap(from, to map[string]schema.Type) { to[name] = typ } } + +func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { + switch typeName { + case "Int": + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Int64() + if err != nil { + return "", err + } + return strconv.FormatInt(val, 10), nil + default: + val, ok := xidVal.(int64) + if !ok { + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int but data type in schema is Int", xidName, typeName)) + } + return strconv.FormatInt(val, 10), nil + } + case "Int64": + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Int64() + if err != nil { + return "", err + } + return strconv.FormatInt(val, 10), nil + case int64: + val, ok := xidVal.(int64) + if !ok { + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int64 but data type in schema is Int64", xidName, typeName)) + } + return strconv.FormatInt(val, 10), nil + // If the xid field is of type Int64, both String and Int forms are allowed as per spec. + // The default case handles id xid is passed as string. + default: + xidString, ok := xidVal.(string) + if !ok { + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int64", xidName, typeName)) + } + return xidString, nil + } + case "Float": + switch xidVal.(type) { + case json.Number: + val, err := xidVal.(json.Number).Float64() + if err != nil { + return "", err + } + return strconv.FormatFloat(val, 'f', -1, 64), nil + default: + val, ok := xidVal.(float64) + if !ok { + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Float but data type in schema is Float", xidName, typeName)) + } + return strconv.FormatFloat(val, 'f', -1, 64), nil + } + default: + xidString, ok := xidVal.(string) + if !ok { + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a String", xidName, typeName)) + } + return xidString, nil + } +} From 36b41349f219c8cf9415b90359d7cb948ff372cd Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Tue, 16 Mar 2021 11:40:13 +0530 Subject: [PATCH 10/14] add book --- graphql/e2e/common/mutation.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 8dce73b120a..6dc550b19cf 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4894,16 +4894,22 @@ func idDirectiveWithInt64Mutation(t *testing.T) { desc: "Graphql is the next big thing" }, { - bookId: $bookId + bookId: $bookId2 name: "Dgraph" desc: "A GraphQL database" - } + }, + { + bookId: $bookId3 + name: "DQL" + desc: "Query Language for Dgraph" + } ]) { numUids } }`, Variables: map[string]interface{}{ - "bookId": "1234512345", + "bookId2": "1234512345", + "bookId3": 5432154321, }, } @@ -4911,7 +4917,7 @@ func idDirectiveWithInt64Mutation(t *testing.T) { RequireNoGQLErrors(t, response) expected := `{ "addBook": { - "numUids": 2 + "numUids": 3 } }` require.JSONEq(t, expected, string(response.Data)) @@ -4920,7 +4926,7 @@ func idDirectiveWithInt64Mutation(t *testing.T) { response = query.ExecuteAsPost(t, GraphqlURL) require.Contains(t, response.Errors.Error(), "already exists") - DeleteGqlType(t, "Book", map[string]interface{}{}, 3, nil) + DeleteGqlType(t, "Book", map[string]interface{}{}, 4, nil) } func idDirectiveWithIntMutation(t *testing.T) { From f287df5ce070855a62211c9366555ffb99171b2b Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Tue, 16 Mar 2021 11:54:08 +0530 Subject: [PATCH 11/14] add variable --- graphql/e2e/common/mutation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/e2e/common/mutation.go b/graphql/e2e/common/mutation.go index 6dc550b19cf..33c869edc63 100644 --- a/graphql/e2e/common/mutation.go +++ b/graphql/e2e/common/mutation.go @@ -4886,7 +4886,7 @@ func filterInUpdateMutationsWithFilterAndOr(t *testing.T) { func idDirectiveWithInt64Mutation(t *testing.T) { query := &GraphQLParams{ - Query: `mutation addBook($bookId: Int64!){ + Query: `mutation addBook($bookId2: Int64!, $bookId3: Int64!){ addBook(input:[ { bookId: 1234567890123 From 344bdcc26f08ed1c478c673638ad5597dcf715c4 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Tue, 16 Mar 2021 16:03:40 +0530 Subject: [PATCH 12/14] address Comments --- graphql/resolve/mutation_rewriter.go | 57 ++++++++++++---------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index 1c6f2f6f86c..b4c83f77468 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -2261,68 +2261,59 @@ func copyTypeMap(from, to map[string]schema.Type) { func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { switch typeName { case "Int": - switch xidVal.(type) { + switch xVal := xidVal.(type) { case json.Number: - val, err := xidVal.(json.Number).Int64() + val, err := xVal.Int64() if err != nil { return "", err } return strconv.FormatInt(val, 10), nil + case int64: + return strconv.FormatInt(xVal, 10), nil default: - val, ok := xidVal.(int64) - if !ok { - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int but data type in schema is Int", xidName, typeName)) - } - return strconv.FormatInt(val, 10), nil + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int but data type in schema is Int", xidName, typeName)) } case "Int64": - switch xidVal.(type) { + switch xVal := xidVal.(type) { case json.Number: - val, err := xidVal.(json.Number).Int64() + val, err := xVal.Int64() if err != nil { return "", err } return strconv.FormatInt(val, 10), nil case int64: - val, ok := xidVal.(int64) - if !ok { - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int64 but data type in schema is Int64", xidName, typeName)) - } - return strconv.FormatInt(val, 10), nil - // If the xid field is of type Int64, both String and Int forms are allowed as per spec. - // The default case handles id xid is passed as string. + return strconv.FormatInt(xVal, 10), nil + // If the xid field is of type Int64, both String and Int forms are allowed. + case string: + return xVal, nil default: - xidString, ok := xidVal.(string) - if !ok { - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int64", xidName, typeName)) - } - return xidString, nil + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Int64 but data type in schema is Int64", xidName, typeName)) } case "Float": - switch xidVal.(type) { + switch xVal := xidVal.(type) { case json.Number: - val, err := xidVal.(json.Number).Float64() + val, err := xVal.Float64() if err != nil { return "", err } return strconv.FormatFloat(val, 'f', -1, 64), nil + case float64: + return strconv.FormatFloat(xVal, 'f', -1, 64), nil default: - val, ok := xidVal.(float64) - if !ok { - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Float but data type in schema is Float", xidName, typeName)) - } - return strconv.FormatFloat(val, 'f', -1, 64), nil + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ + "a Float but data type in schema is Float", xidName, typeName)) } - default: + case "String": xidString, ok := xidVal.(string) if !ok { return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ "a String", xidName, typeName)) } return xidString, nil + default: + return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't"+ + "allowed as Xid", xidName, typeName)) } } From a8e475f95df3555ac545a1d2c72ee1ae0b712243 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Tue, 16 Mar 2021 16:42:52 +0530 Subject: [PATCH 13/14] handled ID also --- graphql/resolve/mutation_rewriter.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index b4c83f77468..f76808fb2bb 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -2259,6 +2259,7 @@ func copyTypeMap(from, to map[string]schema.Type) { } func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { + fmt.Println(typeName) switch typeName { case "Int": switch xVal := xidVal.(type) { @@ -2305,7 +2306,8 @@ func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ "a Float but data type in schema is Float", xidName, typeName)) } - case "String": + // "ID" is given as input for the @extended type mutation. + case "String", "ID": xidString, ok := xidVal.(string) if !ok { return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ From a4e561acef977a450ca09414635602b0a3eb703e Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Tue, 16 Mar 2021 17:07:30 +0530 Subject: [PATCH 14/14] address more comments --- graphql/resolve/mutation_rewriter.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index f76808fb2bb..932832af53c 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -2272,8 +2272,8 @@ func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { case int64: return strconv.FormatInt(xVal, 10), nil default: - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int but data type in schema is Int", xidName, typeName)) + return "", fmt.Errorf("encountered an XID %s with %s that isn't "+ + "a Int but data type in schema is Int", xidName, typeName) } case "Int64": switch xVal := xidVal.(type) { @@ -2289,8 +2289,8 @@ func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { case string: return xVal, nil default: - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Int64 but data type in schema is Int64", xidName, typeName)) + return "", fmt.Errorf("encountered an XID %s with %s that isn't "+ + "a Int64 but data type in schema is Int64", xidName, typeName) } case "Float": switch xVal := xidVal.(type) { @@ -2303,19 +2303,19 @@ func extractVal(xidVal interface{}, xidName, typeName string) (string, error) { case float64: return strconv.FormatFloat(xVal, 'f', -1, 64), nil default: - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a Float but data type in schema is Float", xidName, typeName)) + return "", fmt.Errorf("encountered an XID %s with %s that isn't "+ + "a Float but data type in schema is Float", xidName, typeName) } // "ID" is given as input for the @extended type mutation. case "String", "ID": xidString, ok := xidVal.(string) if !ok { - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't "+ - "a String", xidName, typeName)) + return "", fmt.Errorf("encountered an XID %s with %s that isn't "+ + "a String", xidName, typeName) } return xidString, nil default: - return "", errors.New(fmt.Sprintf("encountered an XID %s with %s that isn't"+ - "allowed as Xid", xidName, typeName)) + return "", fmt.Errorf("encountered an XID %s with %s that isn't"+ + "allowed as Xid", xidName, typeName) } }