Skip to content

Commit 9408ba7

Browse files
committed
Fields default_value are no longer dismissed. Fixed #702.
1 parent 05d7e61 commit 9408ba7

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

graphene/types/objecttype.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def __init__(self, *args, **kwargs):
8181

8282
for name, field in fields_iter:
8383
try:
84-
val = kwargs.pop(name, None)
84+
val = kwargs.pop(
85+
name,
86+
field.default_value if isinstance(field, Field) else None
87+
)
8588
setattr(self, name, val)
8689
except KeyError:
8790
pass

graphene/types/tests/test_query.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from ..inputobjecttype import InputObjectType
1010
from ..interface import Interface
1111
from ..objecttype import ObjectType
12-
from ..scalars import Int, String
12+
from ..scalars import Int, String, Boolean
1313
from ..schema import Schema
14-
from ..structures import List
14+
from ..structures import List, NonNull
1515
from ..union import Union
1616
from ..context import Context
1717

@@ -475,3 +475,23 @@ def resolve_info(self, info):
475475
result = test_schema.execute('{ info }', "base")
476476
assert not result.errors
477477
assert result.data == {'info': 'base-info'}
478+
479+
480+
def test_default_as_kwarg_to_NonNull():
481+
# Related to https://github.com/graphql-python/graphene/issues/702
482+
class User(ObjectType):
483+
name = String()
484+
is_admin = NonNull(Boolean, default_value=False)
485+
486+
class Query(ObjectType):
487+
user = Field(User)
488+
489+
def resolve_user(self, *args, **kwargs):
490+
return User(name="foo")
491+
492+
schema = Schema(query=Query)
493+
expected = {'user': {'name': 'foo', 'isAdmin': False}}
494+
result = schema.execute("{ user { name isAdmin } }")
495+
496+
assert not result.errors
497+
assert result.data == expected

0 commit comments

Comments
 (0)