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

Implement nullvalue #119

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,32 @@ def __hash__(self):
return id(self)


class NullValue(Value):
__slots__ = ('loc', 'value')
_fields = ('value',)

def __init__(self, value=None, loc=None):
self.loc = loc
self.value = value

def __eq__(self, other):
return isinstance(other, NullValue)

def __repr__(self):
return ('NullValue('
'value={self.value!r}'
')').format(self=self)

def __copy__(self):
return type(self)(
self.value,
self.loc
)

def __hash__(self):
return id(self)


class EnumValue(Value):
__slots__ = ('loc', 'value',)
_fields = ('value',)
Expand Down
6 changes: 5 additions & 1 deletion graphql/language/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,11 @@ def parse_value_literal(parser, is_const):
advance(parser)
return ast.BooleanValue(value=token.value == 'true', loc=loc(parser, token.start))

if token.value != 'null':
elif token.value in ('null', ):
advance(parser)
return ast.NullValue(loc=loc(parser, token.start))

else:
advance(parser)
return ast.EnumValue(value=token.value, loc=loc(parser, token.start))

Expand Down
3 changes: 3 additions & 0 deletions graphql/language/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def leave_StringValue(self, node, *args):
def leave_BooleanValue(self, node, *args):
return json.dumps(node.value)

def leave_NullValue(self, node, *args):
return json.dumps(node.value)

def leave_EnumValue(self, node, *args):
return node.value

Expand Down
2 changes: 1 addition & 1 deletion graphql/language/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
{
unnamed(truthy: true, falsey: false),
unnamed(truthy: true, falsey: false, nullish: null),
query
}
"""
Expand Down
7 changes: 0 additions & 7 deletions graphql/language/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ def test_does_not_accept_fragments_spread_of_on():
assert 'Syntax Error GraphQL (1:9) Expected Name, found }' in excinfo.value.message


def test_does_not_allow_null_value():
with raises(GraphQLSyntaxError) as excinfo:
parse('{ fieldWithNullableStringInput(input: null) }')

assert 'Syntax Error GraphQL (1:39) Unexpected Name "null"' in excinfo.value.message


def test_parses_multi_byte_characters():
result = parse(u'''
# This comment has a \u0A0A multi-byte character.
Expand Down
2 changes: 1 addition & 1 deletion graphql/language/tests/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_prints_kitchen_sink():
}

{
unnamed(truthy: true, falsey: false)
unnamed(truthy: true, falsey: false, nullish: null)
query
}
'''
6 changes: 6 additions & 0 deletions graphql/language/tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,12 @@ def leave(self, node, key, parent, *args):
['enter', 'BooleanValue', 'value', 'Argument'],
['leave', 'BooleanValue', 'value', 'Argument'],
['leave', 'Argument', 1, None],
['enter', 'Argument', 2, None],
['enter', 'Name', 'name', 'Argument'],
['leave', 'Name', 'name', 'Argument'],
['enter', 'NullValue', 'value', 'Argument'],
['leave', 'NullValue', 'value', 'Argument'],
['leave', 'Argument', 2, None],
['leave', 'Field', 0, None],
['enter', 'Field', 1, None],
['enter', 'Name', 'name', 'Field'],
Expand Down
1 change: 1 addition & 0 deletions graphql/language/visitor_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ast.FloatValue: (),
ast.StringValue: (),
ast.BooleanValue: (),
ast.NullValue: (),
ast.EnumValue: (),
ast.ListValue: ('values',),
ast.ObjectValue: ('fields',),
Expand Down