Skip to content

Commit

Permalink
Fix(GraphQL): This PR fixes the bug which return error when we input …
Browse files Browse the repository at this point in the history
…list of integers. (#6823)

Fixes GRAPHQL-690
  • Loading branch information
JatinDev543 authored Nov 3, 2020
1 parent be919e5 commit 6e21c31
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 2 deletions.
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func RunAll(t *testing.T) {
t.Run("three level deep", threeLevelDeepMutation)
t.Run("update mutation without set & remove", updateMutationWithoutSetRemove)
t.Run("Input coercing for int64 type", int64BoundaryTesting)
t.Run("List of integers", intWithList)
t.Run("Check cascade with mutation without ID field", checkCascadeWithMutationWithoutIDField)
t.Run("Geo - Point type", mutationPointType)
t.Run("Geo - Polygon type", mutationPolygonType)
Expand Down
76 changes: 75 additions & 1 deletion graphql/e2e/common/error_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,78 @@
}
errors:
[ { "message": "Type mismatched for Value `180143985094.0`, expected:`Int64`",
"path": [ "variable", "Post",0.0,"numViews" ] } ]
"path": [ "variable", "Post",0.0,"numViews" ] } ]

-
name: "Error for int64 value given in list as variable"
gqlrequest: |
mutation addpost1($Post: [Addpost1Input!]!){
addpost1(input:$Post){
post1{
title
likesByMonth
}
}
}
gqlvariables: |
{ "Post": [
{ "title": "Dgraph",
"likesByMonth": [180143985094.0,33,1,66]
} ]
}
errors:
[ { "message": "Type mismatched for Value `180143985094.0`, expected:`Int64`",
"path": [ "variable", "Post",0.0,"likesByMonth",0.0 ] } ]

- name: "Error for int64 value given in list"
gqlrequest: |
mutation {
addpost1(input:[{title:"Dgraph",likesByMonth: [180143985094.0,33,1,66]}]){
post1{
title
likesByMonth
}
}
}
gqlvariables: |
{ }
errors:
[ { "message": "Type mismatched for Value `180143985094.0`, expected: Int64, got: 'Float'",
"locations": [ { "line": 2, "column": 50 } ] } ]

-
name: "Error for int value given in list as variable"
gqlrequest: |
mutation addpost1($Post: [Addpost1Input!]!){
addpost1(input:$Post){
post1{
title
commentsByMonth
}
}
}
gqlvariables: |
{ "Post": [
{ "title": "Dgraph",
"commentsByMonth": [2147483648,33,1,66]
} ]
}
errors:
[ { "message": "Out of range value '2147483648', for type `Int`",
"path": [ "variable", "Post",0.0,"commentsByMonth",0.0 ] } ]

- name: "Error for int value given in list"
gqlrequest: |
mutation {
addpost1(input:[{title:"Dgraph",commentsByMonth: [2147483648,33,1,66]}]){
post1{
title
commentsByMonth
}
}
}
gqlvariables: |
{ }
errors:
[ { "message": "Out of range value '2147483648', for type `Int`",
"locations": [ { "line": 2, "column": 53 } ] } ]
65 changes: 65 additions & 0 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,71 @@ func int64BoundaryTesting(t *testing.T) {
deleteGqlType(t, "post1", filter, 2, nil)
}

func intWithList(t *testing.T) {
tcases := []struct {
name string
query string
variables map[string]interface{}
expected string
}{{
name: "list of integers in mutation",
query: `mutation {
addpost1(input: [{title: "Dgraph",commentsByMonth:[2,33,11,6],likesByMonth:[4,33,1,66] }]) {
post1 {
title
commentsByMonth
likesByMonth
}
}
}`,
expected: `{
"addpost1": {
"post1": [{
"title": "Dgraph",
"commentsByMonth": [2,33,11,6],
"likesByMonth": [4,33,1,66]
}]
}
}`,
}, {
name: "list of integers in variable",
query: `mutation($post1:[Addpost1Input!]!) {
addpost1(input:$post1 ) {
post1 {
title
commentsByMonth
likesByMonth
}
}
}`,
variables: map[string]interface{}{"post1": []interface{}{map[string]interface{}{"title": "Dgraph", "commentsByMonth": []int{2, 33, 11, 6}, "likesByMonth": []int64{4, 33, 1, 66}}}},

expected: `{
"addpost1": {
"post1": [{
"title": "Dgraph",
"commentsByMonth": [2,33,11,6],
"likesByMonth": [4,33,1,66]
}]
}
}`,
}}
for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
params := &GraphQLParams{
Query: tcase.query,
Variables: tcase.variables,
}
resp := params.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, resp)
testutil.CompareJSON(t, tcase.expected, string(resp.Data))
filter := map[string]interface{}{"title": map[string]interface{}{"regexp": "/Dgraph.*/"}}
deleteGqlType(t, "post1", filter, 1, nil)
})
}

}

func nestedAddMutationWithHasInverse(t *testing.T) {
params := &GraphQLParams{
Query: `mutation addPerson1($input: [AddPerson1Input!]!) {
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ type post1{
id: ID
title: String! @id @search(by: [regexp])
numLikes: Int64
commentsByMonth: [Int]
likesByMonth: [Int64]
}

type Person1 {
Expand Down
17 changes: 17 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@
],
"upsert": true
},
{
"predicate": "post1.commentsByMonth",
"list": true,
"type": "int"
},
{
"predicate": "post1.likesByMonth",
"list": true,
"type": "int"
},
{
"predicate": "pwd",
"type": "password"
Expand Down Expand Up @@ -875,7 +885,14 @@
},
{
"name": "post1.numLikes"
},
{
"name": "post1.commentsByMonth"
},
{
"name": "post1.likesByMonth"
}

],
"name": "post1"
},
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ type post1{
id: ID
title: String! @id @search(by: [regexp])
numLikes: Int64
commentsByMonth: [Int]
likesByMonth: [Int64]
}

type Person1 {
Expand Down
16 changes: 16 additions & 0 deletions graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@
"trigram"
],
"upsert": true
},
{
"predicate": "post1.commentsByMonth",
"list": true,
"type": "int"
},
{
"predicate": "post1.likesByMonth",
"list": true,
"type": "int"
}
],
"types": [
Expand Down Expand Up @@ -928,6 +938,12 @@
},
{
"name": "post1.numLikes"
},
{
"name": "post1.commentsByMonth"
},
{
"name": "post1.likesByMonth"
}
],
"name": "post1"
Expand Down
2 changes: 1 addition & 1 deletion graphql/schema/validation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func directiveArgumentsCheck(observers *validator.Events, addError validator.Add

func intRangeCheck(observers *validator.Events, addError validator.AddErrFunc) {
observers.OnValue(func(walker *validator.Walker, value *ast.Value) {
if value.Definition == nil || value.ExpectedType == nil || value.Kind == ast.Variable {
if value.Definition == nil || value.ExpectedType == nil || value.Kind == ast.Variable || value.Kind == ast.ListValue {
return
}

Expand Down

0 comments on commit 6e21c31

Please sign in to comment.