Skip to content

Commit

Permalink
fix(GraphQL): This PR extend int64 range to 64-bit numeric values and…
Browse files Browse the repository at this point in the history
… adds input coercing and validation for integers. (#6275)

Fixes GRAPHQL-630
This PR extends the int64 range to 64-bit numeric values and adds input coercing and validation for integers.
  • Loading branch information
JatinDev543 authored Sep 3, 2020
1 parent 518a2d0 commit 82d92a3
Show file tree
Hide file tree
Showing 49 changed files with 328 additions and 125 deletions.
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func RunAll(t *testing.T) {
t.Run("alias works for mutations", mutationsWithAlias)
t.Run("three level deep", threeLevelDeepMutation)
t.Run("update mutation without set & remove", updateMutationWithoutSetRemove)
t.Run("Input coercing for int64 type", int64BoundaryTesting)
t.Run("Check cascade with mutation without ID field", checkCascadeWithMutationWithoutIDField)

// error tests
Expand Down
82 changes: 81 additions & 1 deletion graphql/e2e/common/error_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,46 @@
[ { "message": "Field `title` is not present in type `Author`. You can only use fields which are in type `Author`",
} ]

-
name: "Out of range error for int32 type"
gqlrequest: |
mutation {
addPost(input:[{title:"Dgraph",author:{name:"Bob"},numLikes:2147483648}]){
post{
title
numLikes
author{
name
}
}
}
}
gqlvariables:
{ }
errors:
[ { "message": "Out of range value '2147483648', for type `Int`",
"locations": [ { "line": 2, "column": 63 } ] } ]

-
name: "Out of range error for int64 type"
gqlrequest: |
mutation {
addPost(input:[{title:"Dgraph",author:{name:"Bob"},numViews:9223372036854775808}]){
post{
title
numViews
author{
name
}
}
}
}
gqlvariables:
{ }
errors:
[ { "message": "Out of range value '9223372036854775808', for type `Int64`",
"locations": [ { "line": 2, "column": 63 } ] } ]

-
name: "@cascade only accepts numUids or given type name as arguments for add or update payload "
gqlrequest: |
Expand All @@ -129,4 +169,44 @@
{ }
errors:
[ { "message": "Field `name` is not present in type `AddAuthorPayload`. You can only use fields which are in type `AddAuthorPayload`",
} ]
} ]

-
name: "String value is Incompatible with Int64 type"
gqlrequest: |
mutation {
addPost(input:[{title:"Dgraph",author:{name:"Bob"},numViews:"180143985094"}]){
post{
title
numLikes
author{
name
}
}
}
}
gqlvariables:
{ }
errors:
[ { "message": "Type mismatched for Value `180143985094`, expected: Int64, got: 'String'",
"locations": [ { "line": 2, "column": 64 } ] } ]

-
name: "Float value is Incompatible with Int64 type"
gqlrequest: |
mutation {
addPost(input:[{title:"Dgraph",author:{name:"Bob"},numViews:180143985094.0}]){
post{
title
numLikes
author{
name
}
}
}
}
gqlvariables:
{ }
errors:
[ { "message": "Type mismatched for Value `180143985094.0`, expected: Int64, got: 'Float'",
"locations": [ { "line": 2, "column": 63 } ] } ]
40 changes: 36 additions & 4 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2784,10 +2784,8 @@ func deleteGqlType(
require.Equal(t, "Deleted", deleteType["msg"], "while deleting %s (filter: %v)",
typeName, filter)
}
} else {
if diff := cmp.Diff(expectedErrors, gqlResponse.Errors); diff != "" {
t.Errorf("errors mismatch (-want +got):\n%s", diff)
}
} else if diff := cmp.Diff(expectedErrors, gqlResponse.Errors); diff != "" {
t.Errorf("errors mismatch (-want +got):\n%s", diff)
}
}

Expand Down Expand Up @@ -3649,3 +3647,37 @@ func checkCascadeWithMutationWithoutIDField(t *testing.T) {
filter := map[string]interface{}{"xcode": map[string]interface{}{"eq": "S2"}}
deleteState(t, filter, 1, nil)
}

func int64BoundaryTesting(t *testing.T) {
//This test checks the range of Int64
//(2^63)=9223372036854775808
addPost1Params := &GraphQLParams{
Query: `mutation {
addpost1(input: [{title: "Dgraph", numLikes: 9223372036854775807 },{title: "Dgraph1", numLikes: -9223372036854775808 }]) {
post1 {
title
numLikes
}
}
}`,
}

gqlResponse := addPost1Params.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

addPost1Expected := `{
"addpost1": {
"post1": [{
"title": "Dgraph",
"numLikes": 9223372036854775807
},{
"title": "Dgraph1",
"numLikes": -9223372036854775808
}]
}
}`
testutil.CompareJSON(t, addPost1Expected, string(gqlResponse.Data))
filter := map[string]interface{}{"title": map[string]interface{}{"regexp": "/Dgraph.*/"}}
deleteGqlType(t, "post1", filter, 2, nil)
}
5 changes: 5 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,9 @@ type Post1 {
type Comment1 {
id: String! @id
replies: [Comment1]
}
type post1{
id: ID
title: String! @id @search(by: [regexp])
numLikes: Int64
}
22 changes: 22 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@
],
"upsert": true
},
{
"predicate": "post1.title",
"type": "string",
"index": true,
"tokenizer": ["trigram", "hash"],
"upsert": true
},
{
"predicate": "post1.numLikes",
"type": "int"
},
{
"predicate": "State.capital",
"type": "string"
Expand Down Expand Up @@ -449,6 +460,17 @@
],
"name": "Post1"
},
{
"fields": [
{
"name": "post1.title"
},
{
"name": "post1.numLikes"
}
],
"name": "post1"
},
{
"fields": [
{
Expand Down
6 changes: 6 additions & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,10 @@ type Post1 {
type Comment1 {
id: String! @id
replies: [Comment1]
}

type post1{
id: ID
title: String! @id @search(by: [regexp])
numLikes: Int64
}
22 changes: 22 additions & 0 deletions graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@
],
"upsert": true
},
{
"predicate": "post1.title",
"type": "string",
"index": true,
"tokenizer": ["trigram", "hash"],
"upsert": true
},
{
"predicate": "post1.numLikes",
"type": "int"
},
{
"predicate": "Starship.length",
"type": "float"
Expand Down Expand Up @@ -537,6 +548,17 @@
],
"name": "Post1"
},
{
"fields": [
{
"name": "post1.title"
},
{
"name": "post1.numLikes"
}
],
"name": "post1"
},
{
"fields": [
{
Expand Down
3 changes: 1 addition & 2 deletions graphql/e2e/schema/generatedSchema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type Author {

"""
The Int64 scalar type represents a signed 64‐bit numeric non‐fractional value.
Int64 can currently represent values in range [-(2^53)+1, (2^53)-1] without any error.
Values out of this range but representable by a signed 64-bit integer, may get coercion error.
Int64 can represent values in range [-(2^63),(2^63 - 1)].
"""
scalar Int64

Expand Down
Loading

0 comments on commit 82d92a3

Please sign in to comment.