Skip to content

Commit

Permalink
add NullValue ast node
Browse files Browse the repository at this point in the history
- addresses graphql-python#118
- initial implementation by @yen223 in PR graphql-python#119
  • Loading branch information
jaemk committed May 4, 2018
1 parent b3a6bd0 commit b099fab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
21 changes: 21 additions & 0 deletions graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,27 @@ def __hash__(self):
return id(self)


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

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

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

def __repr__(self):
return 'NullValue'

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
9 changes: 5 additions & 4 deletions graphql/language/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,14 @@ def parse_value_literal(parser, is_const):
return ast.StringValue(value=token.value, loc=loc(parser, token.start))

elif token.kind == TokenKind.NAME:
advance(parser)
if token.value in ('true', 'false'):
advance(parser)
return ast.BooleanValue(value=token.value == 'true', loc=loc(parser, token.start))

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

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

elif token.kind == TokenKind.DOLLAR:
if not is_const:
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 'null'

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

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

0 comments on commit b099fab

Please sign in to comment.