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

Add nullvalue #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
formatting and typing fixes
jaemk committed Sep 18, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d60ff58932c093fa27a3e169690a7d66d6f7b74a
2 changes: 1 addition & 1 deletion graphql/execution/values.py
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ def get_variable_values(
if inputs is None:
inputs = {}

values = {}
values = {} # type: Dict[str, Any]
for def_ast in definition_asts:
var_name = def_ast.variable.name.value
var_type = type_from_ast(schema, def_ast.type)
4 changes: 1 addition & 3 deletions graphql/language/parser.py
Original file line number Diff line number Diff line change
@@ -509,9 +509,7 @@ def parse_value_literal(parser, is_const):
)

if token.value == "null":
return ast.NullValue( # type: ignore
loc=loc(parser, token.start)
)
return ast.NullValue(loc=loc(parser, token.start)) # type: ignore

return ast.EnumValue( # type: ignore
value=token.value, loc=loc(parser, token.start)
7 changes: 6 additions & 1 deletion graphql/language/printer.py
Original file line number Diff line number Diff line change
@@ -83,7 +83,12 @@ def leave_OperationDefinition(self, node, *args):

def leave_VariableDefinition(self, node, *args):
# type: (Any, *Any) -> str
return node.variable + ": " + node.type + wrap(" = ", node.default_value, is_default_value=True)
return (
node.variable
+ ": "
+ node.type
+ wrap(" = ", node.default_value, is_default_value=True)
)

def leave_SelectionSet(self, node, *args):
# type: (Any, *Any) -> str
26 changes: 17 additions & 9 deletions graphql/language/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -107,7 +107,9 @@ def test_allows_null_value():


def test_parses_null_value_to_null():
result = parse('{ fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null}) }')
result = parse(
'{ fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null}) }'
)
values = result.definitions[0].selection_set.selections[0].arguments[0].value.fields
expected = (
(u"a", ast.NullValue()),
@@ -124,7 +126,10 @@ def test_parses_null_value_in_list():
assert result == ast.Document(
definitions=[
ast.OperationDefinition(
operation="query", name=None, variable_definitions=None, directives=[],
operation="query",
name=None,
variable_definitions=None,
directives=[],
selection_set=ast.SelectionSet(
selections=[
ast.Field(
@@ -144,7 +149,7 @@ def test_parses_null_value_in_list():
ast.StringValue(value=u"A"),
ast.NullValue(),
ast.StringValue(value=u"C"),
],
]
),
),
ast.ObjectField(
@@ -153,20 +158,23 @@ def test_parses_null_value_in_list():
),
]
),
),
)
],
),
],
)
]
),
),
],
)
]
)


def test_null_as_name():
result = parse('{ thingy(null: "stringcheese") }')
assert result.definitions[0].selection_set.selections[0].name.value == "thingy"
assert result.definitions[0].selection_set.selections[0].arguments[0].name.value == "null"
assert (
result.definitions[0].selection_set.selections[0].arguments[0].name.value
== "null"
)


def test_parses_multi_byte_characters():
5 changes: 4 additions & 1 deletion graphql/language/tests/test_printer.py
Original file line number Diff line number Diff line change
@@ -87,10 +87,13 @@ def test_correctly_prints_mutation_with_artifacts():

def test_correctly_prints_null():
query_ast_shorthanded = parse('{ thingy(null: "wow", name: null) }')
assert print_ast(query_ast_shorthanded) == """{
assert (
print_ast(query_ast_shorthanded)
== """{
thingy(null: "wow", name: null)
}
"""
)


def test_prints_kitchen_sink():
5 changes: 1 addition & 4 deletions graphql/type/introspection.py
Original file line number Diff line number Diff line change
@@ -541,10 +541,7 @@ def _resolve_default_value(input_value, *_):
("type", GraphQLField(GraphQLNonNull(__Type))),
(
"defaultValue",
GraphQLField(
type=GraphQLString,
resolver=_resolve_default_value,
),
GraphQLField(type=GraphQLString, resolver=_resolve_default_value),
),
]
),
4 changes: 3 additions & 1 deletion graphql/utils/tests/test_ast_to_code.py
Original file line number Diff line number Diff line change
@@ -15,5 +15,7 @@ def test_ast_to_code_using_kitchen_sink():
def loc(start, end):
return Loc(start, end, source)

parsed_code_ast = eval(code_ast, {}, {"ast": ast, "loc": loc, "Undefined": Undefined})
parsed_code_ast = eval(
code_ast, {}, {"ast": ast, "loc": loc, "Undefined": Undefined}
)
assert doc == parsed_code_ast
10 changes: 7 additions & 3 deletions graphql/utils/tests/test_build_client_schema.py
Original file line number Diff line number Diff line change
@@ -75,8 +75,10 @@ def test_builds_a_simple_schema_with_both_operation_types():
"setStringDefault": GraphQLField(
GraphQLString,
description="Set the string field",
args={"default_value": GraphQLArgument(GraphQLString, default_value=None)},
)
args={
"default_value": GraphQLArgument(GraphQLString, default_value=None)
},
),
},
)
SubscriptionType = GraphQLObjectType(
@@ -467,7 +469,9 @@ def test_builds_a_schema_with_field_arguments_with_default_values():
GraphQLField(
GraphQLString,
args={
"intArg": GraphQLArgument(GraphQLInt, default_value=None)
"intArg": GraphQLArgument(
GraphQLInt, default_value=None
)
},
),
),
45 changes: 28 additions & 17 deletions graphql/utils/tests/test_schema_printer.py
Original file line number Diff line number Diff line change
@@ -185,11 +185,15 @@ def test_prints_string_field_with_int_arg_with_default():


def test_prints_string_field_with_int_arg_with_default_null():
output = print_single_field_schema(GraphQLField(
type=GraphQLString,
args={"argOne": GraphQLArgument(GraphQLInt, default_value=None)}
))
assert output == """
output = print_single_field_schema(
GraphQLField(
type=GraphQLString,
args={"argOne": GraphQLArgument(GraphQLInt, default_value=None)},
)
)
assert (
output
== """
schema {
query: Root
}
@@ -198,6 +202,7 @@ def test_prints_string_field_with_int_arg_with_default_null():
singleField(argOne: Int = null): String
}
"""
)


