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

fix(GraphQL): Fix panic caused by incorrect input coercion of scalar to list #7405

Merged
merged 5 commits into from
Feb 12, 2021
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/dgraph-io/badger/v3 v3.0.0-20210209201111-6c35ad6c28e0
github.com/dgraph-io/dgo/v200 v200.0.0-20210125093441-2ab429259580
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.20210205182321-f8e4908e34d1
github.com/dgraph-io/simdjson-go v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ github.com/dgraph-io/dgo/v200 v200.0.0-20210125093441-2ab429259580/go.mod h1:1LU
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.20210205182321-f8e4908e34d1 h1:E+NH1+aTO3vgP/t01DvYbX8qnwBC0bPgPwK7y+y0vaE=
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 @@ -775,6 +775,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 @@ -5307,3 +5307,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 @@ -355,3 +356,8 @@ type Dataset {
project: Project!
name: String! @search(by: [hash])
}

type author1{
name:String! @id @search(by: [regexp])
posts:[post1] @hasInverse(field: author)
}
33 changes: 33 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
"hash"
]
},
{
"predicate": "post1.author",
"type": "uid"
},
{
"index": true,
"predicate": "author1.name",
"tokenizer": [
"hash",
"trigram"
],
"type": "string",
"upsert": true
},
{
"predicate": "Astronaut.id",
"type": "string",
Expand All @@ -17,6 +31,11 @@
],
"upsert": true
},
{
"list": true,
"predicate": "author1.posts",
"type": "uid"
},
{
"predicate": "Astronaut.missions",
"type": "uid",
Expand Down Expand Up @@ -1295,6 +1314,9 @@
},
{
"fields": [
{
"name": "post1.author"
},
{
"name": "post1.title"
},
Expand Down Expand Up @@ -1324,6 +1346,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
35 changes: 34 additions & 1 deletion graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
"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",
Expand Down Expand Up @@ -1351,6 +1370,9 @@
},
{
"fields": [
{
"name": "post1.author"
},
{
"name": "post1.title"
},
Expand All @@ -1365,6 +1387,17 @@
}
],
"name": "post1"
},
{
"fields": [
{
"name": "author1.name"
},
{
"name": "author1.posts"
}
],
"name": "author1"
}
]
}
}
Loading