Skip to content

Commit

Permalink
fix(GraphQL): Fix panic caused by incorrect input coercion of scalar …
Browse files Browse the repository at this point in the history
…to list (#7405)

Fixes-GRAPHQl-1006
We have moved some of the input coercion code to the library as part of the commit. I have also added tests to verify the fix.

(cherry picked from commit a26993a)
  • Loading branch information
JatinDev543 committed Feb 12, 2021
1 parent 474aa62 commit 66ee72f
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 119 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/dgraph-io/badger/v3 v3.2011.1
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6
github.com/dgraph-io/gqlgen v0.13.2
github.com/dgraph-io/gqlparser/v2 v2.1.4
github.com/dgraph-io/gqlparser/v2 v2.1.5
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b
github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d
github.com/dgrijalva/jwt-go v3.2.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6/go.mod h1:rHa
github.com/dgraph-io/gqlgen v0.13.2 h1:TNhndk+eHKj5qE7BenKKSYdSIdOGhLqxR1rCiMso9KM=
github.com/dgraph-io/gqlgen v0.13.2/go.mod h1:iCOrOv9lngN7KAo+jMgvUPVDlYHdf7qDwsTkQby2Sis=
github.com/dgraph-io/gqlparser/v2 v2.1.1/go.mod h1:MYS4jppjyx8b9tuUtjV7jU1UFZK6P9fvO8TsIsQtRKU=
github.com/dgraph-io/gqlparser/v2 v2.1.4 h1:yMj5848fLCiJQX6/qEHtMl46i2mr7HzrD8fwHI86Svg=
github.com/dgraph-io/gqlparser/v2 v2.1.4/go.mod h1:MYS4jppjyx8b9tuUtjV7jU1UFZK6P9fvO8TsIsQtRKU=
github.com/dgraph-io/gqlparser/v2 v2.1.5 h1:FNFSzyOEZ8gs97SLBtn2Pez9f12emvsQ6Vv6oNtg5rc=
github.com/dgraph-io/gqlparser/v2 v2.1.5/go.mod h1:MYS4jppjyx8b9tuUtjV7jU1UFZK6P9fvO8TsIsQtRKU=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b h1:PDEhlwHpkEQ5WBfOOKZCNZTXFDGyCEWTYDhxGQbyIpk=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ=
github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d h1:eQYOG6A4td1tht0NdJB9Ls6DsXRGb2Ft6X9REU/MbbE=
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 @@ -718,6 +718,7 @@ func RunAll(t *testing.T) {
t.Run("two levels linked to one XID", twoLevelsLinkedToXID)
t.Run("cyclically linked mutation", cyclicMutation)
t.Run("parallel mutations", parallelMutations)
t.Run("input coercion to list", inputCoerciontoList)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/common/error_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{ }
errors:
[ { "message": "Cannot query field \"getAuthorszzz\" on type \"Query\". Did you mean
\"getAuthor\"?",
\"getAuthor\" or \"getauthor1\"?",
"locations": [ { "line": 2, "column": 3 } ] } ]

-
Expand Down
142 changes: 142 additions & 0 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5068,3 +5068,145 @@ func twoLevelsLinkedToXID(t *testing.T) {
DeleteGqlType(t, "Owner", map[string]interface{}{}, 1, nil)
DeleteGqlType(t, "Dataset", map[string]interface{}{}, 1, nil)
}

func inputCoerciontoList(t *testing.T) {

tcases := []struct {
name string
query string
variables string
expected string
}{
{name: "Coercion of Scalar value at root to list ",
query: ` mutation {
addpost1(input: { title: "GraphQL", commentsByMonth: 1 }) {
post1 {
title
commentsByMonth
}
}
}`,
expected: `{
"addpost1": {
"post1": [
{
"title": "GraphQL",
"commentsByMonth": [
1
]
}
]
}
}`,
},
{name: "Coercion of Scalar value at root to list using variables",
query: ` mutation($post1: [Addpost1Input!]!) {
addpost1(input: $post1) {
post1 {
title
commentsByMonth
}
}
}`,
expected: `{
"addpost1": {
"post1": [
{
"title": "Dgraph",
"commentsByMonth": [
1
]
}
]
}
}`,
variables: `{"post1": {"title":"Dgraph","commentsByMonth":1}}`,
},
{name: "Coercing nested scalar value to list ",
query: ` mutation {
addauthor1(
input: { name: "Jack", posts: { title: "RDBMS", commentsByMonth: 1 } }
) {
author1 {
name
posts {
title
commentsByMonth
}
}
}
}`,
expected: `{
"addauthor1": {
"author1": [
{
"name": "Jack",
"posts": [
{
"title": "RDBMS",
"commentsByMonth": [
1
]
}
]
}
]
}
}`,
},
{name: "Coercing nested scalar value to list using variables",
query: `mutation($author: [Addauthor1Input!]!) {
addauthor1(input: $author) {
author1 {
name
posts {
title
commentsByMonth
}
}
}
}`,
expected: `{
"addauthor1": {
"author1": [
{
"name": "Jackob",
"posts": [
{
"title": "DB",
"commentsByMonth": [
1
]
}
]
}
]
}
}`,
variables: `{"author": {"name": "Jackob","posts":{"title":"DB","commentsByMonth":1}}}`,
},
}

for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
var vars map[string]interface{}
if tcase.variables != "" {
err := json.Unmarshal([]byte(tcase.variables), &vars)
require.NoError(t, err)
}
params := &GraphQLParams{
Query: tcase.query,
Variables: vars,
}
resp := params.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, resp)
testutil.CompareJSON(t, tcase.expected, string(resp.Data))
})
}

