fix validation of null values, closes #1981 (#2057)

This commit is contained in:
Vamshi Surabhi 2019-04-24 13:19:39 +05:30 committed by GitHub
parent ed3e9caf2f
commit 7151f1387f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 3 deletions

View File

@ -303,7 +303,11 @@ withParsed
withParsed expectedTy valParser val fn = do
parsedVal <- valParser val
case unP parsedVal of
Nothing -> AnnInpVal expectedTy Nothing <$> fn Nothing
Nothing ->
if G.isNullable expectedTy
then AnnInpVal expectedTy Nothing <$> fn Nothing
else throwVE $ "null value found for non-nullable type: "
<> G.showGT expectedTy
Just (Right v) -> AnnInpVal expectedTy Nothing <$> fn (Just v)
Just (Left (var, v)) -> do
let varTxt = G.unName $ G.unVariable var

View File

@ -0,0 +1,19 @@
description: Passing null value for non nullable type
url: /v1alpha1/graphql
status: 400
response:
errors:
- extensions:
path: "$.selectionSet.update_author.args.where"
code: "validation-failed"
message: "null value found for non-nullable type: author_bool_exp!"
query:
query: |
mutation update_author {
update_author(where: null _set: {name: ""}) {
returning {
id
name
}
}
}

View File

@ -0,0 +1,21 @@
description: Passing null value for non nullable type
url: /v1alpha1/graphql
status: 400
response:
errors:
- extensions:
path: "$.variableValues.author_id"
code: "validation-failed"
message: "null value found for non-nullable type: Int!"
query:
variables:
author_id: null
query: |
mutation update_author($author_id: Int!) {
update_author(where: {id: {_eq: $author_id}}, _set: {name: ""}) {
returning {
id
name
}
}
}

View File

@ -0,0 +1,15 @@
type: bulk
args:
#Author table
- type: run_sql
args:
sql: |
create table author(
id serial primary key,
name text unique
);
- type: track_table
args:
schema: public
name: author

View File

@ -0,0 +1,6 @@
type: bulk
args:
- type: run_sql
args:
sql: |
drop table author

View File

@ -51,8 +51,9 @@ class DefaultTestMutations(ABC):
pass
class DefaultTestSelectQueries(ABC):
# Any test which has a setup and a teardown
# Ideally, DefaultTestSelectQueries should just be this
class GraphQLEngineTest(ABC):
@pytest.fixture(scope='class')
def transact(self, request, hge_ctx):
@ -69,3 +70,6 @@ class DefaultTestSelectQueries(ABC):
@abstractmethod
def dir(self):
pass
class DefaultTestSelectQueries(GraphQLEngineTest):
pass

View File

@ -0,0 +1,20 @@
import pytest
import yaml
from validate import check_query_f
from super_classes import GraphQLEngineTest
# @pytest.mark.parametrize("transport", ['http','websocket'])
# graphql parser can't seem to parse {where: null}, disabling
# websocket till then
@pytest.mark.parametrize("transport", ['http'])
class TestGraphQLValidation(GraphQLEngineTest):
def test_null_value(self, hge_ctx, transport):
check_query_f(hge_ctx, self.dir() + "/null_value_err.yaml", transport)
def test_null_variable_value(self, hge_ctx, transport):
check_query_f(hge_ctx, self.dir() + "/null_variable_value_err.yaml", transport)
@classmethod
def dir(cls):
return "queries/graphql_validation"