Skip to content

Commit

Permalink
Default value for required argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Sep 15, 2023
1 parent 921aebc commit 338c17f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
31 changes: 23 additions & 8 deletions gqt/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
}


def is_optional_argument(field_type, default_value):
if field_type['kind'] != 'NON_NULL':
return True
elif default_value is not None:
return True
else:
return False


class QueryError(Exception):

def __init__(self, message, node):
Expand Down Expand Up @@ -510,14 +519,15 @@ def __init__(self,
name,
field_type,
description,
default_value,
state,
types):
super().__init__()
self.name = name
self._type = get_type(field_type)['name']
self.type = get_type_string(field_type)
self.description = description
self.is_optional = (field_type['kind'] != 'NON_NULL')
self.is_optional = is_optional_argument(field_type, default_value)
self.is_variable = False

if self.is_optional:
Expand Down Expand Up @@ -681,13 +691,14 @@ def __init__(self,
name,
field_type,
description,
default_value,
state,
types):
super().__init__()
self.name = name
self.type = get_type_string(field_type)
self.description = description
self.is_optional = (field_type['kind'] != 'NON_NULL')
self.is_optional = is_optional_argument(field_type, default_value)
self.is_variable = False

if not self.is_optional:
Expand Down Expand Up @@ -846,14 +857,15 @@ def __init__(self,
name,
field_type,
description,
default_value,
state,
types):
super().__init__()
self.name = name
self._type = get_type(field_type)['name']
self.type = get_type_string(field_type)
self.description = description
self.is_optional = (field_type['kind'] != 'NON_NULL')
self.is_optional = is_optional_argument(field_type, default_value)
self.is_variable = False
self.state = state
self.types = types
Expand Down Expand Up @@ -1161,13 +1173,14 @@ def __init__(self,
name,
field_type,
description,
default_value,
state,
types):
super().__init__()
self.name = name
self.type = get_type_string(field_type)
self.description = description
self.is_optional = (field_type['kind'] != 'NON_NULL')
self.is_optional = is_optional_argument(field_type, default_value)
self.is_variable = False
self.state = state
self.field_type = field_type
Expand Down Expand Up @@ -1487,18 +1500,19 @@ def build_argument(argument, types, state):
description = argument['description']
arg_type = argument['type']
kind = arg_type['kind']
default_value = argument.get('defaultValue')

if kind == 'NON_NULL':
kind = arg_type['ofType']['kind']

if kind == 'LIST':
return ListArgument(name, arg_type, description, state, types)
return ListArgument(name, arg_type, description, default_value, state, types)
elif kind == 'INPUT_OBJECT':
return InputArgument(name, arg_type, description, state, types)
return InputArgument(name, arg_type, description, default_value, state, types)
elif kind == 'ENUM':
return EnumArgument(name, arg_type, description, state, types)
return EnumArgument(name, arg_type, description, default_value, state, types)
else:
return ScalarArgument(name, arg_type, description, state, types)
return ScalarArgument(name, arg_type, description, default_value, state, types)


class ObjectFieldsIterator:
Expand Down Expand Up @@ -1822,6 +1836,7 @@ def _find_last(self, node):


def load_tree_from_schema(schema):
# print(schema)
types = schema['__schema']['types']
query_type = schema['__schema']['queryType']

Expand Down
2 changes: 1 addition & 1 deletion gqt/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.125.2'
__version__ = '0.125.3'
18 changes: 18 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ def test_enum_argument(self):
'■ a\n'
' X x: C')

def test_input_argument_default_value(self):
schema = ('type Query {'
' a(x: Foo): String'
'}'
'input Foo {'
' y: Int = 1'
'}')
tree = load_tree(schema)
tree.select()
tree.key_down()
self.assertEqual(tree.query(), 'query Query {a}')
tree.select()
self.assertEqual(tree.query(), 'query Query {a(x:{})}')
self.assertDraw(tree,
'■ a\n'
' X x\n'
' □ y:')

def test_interface(self):
schema = ('type Query {'
' a: Foo'
Expand Down

0 comments on commit 338c17f

Please sign in to comment.