author1DeleteFilter := map[string]interface{}{"name": map[string]interface{}{"in": []string{"Jack", "Jackob"}}}
DeleteGqlType(t, "author1", author1DeleteFilter, 2, nil)
posts1DeleteFilter := map[string]interface{}{"title": map[string]interface{}{"in": []string{"Dgraph", "GraphQL", "RDBMS", "DB"}}}
DeleteGqlType(t, "post1", posts1DeleteFilter, 4, nil)

}
6 changes: 6 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ type post1{
numLikes: Int64 @search
commentsByMonth: [Int]
likesByMonth: [Int64]
author: author1 @hasInverse(field: posts)
}

type Person1 {
Expand Down Expand Up @@ -315,3 +316,8 @@ type Dataset {
project: Project!
name: String! @search(by: [hash])
}

type author1{
name:String! @id @search(by: [regexp])
posts:[post1] @hasInverse(field: author)
}
69 changes: 69 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,61 @@
"hash"
]
},
{
"predicate": "post1.author",
"type": "uid"
},
{
"index": true,
"predicate": "author1.name",
"tokenizer": [
"hash",
"trigram"
],
"type": "string",
"upsert": true
},
{
"predicate": "Astronaut.id",
"type": "string",
"index": true,
"tokenizer": [
"hash"
],
"upsert": true
},
{
"list": true,
"predicate": "author1.posts",
"type": "uid"
},
{
"predicate": "Astronaut.missions",
"type": "uid",
"list": true
},
{
"predicate": "Book.bookId",
"type": "int",
"index": true,
"tokenizer": [
"int"
],
"upsert": true
},
{
"predicate": "Book.chapters",
"type": "uid",
"list": true
},
{
"predicate": "Book.desc",
"type": "string"
},
{
"predicate": "Book.name",
"type": "string"
},
{
"predicate": "Category.name",
"type": "string"
Expand Down Expand Up @@ -1086,6 +1141,9 @@
},
{
"fields": [
{
"name": "post1.author"
},
{
"name": "post1.title"
},
Expand Down Expand Up @@ -1115,6 +1173,17 @@
],
"name": "roboDroid"
},
{
"fields": [
{
"name": "author1.name"
},
{
"name": "author1.posts"
}
],
"name": "author1"
},
{
"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 @@ -196,12 +196,18 @@ type Comment1 {
replies: [Comment1]
}

type author1{
name:String! @id @search(by: [regexp])
posts:[post1] @hasInverse(field: author)
}

type post1{
id: ID
title: String! @id @search(by: [regexp])
numLikes: Int64 @search
commentsByMonth: [Int]
likesByMonth: [Int64]
author:author1 @hasInverse(field: posts)
}

type Person1 {
Expand Down
49 changes: 48 additions & 1 deletion graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@
"hash"
]
},
{
"predicate": "post1.author",
"type": "uid"
},
{
"index": true,
"predicate": "author1.name",
"tokenizer": [
"hash",
"trigram"
],
"type": "string",
"upsert": true
},
{
"list": true,
"predicate": "author1.posts",
"type": "uid"
},
{
"predicate": "Astronaut.id",
"type": "string",
"index": true,
"tokenizer": [
"hash"
],
"upsert": true
},
{
"predicate": "Astronaut.missions",
"type": "uid",
"list": true
},
{
"predicate": "Author.country",
"type": "uid"
Expand Down Expand Up @@ -1142,6 +1175,9 @@
},
{
"fields": [
{
"name": "post1.author"
},
{
"name": "post1.title"
},
Expand All @@ -1156,6 +1192,17 @@
}
],
"name": "post1"
},
{
"fields": [
{
"name": "author1.name"
},
{
"name": "author1.posts"
}
],
"name": "author1"
}
]
}
}
Loading

0 comments on commit 66ee72f

Please sign in to comment.