def test_prints_string_field_with_non_null_int_arg():
@@ -515,22 +520,24 @@ def test_prints_input_type():
def test_prints_input_type_with_default():
InputType = GraphQLInputObjectType(
name="InputType",
fields={
"int": GraphQLInputObjectField(GraphQLInt, default_value=2)
}
fields={"int": GraphQLInputObjectField(GraphQLInt, default_value=2)},
)

Root = GraphQLObjectType(
name="Root",
fields={
"str": GraphQLField(GraphQLString, args={"argOne": GraphQLArgument(InputType)})
}
"str": GraphQLField(
GraphQLString, args={"argOne": GraphQLArgument(InputType)}
)
},
)

Schema = GraphQLSchema(Root)
output = print_for_test(Schema)

assert output == """
assert (
output
== """
schema {
query: Root
}
@@ -543,27 +550,30 @@ def test_prints_input_type_with_default():
str(argOne: InputType): String
}
"""
)


def test_prints_input_type_with_default_null():
InputType = GraphQLInputObjectType(
name="InputType",
fields={
"int": GraphQLInputObjectField(GraphQLInt, default_value=None)
}
fields={"int": GraphQLInputObjectField(GraphQLInt, default_value=None)},
)

Root = GraphQLObjectType(
name="Root",
fields={
"str": GraphQLField(GraphQLString, args={"argOne": GraphQLArgument(InputType)})
}
"str": GraphQLField(
GraphQLString, args={"argOne": GraphQLArgument(InputType)}
)
},
)

Schema = GraphQLSchema(Root)
output = print_for_test(Schema)

assert output == """
assert (
output
== """
schema {
query: Root
}
@@ -576,6 +586,7 @@ def test_prints_input_type_with_default_null():
str(argOne: InputType): String
}
"""
)


def test_prints_custom_scalar():
6 changes: 3 additions & 3 deletions graphql/utils/value_from_ast.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..language import ast
from ..utils.undefined import Undefined
from ..utils.undefined import Undefined, _Undefined
from ..type import (
GraphQLEnumType,
GraphQLInputObjectType,
@@ -16,7 +16,7 @@


def value_from_ast(value_ast, type, variables=None):
# type: (Optional[Node], GraphQLType, Optional[Dict[str, Union[List, Dict, int, float, bool, str, None]]]) -> Union[List, Dict, int, float, bool, str, None]
# type: (Optional[Node], GraphQLType, Optional[Dict[str, Union[List, Dict, int, float, bool, str, None]]]) -> Union[List, Dict, int, float, bool, str, None, _Undefined]
"""Given a type and a value AST node known to match this type, build a
runtime value."""
if isinstance(type, GraphQLNonNull):
@@ -25,7 +25,7 @@ def value_from_ast(value_ast, type, variables=None):
return value_from_ast(value_ast, type.of_type, variables)

if value_ast is Undefined:
return value_ast
return Undefined

if isinstance(value_ast, ast.NullValue):
return None
17 changes: 11 additions & 6 deletions graphql/validation/tests/test_arguments_of_correct_type.py
Original file line number Diff line number Diff line change
@@ -115,13 +115,16 @@ def test_good_enum_value(self):
)

def test_null_nullable_int_value(self):
expect_passes_rule(ArgumentsOfCorrectType, """
expect_passes_rule(
ArgumentsOfCorrectType,
"""
{
complicatedArgs {
intArgField(intArg: null)
}
}
""")
""",
)


# noinspection PyMethodMayBeStatic
@@ -247,15 +250,17 @@ def test_float_into_int(self):
)

def test_null_into_non_null_int(self):
expect_fails_rule(ArgumentsOfCorrectType, """
expect_fails_rule(
ArgumentsOfCorrectType,
"""
{
complicatedArgs {
nonNullIntArgField(nonNullIntArg: null)
}
}
""", [
bad_value("nonNullIntArg", "Int!", "null", 4, 51)
])
""",
[bad_value("nonNullIntArg", "Int!", "null", 4, 51)],
)


# noinspection PyMethodMayBeStatic
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ def test_variables_with_invalid_default_values():
['Expected "ComplexInput", found not an object.'],
),
bad_value("d", "Int!", "null", 6, 20),
default_for_non_null_arg("d", "Int!", "Int", 6, 20)
default_for_non_null_arg("d", "Int!", "Int", 6, 20),
],
)

3 changes: 1 addition & 2 deletions graphql/validation/tests/utils.py
Original file line number Diff line number Diff line change
@@ -295,8 +295,7 @@ def expect_invalid(schema, rules, query, expected_errors, sort_list=True):
]

errors = list(map(format_error, errors))
msg = ("\nexpected errors: %s"
"\n got errors: %s" % (expected_errors, errors))
msg = "\nexpected errors: {}\n got errors: {}".format(expected_errors, errors)
if sort_list:
sorted_errors = sort_lists(list(map(format_error, errors)))
expected_errors = map(format_message, expected_errors)